Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- internal Player(TcpClient TcpClient) {
- try {
- socket = TcpClient.Client;
- ip = socket.RemoteEndPoint.ToString().Split(':')[0];
- ...
- socket.BeginReceive(tempBuffer, 0, tempBuffer.Length, SocketFlags.None, new AsyncCallback(Incoming), this);
- Server.Connections.Add(this);
- }
- catch (Exception e) {
- Server.Log(e);
- }
- }
- protected static void Incoming(IAsyncResult result) {
- while (!Server.Started)
- Thread.Sleep(100);
- Player p = (Player)result.AsyncState;
- if (!p.isOnline) return;
- try {
- int length = p.socket.EndReceive(result);
- if (length == 0) {
- p.CloseConnection();
- return;
- }
- byte[] b = new byte[p.buffer.Length + length];
- Buffer.BlockCopy(p.buffer, 0, b, 0, p.buffer.Length);
- Buffer.BlockCopy(p.tempBuffer, 0, b, p.buffer.Length, length);
- p.buffer = p.HandlePacket(b);
- p.socket.BeginReceive(p.tempBuffer, 0, p.tempBuffer.Length, SocketFlags.None, new AsyncCallback(Incoming), p);
- }
- catch (SocketException e) {
- SocketException rawr = e;
- p.CloseConnection();
- return;
- }
- catch (Exception e) {
- Exception rawr = e;
- Server.Log(e);
- return;
- }
- }
- protected byte[] HandlePacket(byte[] buffer) {
- try {
- int length = 0; byte msg = buffer[0];
- // Get the length of the message by checking the first byte
- switch (msg) {
- case 0: length = 130; break; // login
- case 2: SMPKick("This is not an SMP Server!"); break; // login??
- case 5: length = 8; break; // blockchange
- case 8: length = 9; break; // input
- case 13: length = 65; break; // chat
- default: Kick("Unhandled message id \"" + msg + "\"!"); return new byte[0];
- }
- if (buffer.Length > length) {
- byte[] message = new byte[length];
- Buffer.BlockCopy(buffer, 1, message, 0, length);
- byte[] tempbuffer = new byte[buffer.Length - length - 1];
- Buffer.BlockCopy(buffer, length + 1, tempbuffer, 0, buffer.Length - length - 1);
- buffer = tempbuffer;
- ThreadPool.QueueUserWorkItem(delegate {
- switch (msg) {
- case 0: HandleLogin(message); break;
- case 5: HandleBlockchange(message); break;
- case 8: HandleIncomingPos(message); break;
- case 13: HandleChat(message); break;
- }
- });
- if (buffer.Length > 0)
- buffer = HandlePacket(buffer);
- else
- return new byte[0];
- }
- }
- catch (Exception e) {
- Kick("CONNECTION ERROR: (0x03)");
- Server.Log("[ERROR]: PLAYER MESSAGE RECIEVE ERROR (0x03)", ConsoleColor.Red, ConsoleColor.Black);
- Server.Log(e);
- }
- return buffer;
- }
Add Comment
Please, Sign In to add comment