diondokter

Server ConnectionPage

May 26th, 2014
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.Net;
  8. using System.Threading;
  9. using System.Net.Sockets;
  10. using System.Windows.Forms;
  11.  
  12. namespace Fps_Server
  13. {
  14.     public class ConnectionClass
  15.     {
  16.         public List<NetworkClient> UnIdentifiedClients = new List<NetworkClient>();
  17.         public bool IsServer = true;
  18.  
  19.             private static Main_Form MainForm = Main_Form.MainForm;
  20.         private TcpListener Listener;
  21.  
  22.             bool Stop = false;
  23.  
  24.         public void StartListening(int Port)
  25.         {
  26.             Listener = new TcpListener(IPAddress.Any, Port);
  27.             Listener.AllowNatTraversal(true);
  28.             Listener.Start(10);
  29.  
  30.             MainForm.ConsoleWriter("Server is now listening at port: " + Port);
  31.  
  32.             new Thread(() => ListenLoop()).Start();
  33.         }
  34.  
  35.         public void StopListening()
  36.         {
  37.             Stop = true;
  38.             Listener.Stop();
  39.         }
  40.  
  41.         private void ListenLoop()
  42.         {
  43.             Stop = false;
  44.             while (!Stop)
  45.             {
  46.                 if (Listener.Pending())
  47.                 {
  48.                     AddNewClient();
  49.                 }
  50.                 else
  51.                 {
  52.                     Thread.Sleep(10);
  53.                 }
  54.             }
  55.         }
  56.  
  57.         private void AddNewClient()
  58.         {
  59.             UnIdentifiedClients.Add(new NetworkClient(Listener.AcceptTcpClient()));
  60.         }
  61.     }
  62.  
  63.     public class NetworkClient
  64.     {
  65.         public TcpClient Socket;
  66.         public NetworkStream Stream;
  67.         private List<SignalClass> Signals = new List<SignalClass>();
  68.  
  69.         public NetworkClient()
  70.         {
  71.  
  72.         }
  73.         public NetworkClient(TcpClient Socket)
  74.         {
  75.             this.Socket = Socket;
  76.             Stream = Socket.GetStream();
  77.         }
  78.         public NetworkClient(TcpClient Socket, NetworkStream Stream)
  79.         {
  80.             this.Socket = Socket;
  81.             this.Stream = Stream;
  82.         }
  83.  
  84.         public NetworkClient(NetworkClient OriginalClient)
  85.         {
  86.             this.Socket = OriginalClient.Socket;
  87.             this.Stream = OriginalClient.Stream;
  88.         }
  89.  
  90.         public void Send(SignalClass Signal)
  91.         {
  92.             byte[] Message = AssemblePackage(Signal);
  93.             Stream.Write(BitConverter.GetBytes((ushort)Message.Length), 0, 2);
  94.             Stream.Write(Message, 0, Message.Length);
  95.         }
  96.  
  97.         public void Receive()
  98.         {
  99.             byte[] Buffer = new byte[2];
  100.             Stream.Read(Buffer, 0, 2);
  101.             ushort MessageLenght = BitConverter.ToUInt16(Buffer, 0);
  102.             byte[] Message = new byte[MessageLenght];
  103.             Stream.Read(Message, 0, MessageLenght);
  104.             SignalClass Signal = DisassemblePackage(Message);
  105.             Signal.Sender = this;
  106.             Signals.Add(Signal);
  107.         }
  108.  
  109.         public void CloseConnection()
  110.         {
  111.             Stream.Close(10);
  112.             Socket.Close();
  113.         }
  114.  
  115.         public SignalClass PeekNewestSignal()
  116.         {
  117.             return Signals[Signals.Count - 1];
  118.         }
  119.  
  120.         public SignalClass PeekOldestSignal()
  121.         {
  122.             return Signals[0];
  123.         }
  124.  
  125.         public SignalClass PeekSignal(int Index)
  126.         {
  127.             return Signals[Index];
  128.         }
  129.  
  130.         public SignalClass GetNewestSignal()
  131.         {
  132.             SignalClass Signal = PeekNewestSignal();
  133.             Signals.RemoveAt(Signals.Count - 1);
  134.             return Signal;
  135.         }
  136.  
  137.         public SignalClass GetOldestSignal()
  138.         {
  139.             SignalClass Signal = PeekOldestSignal();
  140.             Signals.RemoveAt(0);
  141.             return Signal;
  142.         }
  143.  
  144.         public SignalClass GetSignal(int Index)
  145.         {
  146.             SignalClass Signal = PeekSignal(Index);
  147.             Signals.RemoveAt(Index);
  148.             return Signal;
  149.         }
  150.  
  151.         public static byte[] AssemblePackage(SignalClass Signal)
  152.         {
  153.             List<byte> Message = new List<byte>();
  154.  
  155.             if (Signal.ToClient)
  156.             {
  157.                 Message.Add((byte)Signal.ClientAction);
  158.             }
  159.             if (Signal.ToServer)
  160.             {
  161.                 Message.Add((byte)Signal.ServerAction);
  162.             }
  163.  
  164.             Message.AddRange(BitConverter.GetBytes(Signal.Destination));
  165.  
  166.             foreach (object Parameter in Signal.Parameters)
  167.             {
  168.                 Message.AddRange(ObjectToBytes(Parameter));
  169.             }
  170.  
  171.             return Message.ToArray();
  172.         }
  173.  
  174.         public static byte[] ObjectToBytes(object Parameter)
  175.         {
  176.             List<byte> Bytes = new List<byte>();
  177.             if (Parameter is bool)
  178.             {
  179.                 Bytes.Add((byte)ParameterType.Bool);
  180.                 Bytes.AddRange(BitConverter.GetBytes((bool)Parameter));
  181.             }
  182.             else if (Parameter is char)
  183.             {
  184.                 Bytes.Add((byte)ParameterType.Char);
  185.                 Bytes.AddRange(BitConverter.GetBytes((char)Parameter));
  186.             }
  187.             else if (Parameter is double)
  188.             {
  189.                 Bytes.Add((byte)ParameterType.Double);
  190.                 Bytes.AddRange(BitConverter.GetBytes((double)Parameter));
  191.             }
  192.             else if (Parameter is float)
  193.             {
  194.                 Bytes.Add((byte)ParameterType.Float);
  195.                 Bytes.AddRange(BitConverter.GetBytes((float)Parameter));
  196.             }
  197.             else if (Parameter is int)
  198.             {
  199.                 Bytes.Add((byte)ParameterType.Int);
  200.                 Bytes.AddRange(BitConverter.GetBytes((int)Parameter));
  201.             }
  202.             else if (Parameter is long)
  203.             {
  204.                 Bytes.Add((byte)ParameterType.Long);
  205.                 Bytes.AddRange(BitConverter.GetBytes((long)Parameter));
  206.             }
  207.             else if (Parameter is short)
  208.             {
  209.                 Bytes.Add((byte)ParameterType.Short);
  210.                 Bytes.AddRange(BitConverter.GetBytes((short)Parameter));
  211.             }
  212.             else if (Parameter is uint)
  213.             {
  214.                 Bytes.Add((byte)ParameterType.Uint);
  215.                 Bytes.AddRange(BitConverter.GetBytes((uint)Parameter));
  216.             }
  217.             else if (Parameter is ulong)
  218.             {
  219.                 Bytes.Add((byte)ParameterType.Ulong);
  220.                 Bytes.AddRange(BitConverter.GetBytes((ulong)Parameter));
  221.             }
  222.             else if (Parameter is ushort)
  223.             {
  224.                 Bytes.Add((byte)ParameterType.Ushort);
  225.                 Bytes.AddRange(BitConverter.GetBytes((ushort)Parameter));
  226.             }
  227.             else if (Parameter is string)
  228.             {
  229.                 Bytes.Add((byte)ParameterType.String);
  230.                 byte[] String = Encoding.UTF8.GetBytes((string)Parameter);
  231.                 Bytes.Add((byte)String.Length);
  232.                 Bytes.AddRange(String);
  233.             }
  234.             else if (Parameter is byte)
  235.             {
  236.                 Bytes.Add((byte)ParameterType.Byte);
  237.                 Bytes.Add((byte)Parameter);
  238.             }
  239.             else
  240.             {
  241.                 throw new FormatException("Not supported object type");
  242.             }
  243.  
  244.             return Bytes.ToArray();
  245.         }
  246.         public static object BytesToObject(byte[] Source, int StartIndex, out int ByteAmountRead)
  247.         {
  248.             if ((ParameterType)Source[StartIndex] == ParameterType.Bool)
  249.             {
  250.                 ByteAmountRead = 2;
  251.                 return BitConverter.ToBoolean(Source, StartIndex + 1);
  252.             }
  253.             else if ((ParameterType)Source[StartIndex] == ParameterType.Char)
  254.             {
  255.                 ByteAmountRead = 3;
  256.                 return BitConverter.ToChar(Source, StartIndex + 1);
  257.             }
  258.             else if ((ParameterType)Source[StartIndex] == ParameterType.Double)
  259.             {
  260.                 ByteAmountRead = 9;
  261.                 return BitConverter.ToDouble(Source, StartIndex + 1);
  262.             }
  263.             else if ((ParameterType)Source[StartIndex] == ParameterType.Float)
  264.             {
  265.                 ByteAmountRead = 5;
  266.                 return BitConverter.ToSingle(Source, StartIndex + 1);
  267.             }
  268.             else if ((ParameterType)Source[StartIndex] == ParameterType.Int)
  269.             {
  270.                 ByteAmountRead = 5;
  271.                 return BitConverter.ToInt32(Source, StartIndex + 1);
  272.             }
  273.             else if ((ParameterType)Source[StartIndex] == ParameterType.Long)
  274.             {
  275.                 ByteAmountRead = 9;
  276.                 return BitConverter.ToInt64(Source, StartIndex + 1);
  277.             }
  278.             else if ((ParameterType)Source[StartIndex] == ParameterType.Short)
  279.             {
  280.                 ByteAmountRead = 3;
  281.                 return BitConverter.ToInt16(Source, StartIndex + 1);
  282.             }
  283.             else if ((ParameterType)Source[StartIndex] == ParameterType.Uint)
  284.             {
  285.                 ByteAmountRead = 5;
  286.                 return BitConverter.ToUInt32(Source, StartIndex + 1);
  287.             }
  288.             else if ((ParameterType)Source[StartIndex] == ParameterType.Ulong)
  289.             {
  290.                 ByteAmountRead = 9;
  291.                 return BitConverter.ToUInt64(Source, StartIndex + 1);
  292.             }
  293.             else if ((ParameterType)Source[StartIndex] == ParameterType.Ushort)
  294.             {
  295.                 ByteAmountRead = 3;
  296.                 return BitConverter.ToUInt16(Source, StartIndex + 1);
  297.             }
  298.             else if ((ParameterType)Source[StartIndex] == ParameterType.String)
  299.             {
  300.                 ByteAmountRead = 2;
  301.                 return Encoding.UTF8.GetString(Source, StartIndex + 2, Source[StartIndex + 1]);
  302.             }
  303.             else if ((ParameterType)Source[StartIndex] == ParameterType.Byte)
  304.             {
  305.                 ByteAmountRead = 2;
  306.                 return Source[StartIndex + 1];
  307.             }
  308.             else
  309.             {
  310.                 throw new FormatException("Not supported parametertype");
  311.             }
  312.         }
  313.  
  314.         public static SignalClass DisassemblePackage(byte[] Message)
  315.         {
  316.             List<byte> ListMessage = Message.ToList();
  317.  
  318.             SignalClass Signal = new SignalClass();
  319.  
  320.             if (Main_Form.MainForm.Connection.IsServer)
  321.             {
  322.                 Signal.ServerAction = (ToServerAction)ListMessage[0];
  323.             }
  324.             else
  325.             {
  326.                 Signal.ClientAction = (ToClientAction)ListMessage[0];
  327.             }
  328.             ListMessage.RemoveAt(0);
  329.  
  330.             Signal.Destination = BitConverter.ToInt16(ListMessage.ToArray(), 0);
  331.             ListMessage.RemoveRange(0, 2);
  332.  
  333.             List<object> Parameters = new List<object>();
  334.             while (ListMessage.Count > 0)
  335.             {
  336.                 int ByteAmountRead;
  337.                 Parameters.Add(BytesToObject(ListMessage.ToArray(), 0, out ByteAmountRead));
  338.                 ListMessage.RemoveRange(0, ByteAmountRead);
  339.             }
  340.  
  341.             return Signal;
  342.         }
  343.     }
Advertisement
Add Comment
Please, Sign In to add comment