예제 #1
0
        public void Update()
        {
            if (_tcpClient == null)
            {
                return;
            }

            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!");
                        }
                        _cmdExec.Execute(new UsCmd(buffer));
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.LogException(ex);
                CloseTcpClient();
            }
        }
예제 #2
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);
            }
        }