UDPIPE

Connects clients and servers with UDP.

When connecting, is uses handshaking, and when connected, it uses PING packets to discover closed connections.  When the connection is stalled, it can queue the messages, which are sent out after the connection is reestablished.

You can use a subclass instance and implement Callbacks to react on new packets.

Client side has only one connection (to the server), but the server side can send messages to any or all of the connected clients.  To send a message to all connected clients, use UDPIPE.sendAll.  To send a message to the specified client, you need to call UDPIPE.send with a client’s socket as an argument.

See also: UDPIPE Description

Usage

Here are the simplest client and a server.  You need a lua with luasockets to run the examples.

Client side

UDPIPE=require 'UDPIPE'
client=UDPIPE()
host, port='127.0.0.1', 1234
client:connect(host, port)

function client:onConnect(sock)
  self:send("HelloWorld")
end

function client:onNewMessage(msg)
  print('Got message:', msg)
end

while true do
  client:step()
end

Server side

UDPIPE=require 'UDPIPE'
server=UDPIPE()
host, port='127.0.0.1', 1234
server:start(port, host)

function server:onNewMessage(msg, sock)
  self:send('Reply from server: '..msg, sock)
end

while true do
  server:step()
end
Summary
UDPIPEConnects clients and servers with UDP.
Private functions
_startUdpStarts listening for UDP packets
Public methods
constructorCreates new UDPIPE instance.
setTimeoutSets the timeout for all current and newly opened sockets.
setDebugSets the debugging.
setTokenSets the token for handshaking.
startStarts the server.
sendAllSends a message to all connected sockets (clients).
sendSends a message to a connected socket.
getSentPacketsReturns the number of packets sent out.
getReceivedPacketsReturns the number of packets received.
pingSends a ping message to server (in client mode) or all connected sockets (in server mode).
stepKeeps the client/server running.
closeClose the connection.
connectConnects as a client to the server.
Private methods
_onTimeoutCalled when no packet is received in some amount of time.
_onNewClientCallback called when new client connection is detected.
_setPeerSets the peer for the socket, makes socket connected.
_unsetPeerUnsets the peer for the socket, makes socket unconnected.
_onNewMessageCalled when new packed arrives on connected socket.
Callbacks
onNewClientCalled when new client is connected.
onNewMessageCalled when new message arrives.
onDisconnectCalled when a socket is disconnected.
onConnectCalled when client connected to the Server, or a new client has connected.
onCloseCalled when client or server closed the connection.
onConnectingCalled when client is connecting to the Server on a final port.
onStalledCalled when the connection lags.
onUnstalledConnection is no longer stalled.
onMaxClientsCalled when too much client connections is open.
onPongCalled when PONG packet is received.
onPingCalled when PING packet is received.
onBadTokenCalled when bad token is received in HELLO packet.

Private functions

_startUdp

Starts listening for UDP packets

Parameters

port(number)
host(string)
timeout(number)

Public methods

constructor

Creates new UDPIPE instance.

Parameters

port(number) port number
host(string) host
timeout(number) socket timeout

setTimeout

function M:setTimeout(t)

Sets the timeout for all current and newly opened sockets.

Parameters

t(number) timeout in seconds

setDebug

function M:setDebug(d)

Sets the debugging.

Parameters

d(bool) turn on/off debugging.

setToken

function M:setToken(t)

Sets the token for handshaking.  Both client and server must have the same token for successful connection.

Parameters

t(string) token

start

function M:start(port,
host,
timeout)

Starts the server.

Parameters

port(number) public known server port
host(string) public known server IP address
timeout(number) [optional] socket timeout

sendAll

function M:sendAll(msg)

Sends a message to all connected sockets (clients).

Parameters

msgmessage

send

function M:send(msg,
sock)

Sends a message to a connected socket.  If the socket is not connected, it buffers the data.

Parameters

msgmessage
socksocket

getSentPackets

function M:getSentPackets()

Returns the number of packets sent out.

Returns

totalSent(number) number of packets sent.

getReceivedPackets

function M:getReceivedPackets()

Returns the number of packets received.

Returns

totalReceived(number) number of packets received.

ping

function M:ping(sock)

Sends a ping message to server (in client mode) or all connected sockets (in server mode).

step

function M:step(dt)

Keeps the client/server running.

Parameters

dtdelta time

close

function M:close(skt)

Close the connection.

In case of client, it also disables automatic reconnection.  Otherwise if the connection was broken by the server, the client will be reconnecting to the server.

Parameters

sktsocket

connect

function M:connect(host,
port,
timeout)

Connects as a client to the server.

Parameters

hostserver address
portserver port
timeouttimeout

Private methods

_onTimeout

function M:_onTimeout()

Called when no packet is received in some amount of time.

_onNewClient

function M:_onNewClient(msg,
ip,
port)

