MTSC  1.0.5
Build TCP servers out of modules with handlers for communication, exception and logging.
CommunicationPrimitives.cs
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Net.Security;
5 using System.Net.Sockets;
6 using System.Text;
7 
8 namespace MTSC
9 {
10  static class CommunicationPrimitives
11  {
12  public static string RequestPublicKey = "REQPBKEY";
13  public static string SendPublicKey = "PUBKEY";
14  public static string SendEncryptionKey = "SYMKEY";
15  public static string AcceptEncryptionKey = "SYMKEYOK";
16 
17  public static Message GetMessage(TcpClient client, SslStream sslStream = null)
18  {
19  Stream stream;
20  if (sslStream != null)
21  {
22  stream = sslStream;
23  }
24  else
25  {
26  stream = client.GetStream();
27  }
28  uint messageLength = (uint)client.Available;
29  byte[] messageBuffer = new byte[messageLength];
30  if (messageLength > 0)
31  {
32  stream.Read(messageBuffer, 0, (int)messageLength);
33  }
34  Message message = new Message(messageLength, messageBuffer);
35  return message;
36  }
37 
38  public static void SendMessage(TcpClient client, Message message, SslStream sslStream = null)
39  {
40  Stream stream;
41  if (sslStream != null)
42  {
43  stream = sslStream;
44  }
45  else
46  {
47  stream = client.GetStream();
48  }
49  stream.Write(message.MessageBytes, 0, (int)message.MessageLength);
50  stream.Flush();
51  }
52 
53  public static Message BuildMessage(byte[] msgData)
54  {
55  Message message = new Message((ushort)msgData.Length, msgData);
56  return message;
57  }
58  }
59 }
Definition: Client.cs:14