Advertisement
JollyPersonREAL

ServerDataHandler.cs

Aug 29th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace KTWServer
  5. {
  6.     public class ServerDataHandler
  7.     {
  8.         private delegate void Packet_(long connectionID, byte[] data);
  9.  
  10.         private static Dictionary<long, Packet_> packets;
  11.         private static long pLength;
  12.  
  13.         public static void InitializePackets()
  14.         {
  15.             Text.WriteDebug("Initializing Network Packets...");
  16.             packets = new Dictionary<long, Packet_>
  17.             {
  18.                 { (long)ClientPackets.CThankYouMessage, Packet_ThankYouMessage },
  19.                 { (long)ClientPackets.CMovement, Packet_Movement }
  20.             };
  21.         }
  22.  
  23.         public static void HandleData(long connectionID, byte[] data)
  24.         {
  25.             byte[] Buffer;
  26.             Buffer = (byte[])data.Clone();
  27.  
  28.             if (ServerTCP.Client[connectionID].playerBuffer == null)
  29.             {
  30.                 ServerTCP.Client[connectionID].playerBuffer = new ByteBuffer();
  31.             }
  32.             ServerTCP.Client[connectionID].playerBuffer.WriteBytes(Buffer);
  33.  
  34.             if (ServerTCP.Client[connectionID].playerBuffer.Count() == 0)
  35.             {
  36.                 ServerTCP.Client[connectionID].playerBuffer.Clear();
  37.                 return;
  38.             }
  39.  
  40.             if (ServerTCP.Client[connectionID].playerBuffer.Length() >= 8)
  41.             {
  42.                 pLength = ServerTCP.Client[connectionID].playerBuffer.ReadLong(false);
  43.                 if (pLength <= 0)
  44.                 {
  45.                     ServerTCP.Client[connectionID].playerBuffer.Clear();
  46.                     return;
  47.                 }
  48.             }
  49.  
  50.             while (pLength > 0 & pLength <= ServerTCP.Client[connectionID].playerBuffer.Length() - 8)
  51.             {
  52.                 if (pLength <= ServerTCP.Client[connectionID].playerBuffer.Length() - 8)
  53.                 {
  54.                     ServerTCP.Client[connectionID].playerBuffer.ReadLong();
  55.                     data = ServerTCP.Client[connectionID].playerBuffer.ReadBytes((int)pLength);
  56.                     HandleDataPackets(connectionID, data);
  57.                 }
  58.  
  59.                 pLength = 0;
  60.  
  61.                 if (ServerTCP.Client[connectionID].playerBuffer.Length() >= 8)
  62.                 {
  63.                     pLength = ServerTCP.Client[connectionID].playerBuffer.ReadLong(false);
  64.  
  65.                     if (pLength < 0)
  66.                     {
  67.                         ServerTCP.Client[connectionID].playerBuffer.Clear();
  68.                         return;
  69.                     }
  70.                 }
  71.             }
  72.         }
  73.  
  74.         private static void HandleDataPackets(long connectionID, byte[] data)
  75.         {
  76.             long packetIdentifier;
  77.             ByteBuffer buffer = new ByteBuffer();
  78.             buffer.WriteBytes(data);
  79.             packetIdentifier = buffer.ReadLong();
  80.             buffer.Dispose();
  81.  
  82.             if (packets.TryGetValue(packetIdentifier, out Packet_ packet))
  83.             {
  84.                 packet.Invoke(connectionID, data);
  85.             }
  86.         }
  87.  
  88.         private static void Packet_ThankYouMessage(long connectionID, byte[] data)
  89.         {
  90.             ByteBuffer buffer = new ByteBuffer();
  91.             buffer.WriteBytes(data);
  92.  
  93.             long packetIdentifier = buffer.ReadLong();
  94.             string msg = buffer.ReadString();
  95.  
  96.             Console.WriteLine(msg);
  97.  
  98.             buffer.Dispose();
  99.         }
  100.  
  101.         private static void Packet_Movement(long connectionID, byte[] data)
  102.         {
  103.             ByteBuffer buffer = new ByteBuffer();
  104.             buffer.WriteBytes(data);
  105.             long packetIdentifer = buffer.ReadLong();
  106.  
  107.             float x = buffer.ReadFloat();
  108.             float y = buffer.ReadFloat();
  109.             float z = buffer.ReadFloat();
  110.             float rotX = buffer.ReadFloat();
  111.             float rotY = buffer.ReadFloat();
  112.             float rotZ = buffer.ReadFloat();
  113.  
  114.             ServerTCP.SendPlayerMove((int)connectionID, x, y, z, rotX, rotY, rotZ);
  115.         }
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement