Guest User

Untitled

a guest
Jul 22nd, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.54 KB | None | 0 0
  1. internal Player(TcpClient TcpClient) {
  2.             try {
  3.                 socket = TcpClient.Client;
  4.                 ip = socket.RemoteEndPoint.ToString().Split(':')[0];
  5.                 ...
  6.                 socket.BeginReceive(tempBuffer, 0, tempBuffer.Length, SocketFlags.None, new AsyncCallback(Incoming), this);
  7.  
  8.                 Server.Connections.Add(this);
  9.             }
  10.             catch (Exception e) {
  11.  
  12.                 Server.Log(e);
  13.             }
  14.         }
  15. protected static void Incoming(IAsyncResult result) {
  16.             while (!Server.Started)
  17.                 Thread.Sleep(100);
  18.  
  19.             Player p = (Player)result.AsyncState;
  20.  
  21.             if (!p.isOnline) return;
  22.  
  23.             try {
  24.                 int length = p.socket.EndReceive(result);
  25.                 if (length == 0) {
  26.                     p.CloseConnection();
  27.                     return;
  28.                 }
  29.                 byte[] b = new byte[p.buffer.Length + length];
  30.                 Buffer.BlockCopy(p.buffer, 0, b, 0, p.buffer.Length);
  31.                 Buffer.BlockCopy(p.tempBuffer, 0, b, p.buffer.Length, length);
  32.                 p.buffer = p.HandlePacket(b);
  33.                 p.socket.BeginReceive(p.tempBuffer, 0, p.tempBuffer.Length, SocketFlags.None, new AsyncCallback(Incoming), p);
  34.             }
  35.             catch (SocketException e) {
  36.                 SocketException rawr = e;
  37.                 p.CloseConnection();
  38.                 return;
  39.             }
  40.             catch (Exception e) {
  41.                 Exception rawr = e;
  42.                 Server.Log(e);
  43.                 return;
  44.             }
  45.         }
  46.   protected byte[] HandlePacket(byte[] buffer) {
  47.             try {
  48.                 int length = 0; byte msg = buffer[0];
  49.                 // Get the length of the message by checking the first byte
  50.                 switch (msg) {
  51.                     case 0: length = 130; break; // login
  52.                     case 2: SMPKick("This is not an SMP Server!"); break; // login??
  53.                     case 5: length = 8; break; // blockchange
  54.                     case 8: length = 9; break; // input
  55.                     case 13: length = 65; break; // chat
  56.                     default: Kick("Unhandled message id \"" + msg + "\"!"); return new byte[0];
  57.                 }
  58.                 if (buffer.Length > length) {
  59.                     byte[] message = new byte[length];
  60.                     Buffer.BlockCopy(buffer, 1, message, 0, length);
  61.  
  62.                     byte[] tempbuffer = new byte[buffer.Length - length - 1];
  63.                     Buffer.BlockCopy(buffer, length + 1, tempbuffer, 0, buffer.Length - length - 1);
  64.  
  65.                     buffer = tempbuffer;
  66.  
  67.                     ThreadPool.QueueUserWorkItem(delegate {
  68.                         switch (msg) {
  69.                             case 0: HandleLogin(message); break;
  70.                             case 5: HandleBlockchange(message); break;
  71.                             case 8: HandleIncomingPos(message); break;
  72.                             case 13: HandleChat(message); break;
  73.                         }
  74.                     });
  75.  
  76.                     if (buffer.Length > 0)
  77.                         buffer = HandlePacket(buffer);
  78.                     else
  79.                         return new byte[0];
  80.  
  81.  
  82.                 }
  83.             }
  84.             catch (Exception e) {
  85.                 Kick("CONNECTION ERROR: (0x03)");
  86.                 Server.Log("[ERROR]: PLAYER MESSAGE RECIEVE ERROR (0x03)", ConsoleColor.Red, ConsoleColor.Black);
  87.                 Server.Log(e);
  88.             }
  89.             return buffer;
  90.         }
Add Comment
Please, Sign In to add comment