Socketses

posted in DruinkJournal
Published March 04, 2005
Advertisement
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.
Previous Entry MUD, Mud, mud...
0 likes 4 comments

Comments

Ravuya
So will Bomberman use this socket crap for two-player multiplayer? Remember the lesson of Muffins.
March 04, 2005 12:02 PM
Evil Steve
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.
March 04, 2005 12:09 PM
Ravuya
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
March 04, 2005 12:32 PM
mattd
March 04, 2005 11:42 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement
Advertisement