예제 #1
0
파일: Sockets.cs 프로젝트: rdebath/MCGalaxy
        static void RecvCallback(object sender, SocketAsyncEventArgs e)
        {
            TcpSocket s = (TcpSocket)e.UserToken;

            if (s.Disconnected)
            {
                return;
            }

            try {
                // If received 0, means socket was closed
                int recvLen = e.BytesTransferred;
                if (recvLen == 0)
                {
                    s.Disconnect(); return;
                }

                s.HandleReceived(s.recvBuffer, recvLen);
                if (!s.Disconnected)
                {
                    s.ReceiveNextAsync();
                }
            } catch (SocketException) {
                s.Disconnect();
            } catch (ObjectDisposedException) {
                // Socket was closed by another thread, mark as disconnected
            } catch (Exception ex) {
                Logger.LogError(ex);
                s.Disconnect();
            }
        }
예제 #2
0
        static void RecvCallback(object sender, SocketAsyncEventArgs e)
        {
            Player p = null;

            try {
                TcpSocket s = (TcpSocket)e.UserToken;
                p = s.player;
                if (p.disconnected)
                {
                    return;
                }

                int recvLen = e.BytesTransferred;
                if (recvLen == 0)
                {
                    p.Disconnect(); return;
                }

                // Packets may not always be fully received in a Receive call
                // As such, we may need to retain a little bit of partial packet data
                Buffer.BlockCopy(s.recvBuffer, 0, s.unprocessed, s.unprocessedLen, recvLen);
                s.unprocessedLen += recvLen;
                int processedLen = p.ProcessReceived(s.unprocessed, s.unprocessedLen);

                // Disconnect invalid clients
                if (p.nonPlayerClient && processedLen == -1)
                {
                    s.Close(); p.disconnected = true;
                }
                if (processedLen == -1)
                {
                    return;
                }

                // move remaining partial packet data back to start of unprocessed buffer
                for (int i = processedLen; i < s.unprocessedLen; i++)
                {
                    s.unprocessed[i - processedLen] = s.unprocessed[i];
                }
                s.unprocessedLen -= processedLen;

                if (!p.disconnected)
                {
                    s.ReceiveNextAsync();
                }
            } catch (SocketException) {
                if (p != null)
                {
                    p.Disconnect();
                }
            }  catch (ObjectDisposedException) {
                // Socket was closed by another thread, mark as disconnected
                if (p == null)
                {
                    return;
                }
                Player.connections.Remove(p);
                p.RemoveFromPending();
                p.disconnected = true;
            } catch (Exception ex) {
                Logger.LogError(ex);
                if (p != null)
                {
                    p.Leave("Error!");
                }
            }
        }