-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
864 lines (754 loc) · 31.7 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace AsyncSocketServer
{
/// <summary>
/// class OSAsyncEventStack
/// This is a very standard stack implementation.
/// This one is set up to stack asynchronous socket connections.
/// It has only two operations: a push onto the stack, and a pop off of it.
/// </summary>
sealed class OSAsyncEventStack
{
private Stack<SocketAsyncEventArgs> socketstack;
// This constructor needs to know how many items it will be storing max
public OSAsyncEventStack(Int32 maxCapacity)
{
socketstack = new Stack<SocketAsyncEventArgs>(maxCapacity);
}
// Pop an item off of the top of the stack
public SocketAsyncEventArgs Pop()
{
//We are locking the stack, but we could probably use a ConcurrentStack if
// we wanted to be fancy
lock (socketstack)
{
if (socketstack.Count > 0)
{
return socketstack.Pop();
}
else
{
return null;
}
}
}
// Push an item onto the top of the stack
public void Push(SocketAsyncEventArgs item)
{
if (item == null)
{
throw new ArgumentNullException("Cannot add null object to socket stack");
}
lock (socketstack)
{
socketstack.Push(item);
}
}
}
/// <summary>
/// class OSUserToken : IDisposable
/// This class represents the instantiated read socket on the server side.
/// It is instantiated when a server listener socket accepts a connection.
/// </summary>
sealed class OSUserToken : IDisposable
{
// This is a ref copy of the socket that owns this token
private Socket ownersocket;
// this stringbuilder is used to accumulate data off of the readsocket
private StringBuilder stringbuilder;
// This stores the total bytes accumulated so far in the stringbuilder
private Int32 totalbytecount;
// We are holding an exception string in here, but not doing anything with it right now.
public String LastError;
// The read socket that creates this object sends a copy of its "parent" accept socket in as a reference
// We also take in a max buffer size for the data to be read off of the read socket
public OSUserToken(Socket readSocket, Int32 bufferSize)
{
ownersocket = readSocket;
stringbuilder = new StringBuilder(bufferSize);
}
// This allows us to refer to the socket that created this token's read socket
public Socket OwnerSocket
{
get
{
return ownersocket;
}
}
// Do something with the received data, then reset the token for use by another connection.
// This is called when all of the data have been received for a read socket.
public void ProcessData(SocketAsyncEventArgs args)
{
// Get the last message received from the client, which has been stored in the stringbuilder.
String received = stringbuilder.ToString();
//TODO Use message received to perform a specific operation.
Console.WriteLine("Received: \"{0}\". The server has read {1} bytes.", received, received.Length);
//TODO: Load up a send buffer to send an ack back to the calling client
//Byte[] sendBuffer = Encoding.ASCII.GetBytes(received);
//args.SetBuffer(sendBuffer, 0, sendBuffer.Length);
// Clear StringBuffer, so it can receive more data from the client.
stringbuilder.Length = 0;
totalbytecount = 0;
}
// This method gets the data out of the read socket and adds it to the accumulator string builder
public bool ReadSocketData(SocketAsyncEventArgs readSocket)
{
Int32 bytecount = readSocket.BytesTransferred;
if ((totalbytecount + bytecount) > stringbuilder.Capacity)
{
LastError = "Receive Buffer cannot hold the entire message for this connection.";
return false;
}
else
{
stringbuilder.Append(Encoding.ASCII.GetString(readSocket.Buffer, readSocket.Offset, bytecount));
totalbytecount += bytecount;
return true;
}
}
// This is a standard IDisposable method
// In this case, disposing of this token closes the accept socket
public void Dispose()
{
try
{
ownersocket.Shutdown(SocketShutdown.Both);
}
catch
{
//Nothing to do here, connection is closed already
}
finally
{
ownersocket.Close();
}
}
}
/// <summary>
/// class OSCore
/// This is a base class that is used by both clients and servers.
/// It contains the plumbing to set up a socket connection.
/// </summary>
class OSCore
{
// This is just some utilities that we use all over
protected OSUtil os_util;
// these are the defaults if the user does not provide any parameters
protected const string DEFAULT_SERVER = "localhost";
protected const int DEFAULT_PORT = 804;
// We default to a 256 Byte buffer size
protected const int DEFAULT_BUFFER_SIZE = 256;
// This is the connection socket and endpoint information
protected Socket connectionsocket;
protected IPEndPoint connectionendpoint;
// This is some error handling stuff that is not well implemented
protected string lasterror;
protected bool exceptionthrown;
// This is the current buffer size for receive and send
protected int buffersize;
// We only instantiate the utility class here.
// We could probably make it static and avoid this.
public OSCore()
{
os_util = new OSUtil();
}
// An IPEndPoint contains all of the information about a server or client
// machine that a socket needs. Here we create one from information
// that we send in as parameters
public IPEndPoint CreateIPEndPoint(string servername, int portnumber)
{
try
{
// We get the IP address and stuff from DNS (Domain Name Services)
// I think you can also pass in an IP address, but I would not because
// that would not be extensible to IPV6 later
IPHostEntry hostInfo = Dns.GetHostEntry(servername);
IPAddress serverAddr = hostInfo.AddressList[0];
return new IPEndPoint(serverAddr, portnumber);
}
catch (Exception ex)
{
exceptionthrown = true;
lasterror = ex.ToString();
return null;
}
}
// This method peels apart the command string to create either a client or server socket,
// which is not great because it means the method has to know the semantics of the command
// that is passed to it. So this needs to be fixed.
protected bool CreateSocket(string cmdstring)
{
exceptionthrown = false;
if (!string.IsNullOrEmpty(cmdstring))
{
// Here is the utility function that actually parses the command string.
List<string> parameters = os_util.ParseParams(cmdstring);
// Based on the number of parameters in the command string, we create an IPEndPoint
// with the appropriate values for server and port number.
// Implicit in here is the fact that a server always creates on localhost, so the
// startserver command will contain only one parameter (port number), or none.
// Like I said, this needs to be refactored to be more generic
if (parameters.Count < 1)
{
connectionendpoint = CreateIPEndPoint(DEFAULT_SERVER, DEFAULT_PORT);
}
else if (parameters.Count == 1)
{
connectionendpoint = CreateIPEndPoint(DEFAULT_SERVER, Convert.ToInt32(parameters[0]));
}
else
{
connectionendpoint = CreateIPEndPoint(parameters[0], Convert.ToInt32(parameters[1]));
}
}
else
{
connectionendpoint = CreateIPEndPoint(DEFAULT_SERVER, DEFAULT_PORT);
}
// If we get here, we try to create the socket using the IPEndpoint information.
// We are defaulting here to TCP Stream sockets, but you could change this with more parameters.
if (!exceptionthrown)
{
try
{
connectionsocket = new Socket(connectionendpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
}
catch(Exception ex)
{
exceptionthrown = true;
lasterror = ex.ToString();
return false;
}
}
return true;
}
// This method is a lame way for external classes to get the last error message that was posted
// from this class. It is a poor man's exception handler. Don't do this in production code.
// Use proper exception handling.
public string GetLastError()
{
return lasterror;
}
}
/// <summary>
/// class OSServer : OSCore
/// This is the server class that is derived from OSCore.
/// It creates a server that listens for client connections, then receives
/// text data from those clients and writes it to the console screen
/// </summary>
class OSServer : OSCore
{
// We limit this server client connections for test purposes
protected const int DEFAULT_MAX_CONNECTIONS = 4;
// We use a Mutex to block the listener thread so that limited client connections are active
// on the server. If you stop the server, the mutex is released.
private static Mutex mutex;
// Here is where we track the number of client connections
protected int numconnections;
// Here is where we track the totalbytes read by the server
protected int totalbytesread;
// Here is our stack of available accept sockets
protected OSAsyncEventStack socketpool;
// Default constructor
public OSServer()
{
exceptionthrown = false;
// First we set up our mutex and semaphore
mutex = new Mutex();
numconnections = 0;
// Then we create our stack of read sockets
socketpool = new OSAsyncEventStack(DEFAULT_MAX_CONNECTIONS);
// Now we create enough read sockets to service the maximum number of clients
// that we will allow on the server
// We also assign the event handler for IO Completed to each socket as we create it
// and set up its buffer to the right size.
// Then we push it onto our stack to wait for a client connection
for (Int32 i = 0; i < DEFAULT_MAX_CONNECTIONS; i++)
{
SocketAsyncEventArgs item = new SocketAsyncEventArgs();
item.Completed += new EventHandler<SocketAsyncEventArgs>(OnIOCompleted);
item.SetBuffer(new Byte[DEFAULT_BUFFER_SIZE], 0, DEFAULT_BUFFER_SIZE);
socketpool.Push(item);
}
}
// This method is called when there is no more data to read from a connected client
private void OnIOCompleted(object sender, SocketAsyncEventArgs e)
{
// Determine which type of operation just completed and call the associated handler.
// We are only processing receives right now on this server.
switch (e.LastOperation)
{
case SocketAsyncOperation.Receive:
this.ProcessReceive(e);
break;
default:
throw new ArgumentException("The last operation completed on the socket was not a receive");
}
}
// We call this method once to start the server if it is not started
public bool Start(string cmdstring)
{
exceptionthrown = false;
// First create a generic socket
if(CreateSocket(cmdstring))
{
try
{
// Now make it a listener socket at the IP address and port that we specified
connectionsocket.Bind(connectionendpoint);
// Now start listening on the listener socket and wait for asynchronous client connections
connectionsocket.Listen(DEFAULT_MAX_CONNECTIONS);
StartAcceptAsync(null);
mutex.WaitOne();
return true;
}
catch (Exception ex)
{
exceptionthrown = true;
lasterror = ex.ToString();
return false;
}
}
else
{
lasterror = "Unknown Error in Server Start.";
return false;
}
}
// This method is called once to stop the server if it is started.
// We could check for the open socket here
// to stop some exception noise.
public void Stop()
{
connectionsocket.Close();
mutex.ReleaseMutex();
}
// This method implements the asynchronous loop of events
// that accepts incoming client connections
public void StartAcceptAsync(SocketAsyncEventArgs acceptEventArg)
{
// If there is not an accept socket, create it
// If there is, reuse it
if (acceptEventArg == null)
{
acceptEventArg = new SocketAsyncEventArgs();
acceptEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(OnAcceptCompleted);
}
else
{
acceptEventArg.AcceptSocket = null;
}
// this will return true if there is a connection
// waiting to be processed (IO Pending)
bool acceptpending = connectionsocket.AcceptAsync(acceptEventArg);
// If not, we can go ahead and process the accept.
// Otherwise, the Completed event we tacked onto the accept socket will do it when it completes
if (!acceptpending)
{
// Process the accept event
ProcessAccept(acceptEventArg);
}
}
// This method is triggered when the accept socket completes an operation async
// In the case of our accept socket, we are looking for a client connection to complete
private void OnAcceptCompleted(object sender, SocketAsyncEventArgs AsyncEventArgs)
{
ProcessAccept(AsyncEventArgs);
}
// This method is used to process the accept socket connection
private void ProcessAccept(SocketAsyncEventArgs AsyncEventArgs)
{
// First we get the accept socket from the passed in arguments
Socket acceptsocket = AsyncEventArgs.AcceptSocket;
// If the accept socket is connected to a client we will process it
// otherwise nothing happens
if (acceptsocket.Connected)
{
try
{
// Go get a read socket out of the read socket stack
SocketAsyncEventArgs readsocket = socketpool.Pop();
// If we get a socket, use it, otherwise all the sockets in the stack are used up
// and we can't accept anymore connections until one frees up
if (readsocket != null)
{
// Create our user object and put the accept socket into it to use later
readsocket.UserToken = new OSUserToken(acceptsocket, DEFAULT_BUFFER_SIZE);
// We are not using this right now, but it is useful for counting connections
Interlocked.Increment(ref numconnections);
// Start a receive request and immediately check to see if the receive is already complete
// Otherwise OnIOCompleted will get called when the receive is complete
bool IOPending = acceptsocket.ReceiveAsync(readsocket);
if (!IOPending)
{
ProcessReceive(readsocket);
}
}
else
{
acceptsocket.Close();
Console.WriteLine("Client connection refused because the maximum number of client connections allowed on the server has been reached.");
var ex = new Exception("No more connections can be accepted on the server.");
throw ex;
}
}
catch(Exception ex)
{
exceptionthrown = true;
lasterror = ex.ToString();
}
// Start the process again to wait for the next connection
StartAcceptAsync(AsyncEventArgs);
}
}
// This method processes the read socket once it has a transaction
private void ProcessReceive(SocketAsyncEventArgs readSocket)
{
// if BytesTransferred is 0, then the remote end closed the connection
if (readSocket.BytesTransferred > 0)
{
//SocketError.Success indicates that the last operation on the underlying socket succeeded
if (readSocket.SocketError == SocketError.Success)
{
OSUserToken token = readSocket.UserToken as OSUserToken;
if (token.ReadSocketData(readSocket))
{
Socket readsocket = token.OwnerSocket;
// If the read socket is empty, we can do something with the data that we accumulated
// from all of the previous read requests on this socket
if (readsocket.Available == 0)
{
token.ProcessData(readSocket);
}
// Start another receive request and immediately check to see if the receive is already complete
// Otherwise OnIOCompleted will get called when the receive is complete
// We are basically calling this same method recursively until there is no more data
// on the read socket
bool IOPending = readsocket.ReceiveAsync(readSocket);
if (!IOPending)
{
ProcessReceive(readSocket);
}
}
else
{
Console.WriteLine(token.LastError);
CloseReadSocket(readSocket);
}
}
else
{
ProcessError(readSocket);
}
}
else
{
CloseReadSocket(readSocket);
}
}
private void ProcessError(SocketAsyncEventArgs readSocket)
{
Console.WriteLine(readSocket.SocketError.ToString());
CloseReadSocket(readSocket);
}
// This overload of the close method doesn't require a token
private void CloseReadSocket(SocketAsyncEventArgs readSocket)
{
OSUserToken token = readSocket.UserToken as OSUserToken;
CloseReadSocket(token, readSocket);
}
// This method closes the read socket and gets rid of our user token associated with it
private void CloseReadSocket(OSUserToken token, SocketAsyncEventArgs readSocket)
{
token.Dispose();
// Decrement the counter keeping track of the total number of clients connected to the server.
Interlocked.Decrement(ref numconnections);
// Put the read socket back in the stack to be used again
socketpool.Push(readSocket);
}
}
/// <summary>
/// class OSClient : OSCore
/// This is a naive client class that I added into this project just to test the server.
/// It does very little error checking and is not suitable for anything but testing.
/// </summary>
class OSClient : OSCore
{
// This method is used to send a message to the server
public bool Send(string cmdstring)
{
exceptionthrown = false;
var parameters = os_util.ParseParams(cmdstring);
if (parameters.Count > 0)
{
try
{
// We need a connection to the server to send a message
if (connectionsocket.Connected)
{
byte[] byData = System.Text.Encoding.ASCII.GetBytes(parameters[0]);
connectionsocket.Send(byData);
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
lasterror = ex.ToString();
return false;
}
}
else
{
lasterror = "No message provided for Send.";
return false;
}
}
// This method disconnects us from the server
public void DisConnect()
{
try
{
connectionsocket.Close();
}
catch
{
//nothing to do since connection is already closed
}
}
// This method connects us to the server.
// Winsock is very optimistic about connecting to the server.
// It will not tell you, for instance, if the server actually accepted the connection. It assumes that it did.
public bool Connect(string cmdstring)
{
exceptionthrown = false;
if (CreateSocket(cmdstring))
{
try
{
var parameters = os_util.ParseParams(cmdstring);
if (parameters.Count > 1)
{
// This will succeed as long as some server is listening on this IP and port
var connectendpoint = CreateIPEndPoint(parameters[0], Convert.ToInt32(parameters[1]));
connectionsocket.Connect(connectionendpoint);
return true;
}
else
{
lasterror = "Server and Port not specified on client connection.";
return false;
}
}
catch(Exception ex)
{
exceptionthrown = true;
lasterror = ex.ToString();
return false;
}
}
else
{
return false;
}
}
}
/// <summary>
/// class OSUtil
/// This class just does some string tricks for the sample app
/// It is no big deal.
/// </summary>
class OSUtil
{
char[] seps;
// Allowed commands for the console app
public enum os_cmd
{
OS_EXIT,
OS_STARTSERVER,
OS_CONNECT,
OS_SEND,
OS_DISCONNECT,
OS_HELP,
OS_UNDEFINED
}
public OSUtil()
{
seps = new char[] { ' ' };
}
// Parse the parameters from a command string
public List<string> ParseParams(string commandstring)
{
string[] parts = commandstring.Split(seps);
var parameters = new List<string>();
if (parts.Length > 1)
{
for (int i = 1; i < parts.Length; i++)
{
parameters.Add(parts[i]);
}
}
return parameters;
}
// Parse a command from a string
public os_cmd ParseCommand(string commandstring)
{
string[] parts = commandstring.Split(seps);
if (!string.IsNullOrEmpty(parts[0]))
{
string cmd = parts[0];
switch (cmd.ToLower())
{
case "exit":
return os_cmd.OS_EXIT;
break;
case "startserver":
return os_cmd.OS_STARTSERVER;
break;
case "connect":
return os_cmd.OS_CONNECT;
break;
case "disconnect":
return os_cmd.OS_DISCONNECT;
break;
case "send":
return os_cmd.OS_SEND;
break;
case "help":
return os_cmd.OS_HELP;
break;
default:
return os_cmd.OS_UNDEFINED;
break;
}
}
return os_cmd.OS_UNDEFINED;
}
}
/// <summary>
/// This is a console app to test the client and server.
/// It does minimal error handling.
/// To see the valid commands, start the app and type "help" at the command prompt
/// </summary>
class Program
{
// We use util, and one server, and one client in this app
static OSUtil os_util;
static OSServer os_server;
static OSClient os_client;
static void Main(string[] args)
{
//application state trackers
bool shutdown = false;
bool serverstarted = false;
bool clientconnected = false;
os_util = new OSUtil();
// This is a loop to get commands from the user and execute them
while (!shutdown)
{
string userinput = Console.ReadLine();
if (!string.IsNullOrEmpty(userinput))
{
switch(os_util.ParseCommand(userinput))
{
case OSUtil.os_cmd.OS_EXIT:
{
if (serverstarted)
{
os_server.Stop();
}
shutdown = true;
break;
}
case OSUtil.os_cmd.OS_STARTSERVER:
{
if (!serverstarted)
{
os_server = new OSServer();
bool started = os_server.Start(userinput);
if (!started)
{
Console.WriteLine("Failed to Start Server.");
Console.WriteLine(os_server.GetLastError());
}
else
{
Console.WriteLine("Server started successfully.");
serverstarted = true;
}
}
else
{
Console.WriteLine("Server is already running.");
}
break;
}
case OSUtil.os_cmd.OS_CONNECT:
{
if (!clientconnected)
{
os_client = new OSClient();
bool connected = os_client.Connect(userinput);
if (!connected)
{
Console.WriteLine("Failed to connect Client.");
Console.WriteLine(os_client.GetLastError());
}
else
{
Console.WriteLine("Client might be connected. It's hard to say.");
clientconnected = true;
}
}
else
{
Console.WriteLine("Client is already connected");
}
break;
}
case OSUtil.os_cmd.OS_DISCONNECT:
if (clientconnected)
{
os_client.DisConnect();
clientconnected = false;
Console.WriteLine("Client dis-connected from server successfully.");
}
break;
case OSUtil.os_cmd.OS_SEND:
{
if (clientconnected)
{
os_client.Send(userinput);
Console.WriteLine("Message sent from client...");
}
else
{
Console.WriteLine("Send Failed with message:");
Console.WriteLine(os_client.GetLastError());
}
break;
}
case OSUtil.os_cmd.OS_HELP:
{
Console.WriteLine("Available Commands:");
Console.WriteLine("startserver <port> = Start the OS Server (Limit 1 per box)");
Console.WriteLine("connect <server> <port> = Connect the client to the OS Server");
Console.WriteLine("disconnect = Disconnect from the OS Server");
Console.WriteLine("send <message> = Send a message to the OS Server");
Console.WriteLine("exit = Stop the server and quit the program");
break;
}
}
}
}
}
}
}