예제 #1
0
        private void DisconnectOnError(string info, Exception ex)
        {
            NetUtil.Log(info);
            NetUtil.Log(ex.ToString());

            Disconnect();
        }
예제 #2
0
        public void Tick_CheckConnectionStatus()
        {
            try
            {
                if (!_tcpClient.Connected)
                {
                    NetUtil.Log("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)
                    {
                        NetUtil.Log("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 void Connect(string host, int port)
 {
     _host      = host;
     _port      = port;
     _tcpClient = new TcpClient();
     _tcpClient.BeginConnect(_host, _port, OnConnect, _tcpClient);
     NetUtil.Log("connecting to [u]{0}:{1}[/u]...", host, port);
 }
예제 #4
0
        private bool Handle_ExecCommandResponse(eNetCmd cmd, UsCmd c)
        {
            int code = c.ReadInt32();

            NetUtil.Log("command executing result: [b]{0}[/b]", code);

            return(true);
        }
예제 #5
0
        private bool Handle_HandshakeResponse(eNetCmd cmd, UsCmd c)
        {
            NetUtil.Log("eNetCmd.SV_HandshakeResponse received, connection validated.");

            SysPost.InvokeMulticast(this, LogicallyConnected);

            _guardTimer.Deactivate();
            return(true);
        }
예제 #6
0
        public void Disconnect()
        {
            if (_tcpClient != null)
            {
                _tcpClient.Close();
                _tcpClient = null;

                _host = "";
                _port = 0;

                NetUtil.Log("connection closed.");
                SysPost.InvokeMulticast(this, Disconnected);
            }
        }
예제 #7
0
        public void Tick_ReceivingData()
        {
            try
            {
                while (_tcpClient.Available > 0)
                {
                    byte[] cmdLenBuf  = new byte[VarTracerConst.ByteSize_Int];
                    int    cmdLenRead = _tcpClient.GetStream().Read(cmdLenBuf, 0, cmdLenBuf.Length);
                    int    cmdLen     = BitConverter.ToInt32(cmdLenBuf, 0);
                    if (cmdLenRead > 0 && cmdLen > 0)
                    {
                        byte[] buffer = new byte[cmdLen];
                        if (!NetUtil.ReadStreamData(_tcpClient, ref buffer))
                        {
                            throw new Exception("Read Stream Data Error!");
                        }

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

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

                        case UsCmdExecResult.HandlerNotFound:
                            NetUtil.Log("net unknown cmd: {0}.", new UsCmd(buffer).ReadNetCmd());
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                DisconnectOnError("error detected while receiving data.", ex);
            }
        }
예제 #8
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
                {
                    NetUtil.Log("connected successfully.");
                    SysPost.InvokeMulticast(this, Connected);
                }
                else
                {
                    throw new Exception();
                }
            }
            catch (Exception ex)
            {
                DisconnectOnError("connection failed while handling OnConnect().", ex);
            }
        }
예제 #9
0
        public void ExecuteCmd(string cmdText)
        {
            if (!IsConnected)
            {
                NetUtil.Log("not connected to server, command ignored.");
                return;
            }

            if (cmdText.Length == 0)
            {
                NetUtil.Log("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);

            NetUtil.Log("command executed: [b]{0}[/b]", cmdText);
        }