
// This class defines our data packets layout:
class AddPacket : Packet
{
Seconds delay;
char string[1];
// Data follows...
}
class MySocket : Socket
{
// This is invoked every time we receive a packet:
void OnReceivePacket(Packet packet)
{
AddPacket addPacket = (AddPacket)packet;
AddThread{}.AddItem(addPacket.string, addPacket.delay);
}
// This is the method we will call to do something useful... It sends a packet to the server.
void AddItem(char * string, Seconds delay)
{
int len = strlen(string);
uint size = sizeof(class AddPacket) + len;
AddPacket packet = (AddPacket)new byte[size];
packet.size = size;
packet.delay = delay;
CopyBytes(packet.string, string, len+1);
SendPacket(packet);
delete packet;
}
}
// Our service simply instantiates a MySocket object upon accepting a connection
class MyServer : Service
{
void OnAccept()
{
MySocket { this };
}
}
// We're listening on port 1234
MyServer server { port = 1234 };
class MyApplication : GuiApplication
{
bool Init()
{
myApp = this;
// Start the server here
server.Start();
return true;
}
}
MyApplication myApp;
MySocket socket {};
MySocket socket2 {};
socket.Connect("localhost", 1234);
socket2.Connect("localhost", 1234);
Users browsing this forum: No registered users and 1 guest