|
|||||||||||||||||||||
|
|||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
The following source was built using Visual Studio 6.0 SP5 and Visual Studio .NET. You need to have a version of the Microsoft Platform SDK installed Note that the debug builds of the code waste a lot of CPU cycles due to the the debug trace output. It's only worth profiling the release builds.
OverviewWriting a high performance server that runs on Windows NT and uses sockets to communicate with the outside world isn't that hard once you dig through the API references. What's more most of the code is common between all of the servers that you're likely to want to write. It should be possible to wrap all of the common code up in some easy to reuse classes. However, when I went looking for some classes to use to write my first socket server all of the examples and articles that I found required the user to pretty much start from scratch or utilise "cut and paste reuse" when they wanted to use the code in their own servers. Also the more complicated examples, ones that used IO completion ports for example, tended to stop short of demonstrating real world usage. After all, anyone can write an echo server... The aim of this article is to explain the set of reusable classes that I designed for writing socket servers and show how they can be used with servers which do more than simply echo every byte they receive. Note that I'm not going to bother explaining the hows and why's of IO completion ports etc, there are plenty of references available. What does a socket server need to do?A socket server needs to be able to listen on a specific port, accept connections and read and write
data from the socket. A high performance and scaleable socket server should use asynchronous socket IO
and IO completion ports. Since we're using IO completion ports we need to maintain a pool of threads to
service the IO completion packets. If we were to confine ourselves to running on Win2k and above we could
use the Before we can start accepting connections we need to have a socket to listen on. Since there are many different ways to set such a socket up, we'll allow the user's derived class to create this socket by providing a virtual function as follows: virtual SOCKET CreateListeningSocket( unsigned long address, unsigned short port); The server class provides a default implementation that's adequate in most circumstances. It looks something like this: SOCKET CSocketServer::CreateListeningSocket(
unsigned long address,
unsigned short port)
{
SOCKET s = ::WSASocket(AF_INET, SOCK_STREAM, IPPROTO_IP, NULL, 0,
WSA_FLAG_OVERLAPPED);
if (s == INVALID_SOCKET)
{
throw CWin32Exception(_T("CSocketServer::CreateListeningSocket()"),
::WSAGetLastError());
}
CSocket listeningSocket(s);
CSocket::InternetAddress localAddress(address, port);
listeningSocket.Bind(localAddress);
listeningSocket.Listen(5);
return listeningSocket.Detatch();
}
Note that we use a helper class, Now that we have a socket to listen on we can expect to start receiving connections. We'll use
the When a connection occurs we create a void CSocketServer::OnConnectionEstablished( Socket *pSocket, CIOBuffer *pAddress) { const std::string welcomeMessage("+OK POP3 server ready\r\n"); pSocket->Write(welcomeMessage.c_str(), welcomeMessage.length()); pSocket->Read(); } Asynchronous IOSince all of our IO operations are operating asynchronously they return immediately to the calling code.
The actual implementation of these operations is made slightly more complex by the fact that any outstanding
IO requests are terminated when the thread that issued those requests exits. Since we wish to ensure that
our IO requests are not terminated inappropriately we marshal these calls into our socket server's IO
thread pool rather than issuing them from the calling thread. This is done by posting an IO completion
packet to the socket server's IO Completion Port. The server's worker threads know how to handle
4 kinds of operation: Read requests, read completions, write requests and write completions. The request
operations are generated by calls to To be able to read and write data we need somewhere to put it, so we need some kind of memory buffer.
To reduce memory allocations we could pool these buffers so that we don't delete them once they're done
with but instead maintain them in a list for reused. Our data buffers are managed by an allocator which
is configured by passing arguments to the constructor of our socket server. This allows the user to set
the size of the IO buffers used as well as being able to control how many buffers are retained in the
list for reuse. The As all good references on IO Completion Ports tell you, calling The socket server's worker threads loop continuously, blocking on their completion port until work
is available and then extracting the int CSocketServer::WorkerThread::Run() { while (true) { DWORD dwIoSize = 0; Socket *pSocket = 0; OVERLAPPED *pOverlapped = 0; m_iocp.GetStatus((PDWORD_PTR)&pSocket, &dwIoSize, &pOverlapped); CIOBuffer *pBuffer = CIOBuffer::FromOverlapped(pOverlapped); switch pBuffer->GetUserData() { case IO_Read_Request : Read(pSocket, pBuffer); break; case IO_Read_Completed : ReadCompleted(pSocket, pBuffer); break; case IO_Write_Request : Write(pSocket, pBuffer); break; case IO_Write_Completed : WriteCompleted(pSocket, pBuffer); break; } } } Read and write requests cause a read or write to be performed on the socket. Note that the actual
read/write is being performed by our IO threads so that they cannot be terminated early due to the
thread exiting. The virtual void ReadCompleted( Socket *pSocket, CIOBuffer *pBuffer) = 0; virtual void WriteCompleted( Socket *pSocket, CIOBuffer *pBuffer); Our client can provide their own worker thread if they wish, it should derive from the socket server's worker thread. If the client decides to do this then we need to have a way for the server to be configured to use this derived worker thread rather than the default one. Whenever the server creates a worker thread (and this only occurs when the server first starts as the threads run for the life time of the server) it calls the following virtual function: virtual WorkerThread *CreateWorkerThread(
CIOCompletionPort &iocp);
If we want to provide our own implementation of the worker thread then we should override this function and create our thread object and return it to the caller. Graceful shutdownThe send and receive sides of a socket can be closed independently. When a client closes the send side of its connection to a server any reads pending on the socket on the server will return with a value of 0. The derived class can opt to receive a notification when the client closes the send side of its connection by overriding the following virtual function. virtual void OnConnectionClientClose( Socket *pSocket); The server will only receive this notification once even if it had multiple reads outstanding on the socket when the client closed it. The server will not receive 0 length read completions. The closure of the client's send side of the socket, the server's receive side, does not prevent the server from sending more data to the client it simply means that the client has no more data to send to the server. The server can shutdown the connection using the pSocket->Write(pBuffer); pSocket->Shutdown(SD_SEND); Socket closureThe socket is closed when there are no outstanding reads or writes on the socket and no references to the socket
are held. At this point the derived class is notified by a call to the virtual bool OnConnectionClosing( Socket *pSocket); The server's default implementation of this simply returns false which means that derived class doesn't want to
be responsible for closing the socket. The socket server class then closes the socket after first turning off the
linger option. This causes an abortive shutdown and sent data may be lost if it hasn't all been sent when the close
occurs. The derived class may elect to handle socket closure itself and, if so, should override bool CSocketServer::OnConnectionClosing( Socket *pSocket) { // we'll handle socket closure so that we can do a lingering close // note that this is not ideal as this code is executed on the an // IO thread. If we had a thread pool we could fire this off to the // thread pool to handle. pSocket->Close(); return true; } Note that A simple serverWe now have a framework for creating servers. The user simply needs to provide a class that is derived from
class CMySocketServer : CSocketServer { public : CMySocketServer ( unsigned long addressToListenOn, unsigned short portToListenOn); private : virtual void OnConnectionEstablished( Socket *pSocket, CIOBuffer *pAddress); virtual bool OnConnectionClosing( Socket *pSocket); virtual void ReadCompleted( Socket *pSocket, CIOBuffer *pBuffer); }; Implementations for void CMySocketServer::ReadCompleted( Socket *pSocket, CIOBuffer *pBuffer) { pSocket->Write(pBuffer); } YAES - Yet another echo serverA complete echo server is available for download here in JBSocketServer1.zip. The server simply echoes the incoming byte stream back to the client. In addition to implementing the methods discussed above the socket server derived class also implements several 'notification' methods that the server calls to inform the derived class of various internal goings on. The echo server simply outputs a message to the screen (and log file) when these notifications occur but the idea behind them is that the derived class can use them to report on internal server state via performance counters or suchlike. You can test the echo server by using telnet. Simply telnet to localhost on port 5001 (the port that the sample uses by default) and type stuff and watch it get typed back at you. The server runs until a named event is set and then shuts down. The very simple Server Shutdown program, available here, provides an off switch for the server. A slightly more real world exampleServers that do nothing but echo a byte stream are rare, except as poor examples. Normally a server will be expecting a message of some kind, the exact format of the message is protocol specific but two common formats are a binary message with some form of message length indicator in a header and an ASCII text message with a predefined set of 'commands' and a fixed command terminator, often "\r\n". As soon as you start to work with real data you are exposed to a real-world problem that is simply not an issue for echo servers. Real servers need to be able to break the input byte stream provided by the TCP/IP socket interface into distinct commands. The results of issuing a single read on a socket could be any number of bytes up to the size of the buffer that you supplied. You may get a single, distinct, message or you may only get half of a message, or 3 messages, you just can't tell. Too often inexperienced socket developers assume that they'll always get a complete, distinct, message and often their testing methods ensure that this is the case during development. Chunking the byte streamOne of the simplest protocols that a server could implement is a packet based protocol where the first X bytes are a header and the header contains details of the length of the complete packet. The server can read the header, work out how much more data is required and keep reading until it has a complete packet. At this point it can pass the packet to the business logic that knows how to process it. The code to handle this kind of situation might look something like this: void CMySocketServer::ReadCompleted( Socket *pSocket, CIOBuffer *pBuffer) { pBuffer = ProcessDataStream(pSocket, pBuffer); pSocket->Read(pBuffer); } CIOBuffer *CMySocketServer::ProcessDataStream( Socket *pSocket, CIOBuffer *pBuffer) { bool done; do { done = true; const size_t used = pBuffer->GetUsed(); if (used >= GetMinimumMessageSize()) { const size_t messageSize = GetMessageSize(pBuffer); if (used == messageSize) { // we have a whole, distinct, message EchoMessage(pSocket, pBuffer); pBuffer = 0; done = true; } else if (used > messageSize) { // we have a message, plus some more data // allocate a new buffer, copy the extra data into it and try again... CIOBuffer *pMessage = pBuffer->SplitBuffer(messageSize); EchoMessage(pSocket, pMessage); pMessage->Release(); // loop again, we may have another complete message in there... done = false; } else if (messageSize > pBuffer->GetSize()) { Output(_T("Error: Buffer too small\nExpecting: ") + ToString(messageSize) + _T("Got: ") + ToString(pBuffer->GetUsed()) + _T("\nBuffer size = ") + ToString(pBuffer->GetSize()) + _T("\nData = \n") + DumpData(pBuffer->GetBuffer(), pBuffer->GetUsed(), 40)); pSocket->Shutdown(); // throw the rubbish away pBuffer->Empty(); done = true; } } } while (!done); // not enough data in the buffer, reissue a read into the same buffer // to collect more data return pBuffer; } The key points of the code above are that we need to know if we have at least enough data to start
looking at the header, if we do then we can work out the size of the message somehow. Once we know that
we have the minimum amount of data required we can work out if we have all the data that makes up this
message. If we do, great, we process it. If the buffer only contains our message then we simply process
the message and since processing simply involves us posting a write request for the data buffer we return
0 so that the next read uses a new buffer. If we have a complete message and some extra data then we
split the buffer into two, a new one with our complete message in it and the old one which has the extra
data copied to the front of the buffer. We then pass our complete message to the business logic to handle
and loop to handle the data that we had left over. If we don't have enough data we return the buffer and
the Since we're a simple server we have a fairly important limitation, all our messages must fit into the IO buffer size that our server is using. Often this is a practical limitation, maximum message sizes can be known in advance and by setting our IO buffer size to be at least our maximum message size we avoid having to copy data around. If this isn't a viable limitation for your server then you'll need to have an alternative strategy here, copying data out of IO buffers and into something big enough to hold your whole message, or, processing the message in pieces... In our simple server if the message is too big then we simply shutdown the socket connection, throw away the garbage data, and wait for the client to go away... So how do we implement size_t CMySocketServer::GetMinimumMessageSize() const { return 1; } size_t CMySocketServer::GetMessageSize(CIOBuffer *pBuffer) const { size_t messageSize = *pBuffer->GetBuffer(); return messageSize; } Reference counted buffers?You may have noticed that in the case where we had a message and some extra data we called
A packet echo serverA packet based echo server is available for download here in JBSocketServer2.zip. The server expects to receive packets of up to 256 bytes which have a 1 byte header. The header byte contains the total length of the packet (including the header). The server reads complete packets and echoes them back to the client. You can test the echo server by using telnet, if you're feeling clever ;) Simply telnet to localhost on port 5001 (the port that the sample uses by default) and type stuff and watch it get typed back at you. (Hint, CTRL B is 2 which is the smallest packet that contains data). A real internet RFC protocolSome of the common internet protocols, such as RFC 1939 (POP3), use a crlf terminated ASCII text
stream command structure. An example of how such a server might be implemented using the
What next?The classes presented here provide an easy way to develop scalable socket servers using IO completion
and thread pooling in such a way that the user of the classes need not concern themselves with these
low level issues. To create your own server simply derive from CSocketServer server(
"+OK POP3 server ready\r\n",
INADDR_ANY, // address to listen on
5001, // port to listen on
10, // max number of sockets to keep in the pool
10, // max number of buffers to keep in the pool
1024); // buffer size
server.Start();
server.StartAcceptingConnections();
Your code can then do whatever it likes and the socket server runs on its own threads. When you are finished, simply call: server.WaitForShutdownToComplete(); And the socket server will shutdown. In the next article we address the issue of moving the business logic out of the IO thread pool and into a thread pool of its own so that long operations don't block the IO threads. Revision history
Other articles in the series | ||||||||||||||||||||