コード例 #1
0
        private void RequestMultiResponsesCallback(
            ServerConnectorContext serverConnectorContext, int module, int command, int requestId,
            object packet, bool isLast, int nReceived, int nTotal, Exception exception)
        {
            Console.WriteLine($"*********** RequestMultiResponsesCallback ************ requestId = {requestId}");

            var resBuf = ConnectorsUtils.SerializeMultiResponsePacket(
                ConnectorsUtils.RequestTypeRequestMultiResponses, module, command, packet, requestId, isLast, nReceived, nTotal);

            TcpSocketsUtils.Send(Socket, resBuf, OnSend, OnExcp);
        }
コード例 #2
0
 private void HandleRequestMultiResponses(RequestResponseData rrData)
 {
     try
     {
         _serverConnectors.TriggerOnRequestMultiResponsesPacket(this, rrData.Module, rrData.Command, rrData.RequestId, rrData.Packet, RequestMultiResponsesCallback);
     }
     catch (Exception ex)
     {
         _serverConnectors.TriggerOnDebugLog(this, DebugLogType.OnRecvException, $"Module={rrData.Module} Command={rrData.Command} ex={ex.ToString()}");
         var resBuf = ConnectorsUtils.SerializeMultiResponsePacket(ConnectorsUtils.RequestTypeRequestMultiResponses, 0, 1, ex.Message, rrData.RequestId, true, 0, 0);
         TcpSocketsUtils.Send(Socket, resBuf, OnSend, OnExcp);
     }
 }
コード例 #3
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());
            }
        }