Beispiel #1
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);
            }
        }
Beispiel #2
0
 public void SendCommand(UsCmd cmd)
 {
     if (!IsSendAvaiable())
     {
         return;
     }
     byte[] cmdLenBytes = BitConverter.GetBytes((int)cmd.WrittenLen);
     if (_tcpClient.GetStream().CanWrite)
     {
         try
         {
             _tcpClient.GetStream().Write(cmdLenBytes, 0, cmdLenBytes.Length);
             _tcpClient.GetStream().Write(cmd.Buffer, 0, cmd.WrittenLen);
         }
         catch (Exception ex)
         {
             Debug.LogException(ex);
             CloseTcpClient();
         }
     }
     //Debug.Log(string.Format("B cmd len = {0} size = {1})", cmd.WrittenLen, cmdLenBytes.Length));
 }
        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);
        }
        public void SendVarTracerCmd()
        {
            if (!HasNewVarTracerCmd())
            {
                return;
            }
            int newSize     = CalculateCmdSize();
            int currentSize = _uscmd.Buffer.Length;
            int preSize     = currentSize;

            while (newSize > currentSize)
            {
                currentSize *= 2;
            }

            if (currentSize > preSize)
            {
                _uscmd = new UsCmd(new byte[currentSize]);
            }

            writeNetCmd(_uscmd);

            _cmdCacher.Clear();
        }
        private void writeNetCmd(UsCmd uscmd)
        {
            uscmd.ClearCmd();
            uscmd.WriteNetCmd(eNetCmd.SV_VarTracerInfo);
            //group count
            uscmd.WriteInt32(_cmdCacher.GetUsedGroupCount());
            foreach (var package in _cmdCacher.GroupCmdPackage)
            {
                if (package.Value.IsUse())
                {
                    //group name
                    uscmd.WriteString(package.Key);

                    //variable count
                    uscmd.WriteInt32(_cmdCacher.GetUsedVariableCount(package.Value));
                    foreach (var list in package.Value.VariableDict)
                    {
                        if (list.Value.IsUse())
                        {
                            //variable name
                            uscmd.WriteString(list.Key);
                            //session count
                            uscmd.WriteInt32(list.Value.UseIndex);
                            var cacheList = list.Value;
                            int usedCount = cacheList.UseIndex;
                            for (int i = 0; i < usedCount; i++)
                            {
                                //UnityEngine.Debug.LogFormat("send stamp= {0}", list.Value.VarChacheList[i]._stamp);
                                //stamp
                                uscmd.WriteLong(list.Value.VarChacheList[i]._stamp);
                                //value
                                uscmd.WriteFloat(list.Value.VarChacheList[i]._value);
                            }
                        }
                    }

                    //event count
                    uscmd.WriteInt32(_cmdCacher.GetUsedEventCount(package.Value));
                    foreach (var list in package.Value.EventDict)
                    {
                        if (list.Value.IsUse())
                        {
                            //event name
                            uscmd.WriteString(list.Key);
                            //session count
                            uscmd.WriteInt32(list.Value.UseIndex);
                            var cacheList = list.Value;
                            int usedCount = cacheList.UseIndex;
                            for (int i = 0; i < usedCount; i++)
                            {
                                //stamp
                                uscmd.WriteLong(list.Value.VarChacheList[i]._stamp);
                                //duration
                                uscmd.WriteFloat(list.Value.VarChacheList[i]._duration);
                            }
                        }
                    }
                }
            }

            UsNet.Instance.SendCommand(uscmd);
        }
 public void Send(UsCmd cmd)
 {
     _client.SendPacket(cmd);
 }
 private bool Handle_KeepAliveResponse(eNetCmd cmd, UsCmd c)
 {
     //NetUtil.Log("'KeepAlive' received.");
     return(true);
 }