Hi,
as part of my game development, I've developed a UDP-based network library for the client-server communication, written from scratch because I was unable to find a good one that was simple enough to use.
Now that the library has been decently tested, I think that it could be valuable to start making it an actual "library" that someone could download and use.
As a preliminary step, here's the current state of the API, I would really love to have some feedbacks from the community on this, especially from people that has some kind of experience with network programming.
Here it is, it should be pretty easy to understand, but of course feel free to ask anything if it's not clear
NetworkInterface* Network_Client_Open_Connection(char* IP, int port);
void Network_Close_Connection(NetworkInterface* network, int connectionSlot);
NetworkInterface* Network_Server_Init(int port, int maxConnectionCount);
//returns how many connections were accepted.
int Network_Accept(NetworkInterface* network, int* acceptedSlots, int maxAccepted);
enum SendPacketChannel
{
SendPacket_NoGuarantees,
SendPacket_DeliveryGuaranteed,
SendPacket_Ordered,
};
void Network_Queue_Packet(NetworkInterface* network, int connectionSlot, SendPacketChannel channel,void* data, int size);
bool Network_Flush_Send_Queue(NetworkInterface* network, int connectionSlot, float elapsedTimeSinceLastFlush);
void Network_Receive_Raw_Data(NetworkInterface* network);
#define MAX_PACKET_SIZE 1024
struct NetworkPacketReceived
{
bool disconnected;
int dataSize;
unsigned char data[MAX_PACKET_SIZE];
};
NetworkPacketReceived Network_Get_Packet(NetworkInterface* network, int connectionSlot);
///////////////////////////EXAMPLE///////////////////////
SERVER:
#define NET_IMPLEMENTATION
#include "net.h"
int main()
{
int port = 1111;
int maxConnections = 1024;
NetworkInterface* serverNetwork = Network_Server_Init(port, maxConnections);
while(true)
{
float elapsed = Tick();
int acceptedSlots[16];
int accepted = Network_Accept(NetworkInterface* network, acceptedSlots, ArrayCount(acceptedSlots));
for(int acceptedIndex = 0; acceptedIndex < accepted; ++acceptedIndex)
{
//This is all application code!
Player* player = GetNewPlayer();
player->slot = acceptedSlots[acceptedIndex];
}
Network_Receive_Raw_Data(serverNetwork);
for(everyPlayer)
{
Player* player = GetPlayer();
while(true)
{
NetworkPacketReceived packet = Network_Get_Packet(serverNetwork, player->slot);
if(!packet.size)
{
if(packet.disconnected)
{
Network_Close_Connection(serverNetwork, player->slot);
}
break;
}
else
{
//Do Something with the received packet, in this case we just slam the packet back to the client!
Network_Queue_Packet(serverNetwork, player->slot, SendPacket_NoGuarantees, packet.data, packet.size);
}
}
bool allPacketSent = Network_Flush_Send_Queue(serverNetwork, player->slot, elapsed);
}
}
}
///////////////////////////////////////////
CLIENT
#define NET_IMPLEMENTATION
#include "net.h"
int main()
{
int port = 1111;
char* server = "127.0.0.1";
NetworkInterface* clientNetwork = Network_Client_Open_Connection(server, port);
while(true)
{
float elapsed = Tick();
Network_Receive_Raw_Data(clientNetwork);
while(true)
{
NetworkPacketReceived packet = Network_Get_Packet(clientNetwork, 0);
if(!packet.size)
{
if(packet.disconnected)
{
Network_Close_Connection(clientNetwork, 0);
}
break;
}
else
{
//Do Something with the received packet!
printf(packet.data);
}
}
char toSend[256];
sprintf(toSend, "hello world, this is packet number: %d", packetIndex);
Network_Queue_Packet(clientNetwork, 0, SendPacket_Ordered, toSend, StrLen(toSend));
bool allPacketSent = Network_Flush_Send_Queue(clientNetwork, 0, elapsed);
}
}
There are of course many things missing under the hoods (the library is unable to fragment big packets at the moment, and there's no secure connection, and other things, but those things shouldn't alter the API all that much...
edit: sorry for the indentation... I'll try to make it a little more readable tomorrow...
Best regards,
Leonardo