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.
UDPIPE Description | Connects clients and servers with UDP. |
The minimal example | Here is an example of a simple client and server using UDPIPE. |
UDPIPE Protocol | Example of a raw network protocol when connecting UDPIPE client and server. |
Using callbacks | For any real life application you need to use UDPIPE.Callbacks. |
Useful UDPIPE methods |
Here is an example of a simple client and server using UDPIPE.
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
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
Example of a raw network protocol when connecting UDPIPE client and server. You can sniff network packets with tcpdump/wireshark to see it for yourself.
C->S: HELLO token S->C: CONNECT IP PORT S->C: GO AWAY! C->SC: CONNECT SC->C: CONNECTED C->SC: PING SC->C: PONG SC->C: PING C->SC: PONG C->SC: data SC->C: data C->SC: CLOSE SC->C: CLOSED SC->C: CLOSE C->SC: CLOSED
The “S” server socket is used only for initializing the connection. The “SC” server socket is used for all data transmission.
For any real life application you need to use UDPIPE.Callbacks. Check the API in the documentation. When implementing your code, you can change:
UDPIPE=require 'UDPIPE'
to this:
UDPIPE=require 'UDPIPEVERBOSE'
And when you run your code from the text console, you get some debugging messages which will help you getting things up.
So now you can code your multiplayer games.
Called when new client is connected.
function M:onNewClient( msg, ip, port, ipc, portc )
Called when new message arrives.
function M:onNewMessage( msg, sock )
Called when client connected to the Server, or a new client has connected.
function M:onConnect( sock, ip, port )
Called when a socket is disconnected.
function M:onDisconnect( sock )
Close the connection.
function M:close( skt )
Called when client or server closed the connection.
function M:onClose( sock )
Called when client is connecting to the Server on a final port.
function M:onConnecting( sock, ip, port )
Called when the connection lags.
function M:onStalled( sock )
Connection is no longer stalled.
function M:onUnstalled( sock )
Called when too much client connections is open.
function M:onMaxClients( msg, ip, port )
Called when PING packet is received.
function M:onPing( sock )
Called when PONG packet is received.
function M:onPong( sock )
Called when bad token is received in HELLO packet.
function M:onBadToken( msg, ip, port )
Sets the timeout for all current and newly opened sockets.
function M:setTimeout( t )
Sets the token for handshaking.
function M:setToken( t )
Starts the server.
function M:start( port, host, timeout )
Keeps the client/server running.
function M:step( dt )
Connects as a client to the server.
function M:connect( host, port, timeout )
Sends a message to a connected socket.
function M:send( msg, sock )
Sends a message to all connected sockets (clients).
function M:sendAll( msg )
Sends a ping message to server (in client mode) or all connected sockets (in server mode).
function M:ping( sock )
Returns the number of packets sent out.
function M:getSentPackets()
Returns the number of packets received.
function M:getReceivedPackets()