Main Loop
This happens outside of the server code like so:
0003 /* Run main server loop */
0004 while(1){
0005 server_service();
0006 }
The reason for this is that it allows a person to regularly perform
server related tasks, whilst still restricting the entire application to
one core.
Yes, you heard that correctly. We handle multiple clients in
a single RAM-limited loop (not including the kernel space). This is
achieved with the following:
0007 /* Block until input arrives on one or more active sockets */
0008 read_fd_set = active_fd_set;
0009 if(select(FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) < 0){
We block until there is some activity, in which case the program
comes alive and the main loop begins to spin again. For the most part,
this program is actually quite efficient and hardly does anything at
all.
Write To Client
Writing to client is done in two modes, TCP_DELAY (send
as you like) and TCP_CORK (wait until I tell you send bulk
buffered traffic). The implementations are overlapped, such that there
is good code-reuse.
For example, a call to just server_writeToClient() will
simply use TCP_DELAY (up to 40ms delay).
On the other hand, if you call
server_writeToClient_start() beforehand and then
server_writeToClient_end() after, you can enable and then
disable TCP_CORK. This allows you to buffer messages in the
kernel space and then send them off in one go.
Write To All
Another nice functionality is the ability to write to all active
client sockets, minus one. The idea here is that we shouldn’t be
echo’ing what the user just sent. This has a similar functionality with
server_writeToAll(), as well as the TCP_CORK
enable/disable functions on each socket
(server_writeToAll_start() and
server_writeToAll_end()).
There is even the ability to write a notice to all active client
sockets using the server_writeNoticeAll() function. This
allows us to give global messages such as connect/disconnect
messages.
Commands
Finally we have some basic commands (output from the terminal):
0010 | Commands are as follows:
0011 | /b - Display the banner
0012 | /h or /? - Display this help
0013 | /l - List connected IPs
0014 | /q - Quit the connection
We simply have a way to reprint the banner /b (maybe
there is some MOTD the user misses), display help with /h
(or /? for Windows users), /l to list
currently connected IP addresses and finally /q to quit
(amusingly telnet eats CTRL-C requests so this is
required).