brk

package module
v0.0.0-...-246df4e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 8, 2024 License: GPL-3.0 Imports: 8 Imported by: 0

README

Build Status GoDoc

Brk

A UDP library with message retrying

Summary

This is a UDP message-passing library that is capable of retrying messages that timeout. It works at the message level, rather than turning the connections into streams. You send a chunk of bytes to the other end, and sometime later, they receive a buffer of bytes.

Features

Send a message to any IP address or port without having to open a connection. Retry failed messages for a period of time.

Install

go get -u github.com/donomii/Brk

Use

package main

import (
	"bufio"
	"flag"
	"os"
	"strconv"
	"fmt"
)

func main() {
	ip := "0.0.0.0"
	port := "6000"

	//NOTE "ip" is the ip address to listen on.  You do not provide the remote server details here!
	//Same for "port"!
	StartRetryServer(ip, port, processor)
}	

you also have to provide a processor function, to deal with the communication channels. Data arrives on incoming, and you send messages on outgoing

func processor(incoming, outgoing chan UdpMessage) {
	message := []byte("Hello out there!")
	remoteServ := "a.server.somwhere.com"
	remotePort := "6000"
	brk.SendMessage(outgoing, message, remoteServ, remotePort)


	//Read incoming messages and print them to the screen
	go func() {
		for mess := range incoming {
			fmt.Printf("Incoming: %v\n", string(mess.Data))
		}
	}()
}

You can send messages at any time after the server starts:

    brk.SendMessage(outgoing, message, remoteServ, remotePort)

The brk.SendMessage function is a convenience wrapper to format the outgoing packet and put it into the outgoing channel.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Qlength int = 2000

Functions

func SendMessage

func SendMessage(outchan chan UdpMessage, data []byte, server string, port int)

SendMessage sends data to server, via outchan. StartUdp passes outchan to the processor function, which is where would normally run SendMessage

func StartRetryUdp

func StartRetryUdp(hostName, portNum string, processor func(a, b chan UdpMessage)) (chan UdpMessage, chan UdpMessage)

This is the UDP connection that retries failed messages. Otherwise, it works exactly like StartUdp. It does not detect duplicate messages, you will have to do that yourself. This is probably the server you want to use.

func StartUdp

func StartUdp(hostName, portNum string, processor func(incoming, outgoing chan UdpMessage)) (chan UdpMessage, chan UdpMessage)

This is the basic UDP connection. It does not support retrying, however it does support sending messages to arbitrary addresses. Please note that the _hostName_ and _portNum_ are the *local* hostname and port number, because this function actually starts a listening server on a local port. Sending messages is a separate function.

You supply the processor function. It must run in a loop, reading from _incoming_, and sending messages to _outgoing_ using the _SendMessage_ function.

The incoming data can be read from packet.Data, after you read the packet from _incoming_.

Types

type UdpMessage

type UdpMessage struct {
	Data     []byte
	Address  string
	Port     int
	Sequence int
	Type     string
	Cached   time.Time
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL