Quantcast
Viewing all articles
Browse latest Browse all 3

Answer by Dan Lyons for Implementation of an asynchronous TCP/UDP server

At a quick glance, there are a couple minor things I noticed regarding how you handle your events:

  • You are passing null event args. I would instead use EventArgs.Empty, as callers will typically assume the EventArgs object they get from the event handler will be non-null.

  • You are using Interlocked.Increment on your connection counter, suggesting you are going to be using this in multi-threaded code.

As such, you should note that

if (OnClientConnected!= null){   OnClientConnected (this, null);}

is not thread-safe. Instead, you will want to do something more like the following:

var  evt = OnClientConnected;if (evt != null){   evt (this, EventArgs.Empty);}

I would suggest converting all your internal members to private, unless there is a specific need for other classes to access them, which seems unlikely, given their content.

Additionally, if SocketConnectionInfo.BufferSize is >= 0, then

if (bytesRead == 0 || (bytesRead > 0 && bytesRead < SocketConnectionInfo.BufferSize))

can be converted to

if (bytesRead < SocketConnectionInfo.BufferSize)

Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>