Esempio n. 1
0
        private void OnFrameRead(EstablishedConnectionInfo info, IFrame frame)
        {
            var h = FrameRead;

            if (h != null)
            {
                h.Invoke(this, new FrameReadEventArgs(info, frame));
            }
        }
Esempio n. 2
0
        private void OnReadError(EstablishedConnectionInfo info, ErrorCode error, Exception ex)
        {
            var h = ReadError;

            if (h != null)
            {
                h.Invoke(this, new ReadErrorEventArgs(info, error, ex));
            }
        }
Esempio n. 3
0
        private void OnDisconnect(EstablishedConnectionInfo info)
        {
            connections.Remove(info.Id);
            var h = Disconnected;

            if (h != null)
            {
                h.Invoke(this, new DisconnectEventArgs(info));
            }
        }
Esempio n. 4
0
        public bool TryFindConnection(string remoteHost, int remotePort, out EstablishedConnectionInfo eci)
        {
            var conn = connections.Values.FirstOrDefault(x => x.Info.RemoteHost == remoteHost && x.Info.RemotePort == remotePort);

            if (conn != null)
            {
                eci = conn.Info;
                return(true);
            }

            eci = EstablishedConnectionInfo.Empty;
            return(false);
        }
        public EstablishedConnection(
            EstablishedConnectionInfo eci,
            Socket socket,
            Action <EstablishedConnectionInfo> onDisconnect,
            Action <EstablishedConnectionInfo, ErrorCode, Exception> onError,
            Action <EstablishedConnectionInfo, IFrame> onFrameRead,
            EstablishedConnectionOptions options,
            IClock clock)
        {
            Info              = eci.IsEmpty ? throw new ArgumentException("Connection info must not be empty", nameof(eci)) : eci;
            this.socket       = socket ?? throw new ArgumentNullException(nameof(socket));
            this.onDisconnect = onDisconnect ?? throw new ArgumentNullException(nameof(onDisconnect));
            this.onError      = onError ?? throw new ArgumentNullException(nameof(onError));
            this.onFrameRead  = onFrameRead ?? throw new ArgumentNullException(nameof(onFrameRead));
            this.options      = options ?? throw new ArgumentNullException(nameof(options));
            this.clock        = clock ?? throw new ArgumentNullException(nameof(clock));
            networkStream     = new NetworkStream(socket);
            cts   = new CancellationTokenSource();
            pipe  = new Pipe(new PipeOptions(minimumSegmentSize: options.SocketReadMinBufferSize));
            State = EstablishedConnectionState.Opened;
            Stats = new EstablishedConnectionStats();

            listenTask = ListenForIncomingFrames();
        }
Esempio n. 6
0
        private async Task AcceptConnectionsAsync()
        {
            while (!cts.IsCancellationRequested)
            {
                try
                {
                    var acceptSocket = await listenSocket.AcceptAsync().ConfigureAwait(false);

                    if (acceptSocket != null)
                    {
                        var id     = Guid.NewGuid();
                        var remote = (IPEndPoint)acceptSocket.RemoteEndPoint;
                        var eci    = new EstablishedConnectionInfo(id, options.Host, options.Port, remote.Address.ToString(), remote.Port);
                        var clock  = new AdjustedClock();
                        var conn   = new EstablishedConnection(eci, acceptSocket, OnDisconnect, OnReadError, OnFrameRead, options, clock);
                        connections.Add(id, conn);
                    }
                }
                catch (Exception ex)
                {
                    OnReadError(EstablishedConnectionInfo.Empty, ErrorCode.AcceptSocket, ex);
                }
            }
        }
Esempio n. 7
0
 public FrameReadEventArgs(EstablishedConnectionInfo connectionInfo, IFrame frame)
 {
     ConnectionInfo = connectionInfo;
     Frame          = frame;
 }
Esempio n. 8
0
 public ReadErrorEventArgs(EstablishedConnectionInfo connectionInfo, ErrorCode error, Exception ex)
 {
     ConnectionInfo = connectionInfo;
     Error          = error;
     Exception      = ex;
 }
 public DisconnectEventArgs(EstablishedConnectionInfo connectionInfo)
 {
     ConnectionInfo = connectionInfo;
 }