示例#1
0
        internal void OnRecv(byte[] buf, int grossRecvBytes)
        {
            try
            {
                TotalDataBytesReceived  += buf.Length;
                TotalGrossBytesReceived += grossRecvBytes;

                _serverConnectors.TriggerOnDebugLog(this, DebugLogType.Info, $"OnRecv - start, dataRecvBytes:{buf.Length}, grossRecvBytes:{grossRecvBytes}, TotalDataBytesReceived:{TotalDataBytesReceived}, TotalGrossBytesReceived:{TotalGrossBytesReceived}");

                if (buf[0] == 0) //request response packet
                {
                    object reqPacket = null;
                    int    requestId = 0;
                    string exceptionMsg = null;
                    byte   module = 0, command = 0, requestType = 0;
                    try
                    {
                        requestType = buf[1];

                        _serverConnectors.TriggerOnDebugLog(this, DebugLogType.Info, $"Recv requestType = {requestType}");


                        //if (requestType != ConnectorsUtils.RequestTypeRequestMultiResponses)
                        {
                            reqPacket = ConnectorsUtils.DeserializeRequestPacket(buf, _serverConnectors._settings.PacketsMap, out requestId, out module, out command);
                        }
                    }
                    catch (Exception ex) { exceptionMsg = ex.Message; }


                    if (requestType == ConnectorsUtils.RequestTypeKeepAlive) //keep alive
                    {
                        _lastReceivedKeepAliveTimestamp = (long)reqPacket;
                        _serverConnectors.TriggerOnDebugLog(this, DebugLogType.OnKeepAlive, reqPacket.ToString());
                        return;
                    }

                    if (requestType == ConnectorsUtils.RequestTypeRecvInProgress) //client RecvInProgress (keep alive should be less sensitive)
                    {
                        _lastReceivedInProgressTime = DateTime.UtcNow;
                        _serverConnectors.TriggerOnDebugLog(this, DebugLogType.OnKeepAlive, "Recv in progress");
                        return;
                    }

                    if (requestType == ConnectorsUtils.RequestTypeRequestResponse)
                    {
                        if (exceptionMsg != null)
                        {
                            var resBuf = ConnectorsUtils.SerializeRequestPacket(ConnectorsUtils.RequestTypeRequestResponse, 0, 1, exceptionMsg, requestId);
                            TcpSocketsUtils.Send(Socket, resBuf, OnSend, OnExcp);
                        }
                        else
                        {
                            var rrData = new RequestResponseData()
                            {
                                RequestId = requestId,
                                Module    = module,
                                Command   = command,
                                Packet    = reqPacket,
                            };

                            new Task(() => HandleRequestResponse(rrData)).Start();
                        }
                    }

                    if (requestType == ConnectorsUtils.RequestTypeRequestMultiResponses)
                    {
                        if (exceptionMsg != null)
                        {
                            var resBuf = ConnectorsUtils.SerializeMultiResponsePacket(
                                ConnectorsUtils.RequestTypeRequestMultiResponses, 0, 1, exceptionMsg, requestId,
                                true, 0, 0);
                            TcpSocketsUtils.Send(Socket, resBuf, OnSend, OnExcp);
                        }
                        else
                        {
                            //reqPacket = ConnectorsUtils.DeserializeMultiResponsePacket(
                            //    buf, _serverConnectors._settings.PacketsMap, out requestId,
                            //    out bool isLast, out int nReceived, out int nTotal,
                            //    out module, out command);

                            _serverConnectors.TriggerOnDebugLog(this, DebugLogType.Info, $"Recv RequestTypeRequestResponse requestId = {requestId}");

                            var rrData = new RequestResponseData()
                            {
                                RequestId = requestId,
                                Module    = module,
                                Command   = command,
                                Packet    = reqPacket,
                            };
                            new Task(() => HandleRequestMultiResponses(rrData)).Start();
                        }
                    }
                }
                else //packet
                {
                    var packet = ConnectorsUtils.DeserializePacket(buf, _serverConnectors._settings.PacketsMap, out byte module, out byte command);
                    _serverConnectors.TriggerOnPacket(this, module, command, packet);
                }
            }
            catch (Exception ex)
            {
                _serverConnectors.TriggerOnDebugLog(this, DebugLogType.OnRecvException, ex.ToString());
            }
        }
