Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- using System.Net;
- using System.Threading;
- using System.Net.Sockets;
- using System.Windows.Forms;
- namespace Fps_Server
- {
- public class ConnectionClass
- {
- public List<NetworkClient> UnIdentifiedClients = new List<NetworkClient>();
- public bool IsServer = true;
- private static Main_Form MainForm = Main_Form.MainForm;
- private TcpListener Listener;
- bool Stop = false;
- public void StartListening(int Port)
- {
- Listener = new TcpListener(IPAddress.Any, Port);
- Listener.AllowNatTraversal(true);
- Listener.Start(10);
- MainForm.ConsoleWriter("Server is now listening at port: " + Port);
- new Thread(() => ListenLoop()).Start();
- }
- public void StopListening()
- {
- Stop = true;
- Listener.Stop();
- }
- private void ListenLoop()
- {
- Stop = false;
- while (!Stop)
- {
- if (Listener.Pending())
- {
- AddNewClient();
- }
- else
- {
- Thread.Sleep(10);
- }
- }
- }
- private void AddNewClient()
- {
- UnIdentifiedClients.Add(new NetworkClient(Listener.AcceptTcpClient()));
- }
- }
- public class NetworkClient
- {
- public TcpClient Socket;
- public NetworkStream Stream;
- private List<SignalClass> Signals = new List<SignalClass>();
- public NetworkClient()
- {
- }
- public NetworkClient(TcpClient Socket)
- {
- this.Socket = Socket;
- Stream = Socket.GetStream();
- }
- public NetworkClient(TcpClient Socket, NetworkStream Stream)
- {
- this.Socket = Socket;
- this.Stream = Stream;
- }
- public NetworkClient(NetworkClient OriginalClient)
- {
- this.Socket = OriginalClient.Socket;
- this.Stream = OriginalClient.Stream;
- }
- public void Send(SignalClass Signal)
- {
- byte[] Message = AssemblePackage(Signal);
- Stream.Write(BitConverter.GetBytes((ushort)Message.Length), 0, 2);
- Stream.Write(Message, 0, Message.Length);
- }
- public void Receive()
- {
- byte[] Buffer = new byte[2];
- Stream.Read(Buffer, 0, 2);
- ushort MessageLenght = BitConverter.ToUInt16(Buffer, 0);
- byte[] Message = new byte[MessageLenght];
- Stream.Read(Message, 0, MessageLenght);
- SignalClass Signal = DisassemblePackage(Message);
- Signal.Sender = this;
- Signals.Add(Signal);
- }
- public void CloseConnection()
- {
- Stream.Close(10);
- Socket.Close();
- }
- public SignalClass PeekNewestSignal()
- {
- return Signals[Signals.Count - 1];
- }
- public SignalClass PeekOldestSignal()
- {
- return Signals[0];
- }
- public SignalClass PeekSignal(int Index)
- {
- return Signals[Index];
- }
- public SignalClass GetNewestSignal()
- {
- SignalClass Signal = PeekNewestSignal();
- Signals.RemoveAt(Signals.Count - 1);
- return Signal;
- }
- public SignalClass GetOldestSignal()
- {
- SignalClass Signal = PeekOldestSignal();
- Signals.RemoveAt(0);
- return Signal;
- }
- public SignalClass GetSignal(int Index)
- {
- SignalClass Signal = PeekSignal(Index);
- Signals.RemoveAt(Index);
- return Signal;
- }
- public static byte[] AssemblePackage(SignalClass Signal)
- {
- List<byte> Message = new List<byte>();
- if (Signal.ToClient)
- {
- Message.Add((byte)Signal.ClientAction);
- }
- if (Signal.ToServer)
- {
- Message.Add((byte)Signal.ServerAction);
- }
- Message.AddRange(BitConverter.GetBytes(Signal.Destination));
- foreach (object Parameter in Signal.Parameters)
- {
- Message.AddRange(ObjectToBytes(Parameter));
- }
- return Message.ToArray();
- }
- public static byte[] ObjectToBytes(object Parameter)
- {
- List<byte> Bytes = new List<byte>();
- if (Parameter is bool)
- {
- Bytes.Add((byte)ParameterType.Bool);
- Bytes.AddRange(BitConverter.GetBytes((bool)Parameter));
- }
- else if (Parameter is char)
- {
- Bytes.Add((byte)ParameterType.Char);
- Bytes.AddRange(BitConverter.GetBytes((char)Parameter));
- }
- else if (Parameter is double)
- {
- Bytes.Add((byte)ParameterType.Double);
- Bytes.AddRange(BitConverter.GetBytes((double)Parameter));
- }
- else if (Parameter is float)
- {
- Bytes.Add((byte)ParameterType.Float);
- Bytes.AddRange(BitConverter.GetBytes((float)Parameter));
- }
- else if (Parameter is int)
- {
- Bytes.Add((byte)ParameterType.Int);
- Bytes.AddRange(BitConverter.GetBytes((int)Parameter));
- }
- else if (Parameter is long)
- {
- Bytes.Add((byte)ParameterType.Long);
- Bytes.AddRange(BitConverter.GetBytes((long)Parameter));
- }
- else if (Parameter is short)
- {
- Bytes.Add((byte)ParameterType.Short);
- Bytes.AddRange(BitConverter.GetBytes((short)Parameter));
- }
- else if (Parameter is uint)
- {
- Bytes.Add((byte)ParameterType.Uint);
- Bytes.AddRange(BitConverter.GetBytes((uint)Parameter));
- }
- else if (Parameter is ulong)
- {
- Bytes.Add((byte)ParameterType.Ulong);
- Bytes.AddRange(BitConverter.GetBytes((ulong)Parameter));
- }
- else if (Parameter is ushort)
- {
- Bytes.Add((byte)ParameterType.Ushort);
- Bytes.AddRange(BitConverter.GetBytes((ushort)Parameter));
- }
- else if (Parameter is string)
- {
- Bytes.Add((byte)ParameterType.String);
- byte[] String = Encoding.UTF8.GetBytes((string)Parameter);
- Bytes.Add((byte)String.Length);
- Bytes.AddRange(String);
- }
- else if (Parameter is byte)
- {
- Bytes.Add((byte)ParameterType.Byte);
- Bytes.Add((byte)Parameter);
- }
- else
- {
- throw new FormatException("Not supported object type");
- }
- return Bytes.ToArray();
- }
- public static object BytesToObject(byte[] Source, int StartIndex, out int ByteAmountRead)
- {
- if ((ParameterType)Source[StartIndex] == ParameterType.Bool)
- {
- ByteAmountRead = 2;
- return BitConverter.ToBoolean(Source, StartIndex + 1);
- }
- else if ((ParameterType)Source[StartIndex] == ParameterType.Char)
- {
- ByteAmountRead = 3;
- return BitConverter.ToChar(Source, StartIndex + 1);
- }
- else if ((ParameterType)Source[StartIndex] == ParameterType.Double)
- {
- ByteAmountRead = 9;
- return BitConverter.ToDouble(Source, StartIndex + 1);
- }
- else if ((ParameterType)Source[StartIndex] == ParameterType.Float)
- {
- ByteAmountRead = 5;
- return BitConverter.ToSingle(Source, StartIndex + 1);
- }
- else if ((ParameterType)Source[StartIndex] == ParameterType.Int)
- {
- ByteAmountRead = 5;
- return BitConverter.ToInt32(Source, StartIndex + 1);
- }
- else if ((ParameterType)Source[StartIndex] == ParameterType.Long)
- {
- ByteAmountRead = 9;
- return BitConverter.ToInt64(Source, StartIndex + 1);
- }
- else if ((ParameterType)Source[StartIndex] == ParameterType.Short)
- {
- ByteAmountRead = 3;
- return BitConverter.ToInt16(Source, StartIndex + 1);
- }
- else if ((ParameterType)Source[StartIndex] == ParameterType.Uint)
- {
- ByteAmountRead = 5;
- return BitConverter.ToUInt32(Source, StartIndex + 1);
- }
- else if ((ParameterType)Source[StartIndex] == ParameterType.Ulong)
- {
- ByteAmountRead = 9;
- return BitConverter.ToUInt64(Source, StartIndex + 1);
- }
- else if ((ParameterType)Source[StartIndex] == ParameterType.Ushort)
- {
- ByteAmountRead = 3;
- return BitConverter.ToUInt16(Source, StartIndex + 1);
- }
- else if ((ParameterType)Source[StartIndex] == ParameterType.String)
- {
- ByteAmountRead = 2;
- return Encoding.UTF8.GetString(Source, StartIndex + 2, Source[StartIndex + 1]);
- }
- else if ((ParameterType)Source[StartIndex] == ParameterType.Byte)
- {
- ByteAmountRead = 2;
- return Source[StartIndex + 1];
- }
- else
- {
- throw new FormatException("Not supported parametertype");
- }
- }
- public static SignalClass DisassemblePackage(byte[] Message)
- {
- List<byte> ListMessage = Message.ToList();
- SignalClass Signal = new SignalClass();
- if (Main_Form.MainForm.Connection.IsServer)
- {
- Signal.ServerAction = (ToServerAction)ListMessage[0];
- }
- else
- {
- Signal.ClientAction = (ToClientAction)ListMessage[0];
- }
- ListMessage.RemoveAt(0);
- Signal.Destination = BitConverter.ToInt16(ListMessage.ToArray(), 0);
- ListMessage.RemoveRange(0, 2);
- List<object> Parameters = new List<object>();
- while (ListMessage.Count > 0)
- {
- int ByteAmountRead;
- Parameters.Add(BytesToObject(ListMessage.ToArray(), 0, out ByteAmountRead));
- ListMessage.RemoveRange(0, ByteAmountRead);
- }
- return Signal;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment