Last and current version of the library : v1.12
LIBRARY INFOS : This
library is designed to replace Blitz3D native DirectPlay commands as it
is depreciated in newer Windows version. The UDP network library only
uses UDP protocol for fast packets delivery, but this doesn't guarantee
arrival or order of packets instead of TCP protocol. It includes
automatic server switch if the main server quits and constant ping-pong
from server to clients to check for timeouts. With the help on the
forums, I try to make it easy to use but powerful and reliable.
Thanks to Rick Nasher and Guy Fawkes for testing and suggestions. Thanks to stayne for mapping a level to test the library in good conditions. Thanks to RemiD for discussions about server/client managment.
DOCUMENTATION : First
of all, the library uses variables with "net_" prefix, and functions
with "Net_" prefix, be aware with your own variables and functions.
Available functions :
Net_StartInput() A
basic prompt input console to host a server or join an existing one.
Returns 2 if server is created, or 1 if the designated server is
reachable. Returns 0 if an error occured.
Net_HostServer(port) You
can override the prompt console and directly create a server on
designated port (returns 2 if server created, 0 if not). Avoid to use
port 0, as a random available port will be used instead.
Net_JoinServer(ip$,port) You
can override the prompt console and directly join a server at
designated ip and port (returns 1 if server is reachable, 0 if not). Ip
must be in string dotted format (x.x.x.x).
Net_StopNetwork() Just properly stops the current server or client session, and reset it to default state.
Net_CreatePlayer(name$) Creates
the LOCAL player with the designated name (ie. the player of THIS
computer, this is not used to create other players). This must be called
once and only once after you created or joined a session.
Net_RequestPlayer(id) Used
by clients to ask the server informations about a player ID. This can
be useful if you received a message from a ID you don't know, because
with UDP protocol, packets may not arrive (or eventually arrive in any
order), so you can miss a new player "100" message. This will force the
server to resend you that message with proper informations.
Net_KickPlayer(name$) Kicks the player with the designated name if you are the server.
Net_CheckMessage() Call
this function often to check if a new message has arrived. It returns 1
if a message if available. If so, the variables Net_MsgTyp,
Net_MsgFrom, Net_MsgTo and Net_MsgString will automatically be filled
with appropriate data.
Net_SendMessage(typ,message$="",sender=0,recipient=0) Sends a message to the server or connected clients. This is the function you will use to update your network. -
typ is a number (from 1 to 99) you will use to know what kind of
message it is. For example, 1 will be chat message, 2 will be player
position... it's up to you. You may write message type from 1 to 99 for you needs. - message$ is a string to join to the message. You can use it to send chat message or even data stripped with / for example. - sender is the ID of the player who sent the message. When you send a message, you don't need to fill it, it's automatic. -
recipient is the recipient of the message, or 0 if everyone have to
receive it. If you don't fill it, it will be 0 (=broadcast) NOTE :
a message with recipient = 0 (broadcast), won't be sent to the sender
player. You can force it by filling the recipient with the sender ID.
Available variables :
Net_MsgType : Integer - The identification type of the last received message. Net_MsgFrom : Integer - The ID of the player who sent the message. Net_MsgTo : Integer - The ID of the player who has to receive the message (or 0 if broadcast to everyone). Net_MsgString : String - The message string or data string received with the packet.
Also, the library uses standard message types you will find useful : - 100 : tells that a new player has joined (use Net_MsgString to get the name, and Net_MsgFrom to get the ID). - 101 : tells a player has quit (use Net_MsgString to get the name, and Net_MsgFrom to get the ID). - 102 : the host has changed, the new host ID can be read with Net_MsgFrom. - 103 : ping update (read the ping in Net_MsgString and the player ID in Net_MsgFrom). - 200 : fatal exception, if you receive that, something went wrong...
Library parameters :
net_timeOut : Integer - The time in milliseconds before the connection closes when we don't receive any new packets (timeout).
net_autoRouteMessages : Boolean - Tells if the server has to automatically route clients messages to other clients. If set to True, when a client sends a message to the server, the server automatically send it to all other clients. If
set to False, the server won't route any received message to other
clients, so you can, for example check for cheats, correct the packet,
ignore it etc... server side.
net_autoSwitchHost : Boolean
- Allows the server to switch to another player if it quits (the new
host will be the most ancient connected player). Check for message type
102 to see when it happens.
net_defaultPort : Integer -
The default port used by the library if you don't fill a port in the
input console. Port must be in the range 0-65535.
Send binary data : Since v1.1, you can append binary data to your messages very easily. You can use the functions Net_WriteByte(byte), Net_WriteShort(short), Net_WriteInt(int), Net_WriteFloat(float), Net_WriteString(string) to write what you want to your message. You will have to do that just before you use the SendMessage() command. To read binary data back from a message, use the functions Net_ReadByte(), Net_ReadShort(), Net_ReadInt(), Net_ReadFloat(), Net_ReadString(), in the same order you wrote the data. See the examples for more infos.
LIBRARY CODE :
Name it "UDPNetwork_lib.bb" and include it in your game code.
SIMPLE 2D EXAMPLE (TUTORIAL) :
This example is quite simple. It
can be a kind of a tutorial to start to use the library. It's fully
commented so it seems to be big, but the code itself is not ! :-)
ADVANCED 3D EXAMPLE :
This is an advanced example I try to improve each time to see what we can get with the library. Each
player sends his position to the server at a constant rate. Beetween
position packets, linear interpolation is done to position the players
smoothly. Also, I finally use tweening in the main loop, so the game
logic is updated at a constant rate, but the network is updated outside
tweening, as fast as possible to reduce the ping.
|