Esempio n. 1
0
 /// <summary>
 /// 当读取出错时
 /// </summary>
 protected virtual void OnReadError(Exception exception)
 {
     LastException = exception;
     LogUtils.Log("[ReadError]{0}: {1}", DateTime.Now, ExceptionUtils.GetExceptionString(exception));
     InitQpPackageHandler_Stream(null);
 }
Esempio n. 2
0
        private void sendPackage(Func <byte[], Tuple <int, byte[]> > getPackagePayloadFunc, Action afterSendHandler = null)
        {
            var stream = QpPackageHandler_Stream;

            if (stream == null)
            {
                return;
            }

            lock (this)
            {
                var ret = getPackagePayloadFunc(sendBuffer);
                var packageTotalLength = ret.Item1;
                if (packageTotalLength < PACKAGE_HEAD_LENGTH)
                {
                    throw new IOException($"包大小[{packageTotalLength}]小于包头长度[{PACKAGE_HEAD_LENGTH}]");
                }

                byte[] packageBuffer = ret.Item2;

                //构造包头
                writePackageTotalLengthToBuffer(packageBuffer, 0, packageTotalLength);
                try
                {
                    //如果包缓存是发送缓存
                    if (packageBuffer == sendBuffer)
                    {
                        sendPackageBuffer(stream,
                                          new ArraySegment <byte>(packageBuffer, 0, packageTotalLength),
                                          afterSendHandler
                                          );
                    }
                    //否则,拆分为多个包发送
                    else
                    {
                        if (LogUtils.LogSplit)
                        {
                            LogUtils.Log("{0}: [Send-SplitPackage]Length:{1}", DateTime.Now, packageTotalLength);
                        }

                        //每个包内容的最大长度为对方缓存大小减包头大小
                        var maxTakeLength = BufferSize - PACKAGE_HEAD_LENGTH;
                        var currentIndex  = 0;
                        while (currentIndex < packageTotalLength)
                        {
                            var restLength = packageTotalLength - currentIndex;
                            int takeLength = 0;
                            if (restLength >= maxTakeLength)
                            {
                                takeLength = maxTakeLength;
                            }
                            else
                            {
                                takeLength = restLength;
                            }
                            //构造包头
                            writePackageTotalLengthToBuffer(sendBuffer, 0, PACKAGE_HEAD_LENGTH + takeLength);
                            sendBuffer[4] = (byte)QpPackageType.Split;
                            //复制包体
                            Array.Copy(packageBuffer, currentIndex, sendBuffer, PACKAGE_HEAD_LENGTH, takeLength);
                            //发送
                            sendPackageBuffer(
                                stream,
                                new ArraySegment <byte>(sendBuffer, 0, PACKAGE_HEAD_LENGTH + takeLength),
                                afterSendHandler);
                            currentIndex += takeLength;
                        }
                    }
                    lastSendPackageTime = DateTime.Now;
                }
                catch (Exception ex)
                {
                    LastException = ex;
                    LogUtils.Log("[SendPackage]" + ExceptionUtils.GetExceptionString(ex));
                    throw ex;
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 接收到命令请求数据包时
        /// </summary>
        /// <param name="commandId"></param>
        /// <param name="typeName"></param>
        /// <param name="content"></param>
        private void OnCommandRequestReceived(string commandId, string typeName, string content)
        {
            var eventArgs = new RawCommandRequestPackageReceivedEventArgs()
            {
                CommandId = commandId,
                TypeName  = typeName,
                Content   = content
            };

            RawCommandRequestPackageReceived?.Invoke(this, eventArgs);
            //如果已经处理,则直接返回
            if (eventArgs.Handled)
            {
                return;
            }

            try
            {
                //如果在字典中未找到此类型名称,则直接返回
                if (!commandRequestTypeDict.ContainsKey(typeName))
                {
                    throw new CommandException(255, $"Unknown RequestType: {typeName}.");
                }

                var cmdRequestType  = commandRequestTypeDict[typeName];
                var cmdResponseType = commandRequestTypeResponseTypeDict[cmdRequestType];

                var contentModel = JsonConvert.DeserializeObject(content, cmdRequestType);
                CommandRequestPackageReceived?.Invoke(this, new CommandRequestPackageReceivedEventArgs()
                {
                    CommandId    = commandId,
                    TypeName     = typeName,
                    ContentModel = contentModel
                });

                var hasCommandExecuter = false;
                if (options.CommandExecuterManagerList != null)
                {
                    foreach (var commandExecuterManager in options.CommandExecuterManagerList)
                    {
                        if (commandExecuterManager.CanExecuteCommand(typeName))
                        {
                            hasCommandExecuter = true;
                            var responseModel = commandExecuterManager.ExecuteCommand(this, typeName, contentModel);
                            SendCommandResponsePackage(commandId, 0, null, cmdResponseType.FullName, JsonConvert.SerializeObject(responseModel));
                            break;
                        }
                    }
                }
                if (!hasCommandExecuter)
                {
                    throw new CommandException(255, $"No CommandExecuter for RequestType:{typeName}");
                }
            }
            catch (CommandException ex)
            {
                string errorMessage = ExceptionUtils.GetExceptionString(ex);
                SendCommandResponsePackage(commandId, ex.Code, errorMessage, null, null);
            }
            catch (Exception ex)
            {
                string errorMessage = ExceptionUtils.GetExceptionString(ex);
                SendCommandResponsePackage(commandId, 255, errorMessage, null, null);
            }
        }