示例#2
0
        private void OnRecv(byte[] buf, int grossRecvBytes)
        {
            try
            {
                TotalDataBytesReceived  += buf.Length;
                TotalGrossBytesReceived += grossRecvBytes;

                OnDebugLog?.Invoke(DebugLogType.Info, $"OnRecv - start, dataRecvBytes:{buf.Length}, grossRecvBytes:{grossRecvBytes}, TotalDataBytesReceived:{TotalDataBytesReceived}, TotalGrossBytesReceived:{TotalGrossBytesReceived}");

                _lastRecvTime = DateTime.UtcNow;

                // module, command
                if (buf[0] == 0) //request response packet
                {
                    object reqPacket = null;
                    int    requestId = 0;
                    byte   module = 0, command = 0, requestType = 0;
                    try
                    {
                        requestType = buf[1];
                        OnDebugLog?.Invoke(DebugLogType.Info, $"OnRecv - requestType = {requestType}");
                        if (requestType != ConnectorsUtils.RequestTypeRequestMultiResponses)
                        {
                            reqPacket = ConnectorsUtils.DeserializeRequestPacket(buf, _settings.PacketsMap, out requestId, out module, out command);
                        }
                    }
                    catch (Exception ex) { OnDebugLog?.Invoke(DebugLogType.OnRecvException, ex.ToString()); }

                    if (requestType == ConnectorsUtils.RequestTypeKeepAlive) //keep alive
                    {
                        _lastKeepAliveTime = DateTime.UtcNow;
                        TcpSocketsUtils.Send(_socket, buf, OnSend, OnExcp);
                        OnDebugLog?.Invoke(DebugLogType.OnKeepAlive, reqPacket.ToString());
                        return;
                    }

                    if (requestType == ConnectorsUtils.RequestTypeRequestResponse)
                    {
                        if (requestId % 2 == 1)
                        {
                            if (module == 0 && command == 1)
                            {
                                _reqResHandler.HandleExceptionResponse(
                                    requestId,
                                    new Exception("Exception in Server, check inner exception", new Exception((reqPacket ?? "").ToString())));
                            }
                            else
                            {
                                _reqResHandler.HandleResponse(requestId, reqPacket);
                            }
                        }
                        else
                        {
                            if (module == 0 && command == 1)
                            {
                                _reqResAsyncHandler.HandleExceptionResponse(
                                    requestId,
                                    new Exception("Exception in Server, check inner exception", new Exception((reqPacket ?? "").ToString())));
                            }
                            else
                            {
                                _reqResAsyncHandler.HandleResponse(requestId, reqPacket);
                            }
                        }
                    }

                    if (requestType == ConnectorsUtils.RequestTypeRequestMultiResponses)
                    {
                        reqPacket = ConnectorsUtils.DeserializeMultiResponsePacket(
                            buf, _settings.PacketsMap, out requestId,
                            out bool isLast, out int nReceived, out int nTotal,
                            out module, out command);

                        if (module == 0 && command == 1)
                        {
                            _reqMultiResHandler.HandleExceptionResponse(requestId, new Exception((reqPacket ?? "").ToString()));
                        }
                        else
                        {
                            OnDebugLog?.Invoke(DebugLogType.Info, $"OnRecv - ConnectorsUtils.RequestTypeRequestMultiResponses");

                            _reqMultiResHandler.HandleResponse(requestId, reqPacket, isLast, nReceived, nTotal);
                        }
                    }
                }
                else //packet
                {
                    object packet = ConnectorsUtils.DeserializePacket(buf, _settings.PacketsMap, out byte module, out byte command);
                    _packetsQueue.Add(new Tuple <int, int, object>(module, command, packet));
                }
            }
            catch (Exception ex)
            {
                OnDebugLog?.Invoke(DebugLogType.OnRecvException, ex.ToString());
            }
        }