Пример #1
0
        public object SendRequest(int module, int command, object packet)
        {
            if (IsConnected == false)
            {
                throw new InvalidOperationException("Connector not connected");
            }
            var requestId = _nextRequestId += 2;

            byte[] output = ConnectorsUtils.SerializeRequestPacket(ConnectorsUtils.RequestTypeRequestResponse, module, command, packet, requestId);
            var    res    = _reqResHandler.Request(requestId, () => { TcpSocketsUtils.Send(_socket, output, OnSend, OnExcp); });

            return(res);
        }
Пример #2
0
        public void SendRequestMultiResponses(int module, int command, object packet, RequestMultiResponsesClientCallback responseCallback)
        {
            if (IsConnected == false)
            {
                throw new InvalidOperationException("Connector not connected");
            }
            var requestId = _nextRequestId += 2;

            byte[] output = ConnectorsUtils.SerializeRequestPacket(ConnectorsUtils.RequestTypeRequestMultiResponses, module, command, packet, requestId);

            _reqMultiResHandler.Request(
                requestId, () => { TcpSocketsUtils.Send(_socket, output, OnSend, OnExcp); },
                responseCallback);//,                responseType);
        }
Пример #3
0
 private void HandleRequestResponse(RequestResponseData rrData)
 {
     try
     {
         var res    = _serverConnectors.TriggerOnRequestPacket(this, rrData.Module, rrData.Command, rrData.Packet);
         var resBuf = ConnectorsUtils.SerializeRequestPacket(ConnectorsUtils.RequestTypeRequestResponse, rrData.Module, rrData.Command, res, rrData.RequestId);
         TcpSocketsUtils.Send(Socket, resBuf, OnSend, OnExcp);
     }
     catch (Exception ex)
     {
         _serverConnectors.TriggerOnDebugLog(this, DebugLogType.OnRecvException, $"Module={rrData.Module} Command={rrData.Command} ex={ex.ToString()}");
         var resBuf = ConnectorsUtils.SerializeRequestPacket(ConnectorsUtils.RequestTypeRequestResponse, 0, 1, ex.Message, rrData.RequestId);
         TcpSocketsUtils.Send(Socket, resBuf, OnSend, OnExcp);
     }
 }
Пример #4
0
        private void ReconnectTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (_isDisposed)
            {
                return;
            }

            var isRecvActive = (DateTime.UtcNow - _lastRecvProgressTime).TotalSeconds < 10 || (DateTime.UtcNow - _lastRecvTime).TotalSeconds < 10;

            if (isRecvActive)
            {
                var recvInProgressBuf = ConnectorsUtils.SerializeRequestPacket(ConnectorsUtils.RequestTypeRecvInProgress, 0, 0, 0, 0);
                TcpSocketsUtils.Send(_socket, recvInProgressBuf, OnSend, OnExcp);
            }


            if ((DateTime.UtcNow - _lastKeepAliveTime).TotalSeconds > _settings.KeepAliveDisconnectInterval)
            {
                if (isRecvActive == false)
                {
                    if (_socket != null && IsConnected)
                    {
                        OnDebugLog?.Invoke(DebugLogType.OnKeepAlive, "Need to Disconnect");
                        IsConnected = false;
                        try { _socket.Dispose(); } catch { }
                        _socket = null;
                        OnDisconnect?.Invoke();
                    }
                }
                else
                {
                    OnDebugLog?.Invoke(DebugLogType.OnKeepAlive, "Need to Disconnect, but RecvProgressTime/RecvTime is in less than 10 seconds");
                }
            }

            if (_socket == null || _socket.Connected == false) //todo: check also keep alive status
            {
                DisconnectInternal();
                if (_settings.AutoReconnect)
                {
                    ConnectInternal();
                }
            }
        }
Пример #5
0
        private void KeepAliveTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            _keepAliveTimestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
            foreach (var connector in _contextMap.Values)
            {
                bool needToDisconnect = true;
                //check new context
                if ((DateTime.UtcNow - connector._connectedTime).TotalSeconds < _settings.KeepAliveGraceInterval)
                {
                    needToDisconnect = false;
                }

                //_lastReceivedLeepAliveTimestamp less than 30 seconds, and if ReceivedInProgress give x6 time
                var keepAliveDisconnectInterval = _settings.KeepAliveDisconnectInterval;
                if ((DateTime.UtcNow - connector._lastReceivedInProgressTime).TotalSeconds < 20)
                {
                    keepAliveDisconnectInterval *= 6;
                }

                if ((_keepAliveTimestamp - connector._lastReceivedKeepAliveTimestamp) < keepAliveDisconnectInterval)
                {
                    needToDisconnect = false;
                }


                if (needToDisconnect)
                {
                    OnDebugLog?.Invoke(connector, DebugLogType.OnKeepAlive, "Need to Disconnect");
                    try { connector.Socket.Close(); } catch { }
                    connector.OnExcp(null);
                    continue;
                }

                //send new keep alive
                var keepaliveBuf = ConnectorsUtils.SerializeRequestPacket(ConnectorsUtils.RequestTypeKeepAlive, 0, 0, _keepAliveTimestamp, 0);
                TcpSocketsUtils.Send(connector.Socket, keepaliveBuf, connector.OnSend, connector.OnExcp);
            }
        }
Пример #6
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());
            }
        }