Пример #1
0
        private void ChildSocket_WriteCompleted(ServerChildTcpSocket socket, AsyncCompletedEventArgs e)
        {
            // Check for errors
            if (e.Error != null)
            {
                // Note: WriteCompleted may be called as the result of a normal write (SocketPacketizer.WritePacketAsync),
                //  or as the result of a call to SocketPacketizer.WriteKeepaliveAsync. However, WriteKeepaliveAsync
                //  will never invoke WriteCompleted if the write was successful; it will only invoke WriteCompleted if
                //  the keepalive packet failed (indicating a loss of connection).

                // If you want to get fancy, you can tell if the error is the result of a write failure or a keepalive
                //  failure by testing e.UserState, which is set by normal writes.
                if (e.UserState is string)
                {
                    textBoxLog.AppendText("Socket error during Write to " + socket.RemoteEndPoint.ToString() + ": [" + e.Error.GetType().Name + "] " + e.Error.Message + Environment.NewLine);
                }
                else
                {
                    textBoxLog.AppendText("Socket error detected by keepalive to " + socket.RemoteEndPoint.ToString() + ": [" + e.Error.GetType().Name + "] " + e.Error.Message + Environment.NewLine);
                }

                ResetChildSocket(socket);
            }
            else
            {
                string description = (string)e.UserState;
                textBoxLog.AppendText("Socket write completed to " + socket.RemoteEndPoint.ToString() + " for message " + description + Environment.NewLine);
            }

            RefreshDisplay();
        }
Пример #2
0
        private void IncomingConnectedAccepted(AsyncResultEventArgs <ServerChildTcpSocket> args)
        {
            if (args.Error != null)
            {
                string dumpPath = CoreManager.ServerCore.DumpException(args.Error);
                CoreManager.ServerCore.ConsoleManager.Error("Game Socket Manager", "Incoming connection failed!!");
                CoreManager.ServerCore.ConsoleManager.Error("Game Socket Manager", "    An exception dump has been saved to " + dumpPath);
                _listeningSocket.AcceptAsync();
                return;
            }

            ServerChildTcpSocket internalSocket = args.Result;
            GameSocket           socket         = new GameSocket(internalSocket, this);

            GameSocketEventArgs eventArgs = new GameSocketEventArgs(socket);

            CoreManager.ServerCore.OfficalEventFirer.Fire("incoming_game_connection:before", eventArgs);
            if (eventArgs.IsCancelled)
            {
                socket.Disconnect("Connection rejected from " + internalSocket.RemoteEndPoint + "(" + eventArgs.CancelReason + ")");
                return;
            }
            socket.Start();
            CoreManager.ServerCore.OfficalEventFirer.Fire("incoming_game_connection:after", eventArgs);
            CoreManager.ServerCore.ConsoleManager.Notice("Game Socket Manager", "Incoming connection accepted: " + internalSocket.RemoteEndPoint);

            _listeningSocket.AcceptAsync();
        }
Пример #3
0
        internal GameSocket(ServerChildTcpSocket socket, GameSocketProtocol protocol)
        {
            _internalSocket = socket;
            _lengthBuffer   = new byte[protocol.Reader.LengthBytes];

            Habbo = HabboDistributor.GetPreLoginHabbo(this);
        }
Пример #4
0
        private void IncomingConnectedAccepted(AsyncResultEventArgs <ServerChildTcpSocket> args)
        {
            if (args.Error != null)
            {
                // TODO: Die safely?
                CoreManager.ServerCore.StandardOut.Error("Game Socket Manager", "Incoming connection failed!!");

                // TODO: Pretty exception reporting
                Console.WriteLine();
                Console.WriteLine(args.Error.Message);
                Console.WriteLine(args.Error.StackTrace);

                _listeningSocket.AcceptAsync();
                return;
            }

            ServerChildTcpSocket internalSocket = args.Result;
            GameSocket           socket         = new GameSocket(internalSocket, Protocol);

            CancelReasonEventArgs connectionEventArgs = new CancelReasonEventArgs();

            CoreManager.ServerCore.OfficalEventFirer.Fire("incoming_game_connection", EventPriority.Before, socket, connectionEventArgs);
            if (connectionEventArgs.Cancel)
            {
                socket.Disconnect("Connection rejected from " + internalSocket.RemoteEndPoint + "(" + connectionEventArgs.CancelReason + ")");
                return;
            }

            socket.Start();

            CoreManager.ServerCore.OfficalEventFirer.Fire("incoming_game_connection", EventPriority.After, socket, connectionEventArgs);
            CoreManager.ServerCore.StandardOut.Notice("Game Socket Manager", "Incoming connection accepted: " + internalSocket.RemoteEndPoint);

            _listeningSocket.AcceptAsync();
        }
Пример #5
0
        internal GameSocket(ServerChildTcpSocket socket, GameSocketManager manager)
        {
            _internalSocket = socket;
            _lengthBuffer   = new byte[manager.Protocol.Reader.LengthBytes];

            GameSocketManager = manager;
            GameSocketManager.Protocol.HandlerInvokerManager[this] = new GameSocketMessageHandlerInvoker();

            Habbo = HabboDistributor.GetPreLoginHabbo(this);
        }
