Example #1
0
        static void Receive(IAsyncResult result)
        {
            //Server.s.Log(result.AsyncState.ToString());
            Player p = (Player)result.AsyncState;

            if (p.disconnected || p.socket == null)
            {
                return;
            }

            try {
                int length = p.socket.EndReceive(result);
                if (length == 0)
                {
                    p.Disconnect(); return;
                }

                byte[] allData = new byte[p.leftBuffer.Length + length];
                Buffer.BlockCopy(p.leftBuffer, 0, allData, 0, p.leftBuffer.Length);
                Buffer.BlockCopy(p.tempbuffer, 0, allData, p.leftBuffer.Length, length);
                p.leftBuffer = p.ProcessReceived(allData);

                if (p.dontmindme && p.leftBuffer.Length == 0)
                {
                    Server.s.Log("Disconnected");
                    p.socket.Close();
                    p.disconnected = true;
                    return;
                }
                if (!p.disconnected)
                {
                    p.socket.BeginReceive(p.tempbuffer, 0, p.tempbuffer.Length, SocketFlags.None,
                                          new AsyncCallback(Receive), p);
                }
            } catch (SocketException) {
                p.Disconnect();
            }  catch (ObjectDisposedException) {
                // Player is no longer connected, socket was closed
                // Mark this as disconnected and remove them from active connection list
                Player.SaveUndo(p);
                connections.Remove(p);
                p.RemoveFromPending();
                p.disconnected = true;
            } catch (Exception e) {
                Server.ErrorLog(e);
                p.Leave("Error!");
            }
        }