Documentation ¶
Index ¶
- func DeserialiseData(data_type interface{}, raw_data []byte) error
- func GenerateRequest(data interface{}, reqType uint8) ([]byte, error)
- func GetLocalIP() (string, error)
- func GetPublicIP() (string, error)
- func Get_TCP_Reply(conn net.Conn, buff_size uint16) ([]byte, error)
- func Handle_Single_TCP_Exchange(target_addr string, data []byte, buff_size uint16) ([]byte, error)
- func SendInitialTCP(target_address string, data []byte) (net.Conn, error)
- func SendTCPReply(conn net.Conn, data []byte) error
- func SendUDP(target_address string, data []byte) error
- type Request
- type TCPListener
- type TCPNetworkData
- type UDPListener
- type UDPNetworkData
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DeserialiseData ¶
DeserialiseData converts a section of raw data to a given struct or slice of structs. The use of this method requires an instance of the struct.
Example:
var frame ImageFrame err := deserialiseData(&frame, req.Request.Payload) //The payload is read inplace into the frame variable
In practice this function should be paired with the DeserialiseRequest function.
func GenerateRequest ¶
GenerateRequest an object or slice of objects, with their request type and serialises them into a byte format that is able to be transmitted over a network.
Example:
//Excerpt from previous project. var ic ImportedCamera _ := deserialiseData(&ic, req.Request.Payload) newCamera := (Logic to generate camera object) outgoingReq, err := generateRequest(newCamera, RequestSuccessful)
func GetLocalIP ¶
GetLocalIP is a function to get the Local IP address on the machine's WiFi network.
Example:
localIP, err := GetLocalIP() if err != nil { fmt.Println("Error getting Local IP:", err) } fmt.Printf("Server Local IP is - %s\n", localIP)
func GetPublicIP ¶
GetPublicIP is a function to get the public IP address of the machine. The function takes no input and returns a string identifying the IP address. It will be noted it is currently relying on a public API so in future there may be bugs / it may not work and will have to be updated.
Example:
publicIP, err := GetPublicIP() if err != nil { fmt.Println("Error getting public IP:", err) } fmt.Printf("Server Global IP is - %s\n", publicIP)
func Get_TCP_Reply ¶
Get_TCP_Reply is used get the reply on a TCP connection. The function pairs well with SendInitialTCP, which is why the function Handle_Single_TCP_Exchange is provided. Something to note is that the buffer size will have to be defined based on how large you're expecting a given reply to be.
Example:
buff, err := Get_TCP_Reply(conn, buff_size) if err != nil { return nil, fmt.Errorf("error in Get_TCP_Reply: %w", err) }
func Handle_Single_TCP_Exchange ¶
Handle_Single_TCP_Exchange handles a single exchange of TCP and then closes the connection. You will have to implement more complex exchanges yourself using functions within this package.
Example:
(Purposefully excluded error handling) garb := NewGarb(8) req, _ := networktools.GenerateRequest(garb, 14) data, _ := networktools.Handle_Single_TCP_Exchange("192.168.1.76:5057", req, 1024)
func SendInitialTCP ¶
SendInitialTCP is used to start a connection between two machines using TCP. It works similarly to SendUDP with the distinction being this function returns a connection. Be aware you will have to close the connection yourself. It was chosen to defer this to the programmer so more complex exchanges could be handled.
Example:
conn, err := SendInitialTCP(target_addr, data) if err != nil { return nil, fmt.Errorf("error in SendInitialTCP: %w", err) } defer conn.Close() // Ensure the connection is closed when we're done
func SendTCPReply ¶
SendTCPReply is a function to reply to a given TCP connection. The function takes a given connection and data to send and returns an error value, with nil implying there has been no error.
Example:
err := SendTCPReply(previous_conn, data) if err != nil { return nil, err }
func SendUDP ¶
SendUDP takes an address and data and uses UDP to send transmit the data. It is advised to use the Request format provided in standards.go and serialise it using GenerateRequest. These are included within the package to make your life easier.
Example:
outgoingReq, err := generateRequest(newCamera, RequestSuccessful) if err != nil { fmt.Println(err) } err = sendUDP(req.Addr.String(), outgoingReq) if err != nil { fmt.Println(err) } fmt.Printf("Successfully transmitted")
Types ¶
type Request ¶
type Request struct { Type uint8 Payload []byte // Raw data, can be interpreted based on the request type }
func DeserialiseRequest ¶
DeserialiseRequest handles the deserialisation of raw data read from a socket into the request standard. You will have to pair this with the DeserialiseData function as the meaning of each request type is left to the programmer.
Example:
n, remoteAddr, err := conn.ReadFromUDP(buffer) // the buffer could be any sort of raw data req, err := deserialiseRequest(buffer[:n]) var c Camera err := deserialiseData(&c, req.Request.Payload) cameraMap.removeCamera(c)
func NewRequest ¶
type TCPListener ¶
func Create_TCP_Listener ¶
func Create_TCP_Listener(port uint16) (chan TCPNetworkData, *TCPListener)
Creates a TCP listener that forwards all requests to a given port on the request channel. The request channel is a collection of TCPNetworkData onjects defined clearly in the standards file. The function will return the request channel and a TCP listener object that represents the TCP listener routeine. To stop listening on the TCP port use the Stop command.
Example Usage:
request_channel, listener := Create_TCP_listener(8080) (code code code) listener.Stop (When you're done)
type TCPNetworkData ¶
func (*TCPNetworkData) Get_Addr ¶
func (d *TCPNetworkData) Get_Addr() net.Addr
type UDPListener ¶
type UDPListener struct {
StopCh chan struct{}
}
func Create_UDP_Listener ¶
func Create_UDP_Listener(port uint16) (chan UDPNetworkData, *UDPListener)
Creates a UDP listener that forwards all requests to a given port to the request channel. The request channel is a collection of UDPNetworkData objects defined clearly in the standards file. The function will return a UDP listener object that represents the UDP listener routine. To stop listening on the UDP port use the Stop command.
Example usage:
requestChannel := make(chan networktools.UDPNetworkData) listener := Create_UDP_listener(8080, requestChannel) (code code code) listener.Stop (when you're done with the listener)
type UDPNetworkData ¶
The key distinction between the network data types is the fact that UDP is connectionless