Callback called when new client connection is detected.  It opens the new private connected UDP socket for the client, or calls onMaxClient if the limit is exceeded.  Finally it calls <onNewClient>().

Parameters

msg(string) message
ip(string) IP address
port(number) port

_setPeer

function M:_setPeer(sock,
rip,
rport)

Sets the peer for the socket, makes socket connected.

Parameters

socknetwork socket
ripremote IP
rportremote port

_unsetPeer

function M:_unsetPeer(sock)

Unsets the peer for the socket, makes socket unconnected.

Parameters

socknetwork socket

_onNewMessage

function M:_onNewMessage(msg,
sock,
ip,
port)

Called when new packed arrives on connected socket.

Parameters

msg(string) message
sock(userdata) socket
ipIP
portport

Callbacks

onNewClient

function M:onNewClient(msg,
ip,
port,
ipc,
portc)

Called when new client is connected.

Parameters

msg(string)
ip(string) server IP
port(number) server port
ipc(string) client IP
portc(number) client port

onNewMessage

function M:onNewMessage(msg,
sock)

Called when new message arrives.

Parameters

msgmessage
socksocket

onDisconnect

function M:onDisconnect(sock)

Called when a socket is disconnected.

Parameters

sockclient socket (in case of server code) or server socket.

The sock argument makes sense only in server code, as client is always connected (only) to the server.

onConnect

function M:onConnect(sock,
ip,
port)

Called when client connected to the Server, or a new client has connected.

Parameters

socksocker
ipclient ip (in server mode only)
portclient port (in server mode only)

onClose

function M:onClose(sock)

Called when client or server closed the connection.

Parameters

socksocker

onConnecting

function M:onConnecting(sock,
ip,
port)

Called when client is connecting to the Server on a final port.

Parameters

socksocker
ipremote server ip
portremote server port

onStalled

function M:onStalled(sock)

Called when the connection lags.

Parameters

socksocket

onUnstalled

function M:onUnstalled(sock)

Connection is no longer stalled.

Parameters

socksocket

onMaxClients

function M:onMaxClients(msg,
ip,
port)

Called when too much client connections is open.

Parameters

msglast client message
ipip
portport

onPong

function M:onPong(sock)

Called when PONG packet is received.

Parameters

sock(userdata) socket

onPing

function M:onPing(sock)

Called when PING packet is received.

Parameters

sock(userdata) socket

onBadToken

function M:onBadToken(msg,
ip,
port)

Called when bad token is received in HELLO packet.  The server expects a “HELLO <token>” as a first message from new client.  See setToken.

Parameters

msg(string) original packet payload
ip(string) client ip
port(number) client port
function M:setTimeout(t)
Sets the timeout for all current and newly opened sockets.
function M:setDebug(d)
Sets the debugging.
function M:setToken(t)
Sets the token for handshaking.
function M:start(port,
host,
timeout)
Starts the server.
function M:sendAll(msg)
Sends a message to all connected sockets (clients).
function M:send(msg,
sock)
Sends a message to a connected socket.
function M:getSentPackets()
Returns the number of packets sent out.
function M:getReceivedPackets()
Returns the number of packets received.
function M:ping(sock)
Sends a ping message to server (in client mode) or all connected sockets (in server mode).
function M:step(dt)
Keeps the client/server running.
function M:close(skt)
Close the connection.
function M:connect(host,
port,
timeout)
Connects as a client to the server.
function M:_onTimeout()
Called when no packet is received in some amount of time.
function M:_onNewClient(msg,
ip,
port)
Callback called when new client connection is detected.
function M:_setPeer(sock,
rip,
rport)
Sets the peer for the socket, makes socket connected.
function M:_unsetPeer(sock)
Unsets the peer for the socket, makes socket unconnected.
function M:_onNewMessage(msg,
sock,
ip,
port)
Called when new packed arrives on connected socket.
function M:onNewClient(msg,
ip,
port,
ipc,
portc)
Called when new client is connected.
function M:onNewMessage(msg,
sock)
Called when new message arrives.
function M:onDisconnect(sock)
Called when a socket is disconnected.
function M:onConnect(sock,
ip,
port)
Called when client connected to the Server, or a new client has connected.
function M:onClose(sock)
Called when client or server closed the connection.
function M:onConnecting(sock,
ip,
port)
Called when client is connecting to the Server on a final port.
function M:onStalled(sock)
Called when the connection lags.
function M:onUnstalled(sock)
Connection is no longer stalled.
function M:onMaxClients(msg,
ip,
port)
Called when too much client connections is open.
function M:onPong(sock)
Called when PONG packet is received.
function M:onPing(sock)
Called when PING packet is received.
function M:onBadToken(msg,
ip,
port)
Called when bad token is received in HELLO packet.
Connects clients and servers with UDP.
Close