Sign in to follow this  
  • entries
    557
  • comments
    1237
  • views
    421594

Socketses

Sign in to follow this  
Evil Steve

112 views

Yay, my socket classes are done. Hooray for multithreading. There's 4 main classes in it:
CSocket represents an actual socket, which you can connet with, send data to, and get it's buffer.
CSocketSet is a class that contains a group of CSockets. You can poll it every loop to get a list of sockets that have new data.
CSocketManager doesn't do much. It has functions for allocating and freeing a socket, and a way to map a SOCKET to a CSocket*.
CSocketServer is used for handling incomming connections. It can listen on several ports at once, and can be polled to see if there's a new connection.

Here's some example code (my test code). It listens for connections on port 23, and prints any data recieved to the console. When once socket becomes invalid (the client disconnected), it breaks out of the loop and cleans up.

#include "Socket.h"

int main(int argc, char** argv)
{
CSocketServer theServer;
CSocketSet theSet;
std::vector vSocks;
bool bBreak=false;
BYTE byBuff[256];

// Listen on port 23 //
if(!theServer.Listen(23))
return -1;

// Loop //
while(!bBreak)
{
// Accept new connections //
CSocket* pSock = theServer.GetNextConnection();
if(pSock)
{
vSocks.push_back(pSock);
theSet.AddSocket(pSock);
}

// Process pending //
do {
// Get next socket with data waiting //
pSock = theSet.GetNextPending();
if(!pSock)
{
Sleep(50);
continue;
}

// Is socket valid? //
if(!pSock->IsValid())
bBreak = true;

// Process buffer //
else
{
size_t nLen = pSock->ExtractBuffer(byBuff,255);
if(nLen)
{
byBuff[nLen] = 0;
printf("%s",(char*)byBuff);
}
}
} while(pSock);
}

// Cleanup sockets //
for(DWORD i=0; i CSocketManager::Get().FreeSocket(vSocks);

return 1;
}


I still need to profile it, I'm a bit worried that there might be some overhead of getting data from the reading threads into the actual socket buffers, but I don't think it'll be too bad.

And I still haven't finished bomberman. There's not much left to do at all, but I went out on Wednesday and spent most of Thursday recovering (Damn you GBP1/drink...). And now I have to go out to a friends birthday, so I doubt I'll be able to do any coding when I come in.
Sign in to follow this  


4 Comments


Recommended Comments

There will be no 2-player bomberman in the imediate future. Unless I come back to in later on of course. This socket crap is for my MUD.

Share this comment


Link to comment
Why do you neglect the lesson of Muffins? Without multiplayer, crappy AI will be what your game is known for!!

DO NOT NEGLECT THE LESSON

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now