예제 #1
0
        private void DisconnectOnError(string info, Exception ex)
        {
            UsLogging.Printf(LogWndOpt.Bold, info);
            UsLogging.Printf(ex.ToString());

            Disconnect();
        }
예제 #2
0
        public void Tick_CheckConnectionStatus()
        {
            try
            {
                if (!_tcpClient.Connected)
                {
                    UsLogging.Printf("disconnection detected. (_tcpClient.Connected == false).");
                    throw new Exception();
                }

                // check if the client socket is still readable
                if (_tcpClient.Client.Poll(0, SelectMode.SelectRead))
                {
                    byte[] checkConn = new byte[1];
                    if (_tcpClient.Client.Receive(checkConn, SocketFlags.Peek) == 0)
                    {
                        UsLogging.Printf("disconnection detected. (failed to read by Poll/Receive).");
                        throw new IOException();
                    }
                }
            }
            catch (Exception ex)
            {
                DisconnectOnError("disconnection detected while checking connection status.", ex);
            }
        }
예제 #3
0
        public static void Print(UsLogPacket packet)
        {
            if (IsLogFiltered(packet.LogType))
            {
                return;
            }

            string logTypeStr = "";

            switch (packet.LogType)
            {
            case UsLogType.Error:
            case UsLogType.Exception:
            case UsLogType.Assert:
            case UsLogType.Warning:
                logTypeStr = string.Format("[b][color={0}]({1})[/color][/b]", s_type2color[packet.LogType], packet.LogType);
                break;

            case UsLogType.Log:
            default:
                break;
            }

            string timeStr = string.Format("[color={0}]{1:0.00}({2})[/color]", s_gameLogTimeColor, packet.RealtimeSinceStartup, packet.SeqID);

            string ret = string.Format("{0} {1} {2}", timeStr, logTypeStr, packet.Content);

            if (!IsCallstackFiltered(packet.LogType) && !string.IsNullOrEmpty(packet.Callstack))
            {
                ret += string.Format("\n[color=DarkGray]{0}[/color]", packet.Callstack);
            }

            UsLogging.Printf(LogWndOpt.NetLog, ret);
        }
예제 #4
0
        private bool Handle_ExecCommandResponse(eNetCmd cmd, UsCmd c)
        {
            string ret = c.ReadString();

            UsLogging.Printf(string.Format("command executing result: [b]{0}[/b]", ret));

            return(true);
        }
예제 #5
0
 public void Connect(string host, int port)
 {
     _host      = host;
     _port      = port;
     _tcpClient = new TcpClient();
     _tcpClient.BeginConnect(_host, _port, OnConnect, _tcpClient);
     UsLogging.Printf(LogWndOpt.Bold, "connecting to [u]{0}:{1}[/u]...", host, port);
 }
예제 #6
0
        private bool Handle_HandshakeResponse(eNetCmd cmd, UsCmd c)
        {
            UsLogging.Printf("eNetCmd.SV_HandshakeResponse received, connection validated.");

            SysPost.InvokeMulticast(this, LogicallyConnected);

            _guardTimer.Deactivate();
            return(true);
        }
예제 #7
0
        public bool Connect(string addr)
        {
            int port = 0;

            if (!int.TryParse(Properties.Settings.Default.ServerPort, out port))
            {
                UsLogging.Printf(LogWndOpt.Error, "Properties.Settings.Default.ServerPort '{0}' invalid, connection aborted.", Properties.Settings.Default.ServerPort);
                return(false);
            }

            _client.Connect(addr, port);
            return(true);
        }
예제 #8
0
        public void Disconnect()
        {
            if (_tcpClient != null)
            {
                _tcpClient.Close();
                _tcpClient = null;

                _host = "";
                _port = 0;

                UsLogging.Printf("connection closed.");
                SysPost.InvokeMulticast(this, Disconnected);
            }
        }
예제 #9
0
        // Called when a connection to a server is established
        private void OnConnect(IAsyncResult asyncResult)
        {
            // Retrieving TcpClient from IAsyncResult
            TcpClient tcpClient = (TcpClient)asyncResult.AsyncState;

            try
            {
                if (tcpClient.Connected) // may throw NullReference
                {
                    UsLogging.Printf("connected successfully.");
                    SysPost.InvokeMulticast(this, Connected);
                }
                else
                {
                    throw new Exception();
                }
            }
            catch (Exception ex)
            {
                DisconnectOnError("connection failed while handling OnConnect().", ex);
            }
        }
예제 #10
0
        public void ExecuteCmd(string cmdText)
        {
            if (!IsConnected)
            {
                UsLogging.Printf(LogWndOpt.Bold, "not connected to server, command ignored.");
                return;
            }

            if (cmdText.Length == 0)
            {
                UsLogging.Printf(LogWndOpt.Bold, "the command bar is empty, try 'help' to list all supported commands.");
                return;
            }

            UsCmd cmd = new UsCmd();

            cmd.WriteNetCmd(eNetCmd.CL_ExecCommand);
            cmd.WriteString(cmdText);
            Send(cmd);

            UsLogging.Printf(string.Format("command executed: [b]{0}[/b]", cmdText));
        }
예제 #11
0
        public void Tick_ReceivingData()
        {
            try
            {
                while (_tcpClient.Available > 0)
                {
                    byte[] cmdLenBuf  = new byte[2];
                    int    cmdLenRead = _tcpClient.GetStream().Read(cmdLenBuf, 0, cmdLenBuf.Length);
                    ushort cmdLen     = BitConverter.ToUInt16(cmdLenBuf, 0);
                    if (cmdLenRead > 0 && cmdLen > 0)
                    {
                        byte[] buffer = new byte[cmdLen];
                        int    len    = _tcpClient.GetStream().Read(buffer, 0, buffer.Length);

                        UsCmd           cmd    = new UsCmd(buffer);
                        UsCmdExecResult result = _cmdParser.Execute(cmd);
                        switch (result)
                        {
                        case UsCmdExecResult.Succ:
                            break;

                        case UsCmdExecResult.Failed:
                            UsLogging.Printf("net cmd execution failed: {0}.", new UsCmd(buffer).ReadNetCmd());
                            break;

                        case UsCmdExecResult.HandlerNotFound:
                            UsLogging.Printf("net unknown cmd: {0}.", new UsCmd(buffer).ReadNetCmd());
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                DisconnectOnError("error detected while receiving data.", ex);
            }
        }
예제 #12
0
 private void OnGuardingTimeout(object sender, EventArgs e)
 {
     UsLogging.Printf(LogWndOpt.Error, "guarding timeout, closing connection...");
     Disconnect();
 }