コード例 #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[] 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.HandleMessage(b);
                if (p.dontmindme && p.buffer.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.Kick("Error!");
            }
        }