What about coding an efficient socket system?

08/07/2010 14:34 KraHen#1
I want to start a new project from scratch, and I thought about using my sockets from my last unfinished projects until I saw the .NET 3.5 methods. What do you guys think about the methods used in the sample which was released by MS? I`m talking about the BufferManager approach, and the pool for the SocketAsyncEventArgs. It seems nice to me, I implemented this in my project, with some modifications to fit my needs, what do you guys think about it?

#edit : Also, would it be better if I coded the sockets in native?
08/07/2010 14:38 _DreadNought_#2
Does not matter really, Just use Async sockets. Easy to work around I'm learning to use those as well.
08/07/2010 14:45 KraHen#3
Lol, that`s not what I`m talking about. This sample uses a buffer that is allocated for all users, and every user is granted a 'piece' of it. This seems a good method for me, as you don`t have to create new buffers for everyone, even if it limits the amount of users. You don`t have to allocate dynamic memory for everyone, you have one large buffer.
08/07/2010 14:46 _DreadNought_#4
Just tryed to say something smart :) I'm not the best in that area as I've just started learning all this buffer, static buffer shit, :)
08/07/2010 20:18 _Emme_#5
Quote:
Originally Posted by Eliminationn View Post
Does not matter really, Just use Async sockets. Easy to work around I'm learning to use those as well.
Made me giggle
08/07/2010 20:32 KraHen#6
Quote:
Originally Posted by EmmeTheCoder View Post
Made me giggle
And your point related to the topic would be...?
08/07/2010 20:44 CptSky#7
You have 1 or 100 clients and you will use the same memory. A bit stupid to allocate like 100MB for 1 client... I think it's better in a way to allocate a little 2MB for each client. Something like this.
08/07/2010 23:12 Nullable#8
Even 2 mb is too much

Anyways imho best way is dynamically allocating memory per client.
08/07/2010 23:39 KraHen#9
But dynamic memory is allocating memory from the heap, right? And allocating memory from the stack should be faster.
08/08/2010 00:15 Nullable#10
Speed difference is too small, anyhow that won't be an issue because if i'm not mistaken you're going for managed which is slow enough anyway..

On the other hand if you're going for native then you could do better mem. managing by implementing a mem. pool.
And if you're aiming for top notch performance/speed consider writing it for linux/unix, check out beej's guide for a crash course.
08/08/2010 10:03 Korvacs#11
Each connection should have its own buffer if your using sockets, its much easier to manage. One of the main bottle necks is where you split up the packets to be handled so make sure that aspect is small, simple and very efficient.

For the buffer btw, since your dealing with the client's packets you dont need a big buffer, so you can happily allocate 2048 bytes and have done with it without worrying about incomplete packets due to filling the buffer, because you'll never fill it.
08/08/2010 16:52 CptSky#12
Quote:
Originally Posted by Nullable View Post
Even 2 mb is too much

Anyways imho best way is dynamically allocating memory per client.
I know. In general, I allocate between 1 and 3 Ko. Depends of the protocol.
08/08/2010 17:53 _tao4229_#13
Quote:
Originally Posted by Korvacs View Post
Each connection should have its own buffer if your using sockets, its much easier to manage. One of the main bottle necks is where you split up the packets to be handled so make sure that aspect is small, simple and very efficient.

For the buffer btw, since your dealing with the client's packets you dont need a big buffer, so you can happily allocate 2048 bytes and have done with it without worrying about incomplete packets due to filling the buffer, because you'll never fill it.
Possible that it would fill in the case of someones net dropping and then coming back before they time out (of course depends on the time they've dropped and how much data they would've sent in that time). Unlikely, and as long as you have proper checks you can drop someone who lags that bad.
08/09/2010 01:04 CptSky#14
Quote:
Originally Posted by _tao4229_ View Post
Possible that it would fill in the case of someones net dropping and then coming back before they time out (of course depends on the time they've dropped and how much data they would've sent in that time). Unlikely, and as long as you have proper checks you can drop someone who lags that bad.
TQ has a packet for the lag. Just nobody use it.
08/10/2010 12:37 Basser#15
Quote:
Originally Posted by CptSky View Post
TQ has a packet for the lag. Just nobody use it.
Which packet?