Пример #6
0
        /// <summary>
        /// Closes and clears a child socket (established connection), without causing exceptions.
        /// </summary>
        /// <param name="childSocket">The child socket to close. May be null.</param>
        private void ResetChildSocket(ServerChildTcpSocket childSocket)
        {
            // Close the child socket if possible
            if (childSocket != null)
            {
                childSocket.Close();
            }

            // Remove it from the list of child sockets
            ChildSockets.Remove(childSocket);
        }
Пример #7
0
        private void ChildSocket_PacketArrived(ServerChildTcpSocket socket, AsyncResultEventArgs <byte[]> e)
        {
            try
            {
                // Check for errors
                if (e.Error != null)
                {
                    textBoxLog.AppendText("Client socket error during Read from " + socket.RemoteEndPoint.ToString() + ": [" + e.Error.GetType().Name + "] " + e.Error.Message + Environment.NewLine);
                    ResetChildSocket(socket);
                }
                else if (e.Result == null)
                {
                    // PacketArrived completes with a null packet when the other side gracefully closes the connection
                    textBoxLog.AppendText("Socket graceful close detected from " + socket.RemoteEndPoint.ToString() + Environment.NewLine);

                    // Close the socket and remove it from the list
                    ResetChildSocket(socket);
                }
                else
                {
                    // At this point, we know we actually got a message.

                    // Deserialize the message
                    object message = Messages.Util.Deserialize(e.Result);

                    // Handle the message
                    Messages.StringMessage stringMessage = message as Messages.StringMessage;
                    if (stringMessage != null)
                    {
                        textBoxLog.AppendText("Socket read got a string message from " + socket.RemoteEndPoint.ToString() + ": " + stringMessage.Message + Environment.NewLine);
                        return;
                    }

                    Messages.ComplexMessage complexMessage = message as Messages.ComplexMessage;
                    if (complexMessage != null)
                    {
                        textBoxLog.AppendText("Socket read got a complex message from " + socket.RemoteEndPoint.ToString() + ": (UniqueID = " + complexMessage.UniqueID.ToString() +
                                              ", Time = " + complexMessage.Time.ToString() + ", Message = " + complexMessage.Message + ")" + Environment.NewLine);
                        return;
                    }

                    textBoxLog.AppendText("Socket read got an unknown message from " + socket.RemoteEndPoint.ToString() + " of type " + message.GetType().Name + Environment.NewLine);
                }
            }
            catch (Exception ex)
            {
                textBoxLog.AppendText("Error reading from socket " + socket.RemoteEndPoint.ToString() + ": [" + ex.GetType().Name + "] " + ex.Message + Environment.NewLine);
                ResetChildSocket(socket);
            }
            finally
            {
                RefreshDisplay();
            }
        }
Пример #8
0
        private void ListeningSocket_AcceptCompleted(AsyncResultEventArgs <ServerChildTcpSocket> e)
        {
            // Check for errors
            if (e.Error != null)
            {
                ResetListeningSocket();
                textBoxLog.AppendText("Socket error during Accept: [" + e.Error.GetType().Name + "] " + e.Error.Message + Environment.NewLine);
                RefreshDisplay();
                return;
            }

            // Always continue listening for other connections
            ListeningSocket.AcceptAsync();

            ServerChildTcpSocket socket = e.Result;

            try
            {
                // Save the new child socket connection, and create a packetizer for it
                SocketPacketProtocol protocol = new SocketPacketProtocol(socket);
                ChildSocketContext   context  = new ChildSocketContext();
                context.Protocol = protocol;
                context.State    = ChildSocketState.Connected;
                ChildSockets.Add(socket, context);

                protocol.PacketArrived   += (args) => ChildSocket_PacketArrived(socket, args);
                socket.WriteCompleted    += (args) => ChildSocket_WriteCompleted(socket, args);
                socket.ShutdownCompleted += (args) => ChildSocket_ShutdownCompleted(socket, args);

                // Display the connection information
                textBoxLog.AppendText("Connection established to " + socket.RemoteEndPoint.ToString() + Environment.NewLine);

                // Start reading data from the connection
                protocol.Start();
            }
            catch (Exception ex)
            {
                ResetChildSocket(socket);
                textBoxLog.AppendText("Socket error accepting connection: [" + ex.GetType().Name + "] " + ex.Message + Environment.NewLine);
            }
            finally
            {
                RefreshDisplay();
            }
        }
Пример #9
0
        private void ChildSocket_ShutdownCompleted(ServerChildTcpSocket socket, AsyncCompletedEventArgs e)
        {
            // Check for errors
            if (e.Error != null)
            {
                textBoxLog.AppendText("Socket error during Shutdown of " + socket.RemoteEndPoint.ToString() + ": [" + e.Error.GetType().Name + "] " + e.Error.Message + Environment.NewLine);
                ResetChildSocket(socket);
            }
            else
            {
                textBoxLog.AppendText("Socket shutdown completed on " + socket.RemoteEndPoint.ToString() + Environment.NewLine);

                // Close the socket and remove it from the list
                ResetChildSocket(socket);
            }

            RefreshDisplay();
        }