Ejemplo n.º 1
0
        void ClientLoop()
        {
            Exception endWith;

            CommandSendPool = new ConnectObjectFromClient();
            IsConnected     = true;
            OnConnectionBegin();
            while (true)
            {
                try
                {
                    ConnectObjectFromServer serverData = ClientSocket.ReceiveOnce <ConnectObjectFromServer>(MaxBitSize);
                    bool recvCmds, sendCmds;
                    lock (CommandSendPool)
                    {
                        InnerCommandPass(serverData);
                        ClientSocket.SendOnce(CommandSendPool);
                        recvCmds = serverData.Commands.Count != 0;
                        sendCmds = CommandSendPool.Commands.Count != 0;
                        CommandSendPool.ClearCommands();
                    }
                    if (DebugMode)
                    {
                        if (recvCmds && sendCmds)
                        {
                            Console.BackgroundColor = ConsoleColor.Red;
                            Console.Write("x");
                        }
                        else if (recvCmds)
                        {
                            Console.BackgroundColor = ConsoleColor.DarkBlue;
                            Console.Write("-");
                        }
                        else if (sendCmds)
                        {
                            Console.BackgroundColor = ConsoleColor.DarkGreen;
                            Console.Write("+");
                        }
                        else
                        {
                            Console.BackgroundColor = ConsoleColor.Black;
                            Console.Write("=");
                        }
                    }
                }
                catch (Exception e)
                {
                    endWith = e;
                    break;
                }
            }
            try
            {
                ClientSocket.Close();
                ClientSocket.Dispose();
            }
            catch { }
            IsConnected = false;
            OnConnectionBreaked(endWith);
        }
Ejemplo n.º 2
0
 void InnerCommandPass(ConnectObjectFromClient clientData)
 {
     for (int i = 0; i < clientData.Commands.Length; i++)
     {
         ConnectCommand command = clientData.Commands[i];
         PassCommand(command);
     }
 }
Ejemplo n.º 3
0
        public void SocketLoop(object objVaildPlayerSocket)
        {
            IsOnline = true;
            socket   = (Socket)objVaildPlayerSocket;
            bool isFirstSend = true;

            OnConnectionBegin();
            Exception breakWith;

            while (true)
            {
                try
                {
                    ConnectObjectFromClient clientData = null;
                    if (isFirstSend)
                    {
                        clientData  = new ConnectObjectFromClient();
                        isFirstSend = false;
                    }
                    else
                    {
                        clientData = socket.ReceiveOnce <ConnectObjectFromClient>(max_bitsize, Id.ToString());
                    }
                    lock (CommandSendPool)
                    {
                        InnerCommandPass(clientData);
                        socket.SendOnce(CommandSendPool);
                        CommandSendPool.ClearCommands();
                    }
                }
                catch (PlayerSocketFatalException e) { breakWith = e; Logger.LogError(e); break; }
                catch (SocketException e) { breakWith = e; Logger.Log(LogLevel.Info, "Player {0}: Stopped Connection", Id); break; }
                catch (Exception e) { breakWith = e; Logger.Log(LogLevel.Warning, "Player {0}: Exception: {1}", Id, e); break; }
            }
            try
            {
                socket.Close();
                socket.Dispose();
            }
            catch { }
            OnConnectionBreaked(breakWith);
            selfDestroy(this);
        }