Skip to content

Instantly share code, notes, and snippets.

View martinomburajr's full-sized avatar
🦆
Relentlessly pursuing those carbs

Martin Ombura Jr. martinomburajr

🦆
Relentlessly pursuing those carbs
View GitHub Profile
@martinomburajr
martinomburajr / net-conn-read.go
Last active May 14, 2019 05:07
Reader implementation in the net.Conn
// Read implements the Conn Read method.
func (c *conn) Read(b []byte) (int, error) {
if !c.ok() {
return 0, syscall.EINVAL
}
n, err := c.fd.Read(b)
if err != nil && err != io.EOF {
err = &OpError{Op: "read",
Net: c.fd.net,
Source: c.fd.laddr,
@martinomburajr
martinomburajr / netconn-anatomy.go
Last active May 14, 2019 05:08
The truncated anatomy of the net.Conn interface
// Conn is a generic stream-oriented network connection.
// Comments truncated for brevity
type Conn interface {
Read(b []byte) (n int, err error)
Write(b []byte) (n int, err error)
Close() error
LocalAddr() Addr
RemoteAddr() Addr
SetDeadline(t time.Time) error
@martinomburajr
martinomburajr / netFD.go
Created May 13, 2019 13:27
The struct of a netFD or Network File System
// Network file descriptor.
type netFD struct {
pfd poll.FD
// immutable until Close
family int
sotype int
isConnected bool // handshake completed or use of association with peer
net string
laddr Addr
@martinomburajr
martinomburajr / reading-files-using-file-descriptors.go
Created May 13, 2019 04:56
This is an example of how you can perform a low-level read using file descriptors in go. This will build a knowledge of how file descriptors work and will allow us to understand network connections a bit better
package main
import (
"log"
"syscall"
)
func main() {
//Open File to Retrieve Handler
fd, err := syscall.Open("<absolute-path-to-file>", syscall.O_RDWR, 755)
@martinomburajr
martinomburajr / golang-os-windows-openfile.go
Last active May 13, 2019 04:30
Open file is a private function that showcases how Go Opens a file under the hood.
// Code Snippet 1
// os/file_windows.go
func openFile(name string, flag int, perm FileMode) (file *File, err error) {
r, e := syscall.Open(fixLongPath(name), flag|syscall.O_CLOEXEC, syscallMode(perm))
if e != nil {
return nil, e
}
return newFile(r, name, "file"), nil
}
@martinomburajr
martinomburajr / palindrome.asm
Created May 2, 2019 05:11
A small application written in Assembly language, that checks to see if an input string is a palindrome
#OMBMAR001
.data
isPalinMess: .asciiz "It is a palindrome \n"
noPalinMess: .asciiz "It is not a palindrome \n"
startMess: .asciiz "Enter a number: \n"
temp1: .word 0 # entered number
decima: .word 10 #decima
.text
@martinomburajr
martinomburajr / create-gce-instance.go
Last active June 14, 2020 16:05
How to create a Google Compute Engine Instance using REST calls in Golang.
func CreateGCEInstance() {
//STEP 1 - INIT DATA - Add all your initialization data e.g. projectid, zones, accessTokens.
project := "<your-project-here>"
zone := "<your-zone-here>" //e.g. europe-west1-d
accessToken := "" ////retrieve from https://developers.google.com/oauthplayground/ select relevant compute engine scopes e.g. devstorage/read_write and auth/compute
endpoint := fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%s/zones/%s/instances", project, zone)
//STEP 2 - REQUEST BODY
//Create a REST representation of whats required. see https://cloud.google.com/compute/docs/reference/rest/v1/instances/insert
reqBody := struct {
@martinomburajr
martinomburajr / medium-building-a-go-web-app-from-scratch-to-deploying-on-google-cloud-welcome-app-main.go
Last active December 9, 2020 10:19
medium-building-a-go-web-app-from-scratch-to-deploying-on-google-cloud-welcome-app-main.go
package main
import (
"net/http"
"fmt"
"time"
"html/template"
)
//Create a struct that holds information to be displayed in our HTML file
type Welcome struct {
@martinomburajr
martinomburajr / java-multithreaded-server.java
Last active May 12, 2018 05:25
This is an improvement to our simple server we created earlier. It now supports more than 1 simultaneous connections.
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
/**
* Written by Martin Ombura Jr. <@martinomburajr>
*/
public class MyServer {
public static void main(String[] args) {
@martinomburajr
martinomburajr / java-simple-server.java
Last active May 1, 2021 16:18
This is a simple server written in Java! Use Netcat or Telnet to connect to it!
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
/**
* Written by Martin Ombura Jr. <@martinomburajr>
*/
public class MyServer {
public static void main(String[] args) {