protected virtual async Task ReceiveFromRemotePeerAsync(RemoteTcpPeer remoteTcpPeer, CancellationToken cancellationToken)
        {
            Defragmentation.ReadFrameResult readFrameResult = null;

            while (!cancellationToken.IsCancellationRequested)
            {
                using (var timeoutCts = this.Config.ConnectionTimeout == TimeSpan.Zero ? new CancellationTokenSource() : new CancellationTokenSource(this.Config.ConnectionTimeout))
                    using (var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(timeoutCts.Token, cancellationToken))
                    {
                        readFrameResult = await remoteTcpPeer.ProtocolFrameDefragmenter
                                          .ReadFrameAsync(remoteTcpPeer, readFrameResult?.LeftOvers, linkedCts.Token)
                                          .ConfigureAwait(false);
                    }

                if (readFrameResult.ReadFrameStatus == Defragmentation.ReadFrameStatus.StreamClosed)
                {
                    remoteTcpPeer.ConnectionCloseReason = ConnectionCloseReason.RemoteShutdown;
                    return;
                }
                else if (readFrameResult.ReadFrameStatus == Defragmentation.ReadFrameStatus.FrameDropped)
                {
                    readFrameResult = null;

                    continue;
                }

                var frameArrivedEventArgs = new TcpFrameArrivedEventArgs(remoteTcpPeer, readFrameResult.FrameData);

                this.OnFrameArrived(remoteTcpPeer, frameArrivedEventArgs);
            }

            remoteTcpPeer.ConnectionCloseReason = ConnectionCloseReason.LocalShutdown;
        }
Beispiel #2
0
        private void OnDataReceived(TcpFrameArrivedEventArgs message)
        {
            var deserialized = _objectSerializerService.Deserialize(message.FrameData);

            switch (deserialized)
            {
            case ISignalingMessage signalingMessage:
            {
                var args = new MessageReceivedEventArgs
                {
                    Message = signalingMessage
                };
                MessageReceived?.Invoke(this, args);
                break;
            }

            case RowInfo rowInfo:
            {
                var args = new RowInfoReceivedEventArgs
                {
                    RowInfo = rowInfo
                };
                RowInfoReceived?.Invoke(this, args);
                break;
            }
            }
        }
Beispiel #3
0
        private void OnDataReceived(TcpFrameArrivedEventArgs message)
        {
            var args = new MessageReceivedEventArgs {
                Message = (ISignalingMessage)_objectSerializerService.Deserialize(message.FrameData)
            };

            MessageReceived?.Invoke(this, args);
        }
Beispiel #4
0
        protected virtual void OnFrameArrived(RemoteTcpPeer remoteTcpPeer, TcpFrameArrivedEventArgs e)
        {
            try
            {
                remoteTcpPeer.OnFrameArrived(e);

                this.FrameArrived?.Invoke(this, e);
            }
            catch (Exception ex)
            {
                var unhandledErrorEventArgs = new UnhandledErrorEventArgs(new ErrorData(ex));

                this.OnUnhandledError(unhandledErrorEventArgs);
            }
        }
Beispiel #5
0
        private void OnDataReceived(TcpFrameArrivedEventArgs message)
        {
            var deserialized = _objectSerializerService.Deserialize(message.FrameData);

            if (deserialized is EonPacket eonPacket)
            {
                var args = new PackageReceivedEventArgs {
                    Packet = eonPacket
                };
                PackageReceived?.Invoke(this, args);
            }
            else if (deserialized is ISignalingMessage signalingMessage)
            {
                var args = new MessageReceivedEventArgs {
                    Message = signalingMessage
                };
                MessageReceived?.Invoke(this, args);
            }
        }
        protected virtual void OnFrameArrived(RemoteTcpPeer remoteTcpPeer, TcpFrameArrivedEventArgs e)
        {
            try
            {
                remoteTcpPeer.OnFrameArrived(e);
            }
            catch (Exception ex)
            {
                var ue = new ExceptionEventArgs(ex);
                this.OnUnhandledException(ue);
            }

            try
            {
                this.FrameArrived?.Invoke(this, e);
            }
            catch (Exception ex)
            {
                var ue = new ExceptionEventArgs(ex);
                this.OnUnhandledException(ue);
            }
        }
Beispiel #7
0
 private void FrameArrived(object sender, TcpFrameArrivedEventArgs e)
 {
     log.Info($"Client received: " + $"{System.Text.Encoding.UTF8.GetString(e.FrameData)}");
 }
        private void ProcessMessage(TcpFrameArrivedEventArgs e, ISignalingMessage signalingMessage)
        {
            string incomingNode;
            int    incomingPort;

            switch (signalingMessage)
            {
            case SNPNegotiation_req req:
            {
                incomingNode = _nodeOfSocket[e.RemoteTcpPeer];
                incomingPort = req.Port;
                var cable = GetCable(incomingNode, incomingPort);


                if (cable == null)
                {
                    return;
                }

                var nextNode = cable.Node2;
                var nextPort = cable.Port2;

                if (nextNode == incomingNode)
                {
                    nextNode = cable.Node1;
                    nextPort = cable.Port1;
                }

                if (cable.Status)
                {
                    req.Port = nextPort;
                    if (!_socketOfNode.ContainsKey(nextNode))
                    {
                        _logService.LogWarning($"{nextNode} is not connected to CableCloud");
                        return;
                    }
                    SendMessage(_socketOfNode[nextNode], signalingMessage);
                    _logService.LogInfo("Sending package from: " + incomingNode + ":" + incomingPort + " to: " + nextNode + ":" + nextPort);
                }
                else
                {
                    _logService.LogInfo("Discarding package (cable disabled) from: " + incomingNode + ":" + incomingPort + " to: " + nextNode + ":" + nextPort);
                }
                break;
            }

            case SNPNegotiation_rsp rsp:
            {
                incomingNode = _nodeOfSocket[e.RemoteTcpPeer];
                incomingPort = rsp.Port;
                var cable = GetCable(incomingNode, incomingPort);


                if (cable == null)
                {
                    return;
                }

                var nextNode = cable.Node2;
                var nextPort = cable.Port2;

                if (nextNode == incomingNode)
                {
                    nextNode = cable.Node1;
                    nextPort = cable.Port1;
                }

                if (cable.Status)
                {
                    rsp.Port = nextPort;
                    if (!_socketOfNode.ContainsKey(nextNode))
                    {
                        _logService.LogWarning($"{nextNode} is not connected to CableCloud");
                        return;
                    }
                    SendMessage(_socketOfNode[nextNode], signalingMessage);
                    _logService.LogInfo("Sending package from: " + incomingNode + ":" + incomingPort + " to: " + nextNode + ":" + nextPort);
                }
                else
                {
                    _logService.LogInfo("Discarding package (cable disabled) from: " + incomingNode + ":" + incomingPort + " to: " + nextNode + ":" + nextPort);
                }
                break;
            }
            }
        }