Thursday, October 13, 2011

SDL_net / SDL_threads(1): The Threading beast, is it so dangerous?

A multi-threaded echo server....

<What is the most important thing beyond every mmorpg? is it the history?, maybe the fancy 3d graphics? nope... it is the network play!>

<When I started the project , one year ago my objective was to bring the game on-line, I just didn't thought how many new things I needed to learn, the C++ language , the libSDL  library, all the other extension libraries (SDL_image, SDL_ttf, SDL_net, SDL_thread, ...), how to use blogger... , the Gimp, etc, etc....>

<It has been a long trip, but now I have arrived where I wanted to be, to the network>

<On the top of the page there is an screen-shot showing my first networking demo, a multi-threaded socket server using SDL_net and SDL_thread>

<I start the server and then I can connect with telnet to the server, every thing I type on the telnet window goes to the server and it returns the message to the sender, maybe you can think it is something too easy, but let me show why it's important...>

<Here what you can't see is more important than what you see, that's the project structure:>

<I encapsulate all the network code into two classes: csockserver and csockclient, fist one opens an SDL_net server socket who is going to sit and wait for incoming connections, when it happens (new client connection), it creates a new csockclient and adds it to the vector, then the csockclient starts a thread and the thread wait for data incoming to the server for a client, when it happens it just echoes the data back to the client>

<it is a very simple way to handle data, a perfect base to start adding new features, now if we change just a few lines we will have a chat server, but threaded>

<I have remarked the thread thing because when I started to look for demos / tutorials to use SDL_net I basically found nothing related to using it with threads, the question is a server needs threads, we can't stop the server logic just to see if a client is sending data, and for the server I'm going to use more threads: (for example , for the A.I.) so many things can happen at the same time, making threaded code is very hard, because any small mistake is going to crash the server, make your computer leak memory, or even worse open a security hole....>

<I'm going to finish the next step (the chat server) and I will post the c++ code>

<Ideas / suggestions are always welcome>

<see you, in the chat>


<B.T.W. Ogres are ugly, SDL_net + threads too, what could happen if you mix them?? ;)>

1 comment:

Jonny D said...

Oh, you had to say it. :)
When I wrote a test chat client, I wanted to add at least one unique feature. For me, that feature was "Unsend". A packet would be sent which told connected clients to erase the last message from that user. Fun times!