예제 #1
0
        // Completion callback for AcceptAsync
        private void OnAccept(SocketAsyncEventArgs e)
        {
            if (e.SocketError == SocketError.Success)
            {
                Diag.IncrementConnectionCount();

                var clientSocket = e.AcceptSocket;

                // Adjust client socket options.
                clientSocket.NoDelay = NoDelay;

                Log.Info("{0} {1} accepted from {2}",
                    Name, clientSocket.Handle, clientSocket.RemoteEndPoint);

                var session = new AsyncTcpLinkSession(this, clientSocket);

                if (BufferTransform != null)
                {
                    session.BufferTransform = (IBufferTransform)BufferTransform.Clone();
                }

                Flow.Publish(new LinkSessionConnected {
                    LinkName = Name,
                    Result = true,
                    Context = session
                });

                session.BeginReceive(true);

                AcceptImpl();
            }
            else
            {
                if (e.SocketError == SocketError.OperationAborted)
                {
                    // Listening socket has been closed.
                }
                else
                {
                    Log.Error("{0} accept error {1}", Name, e.SocketError);
                }
            }
        }
예제 #2
0
        // Completion callback for ConnectAsync
        private void OnConnect(SocketAsyncEventArgs e)
        {
            var noti = new LinkSessionConnected { LinkName = Name };

            if (e.SocketError == SocketError.Success)
            {
                // Adjust socket options.
                socket.NoDelay = NoDelay;

                Log.Info("{0} {1} connected to {2}", Name, socket.Handle, socket.RemoteEndPoint);

                noti.Result = true;

                ConnectInternal();

                connectEventArgs = null;

                session = new AsyncTcpLinkSession(this, socket);
                session.Polarity = true;

                if (BufferTransform != null)
                {
                    session.BufferTransform = BufferTransform;
                }

                noti.Context = session;
                Flow.Publish(noti);

                session.BeginReceive(true);
            }
            else
            {
                Log.Warn("{0} connect error {1}", Name, e.SocketError);

                noti.Context = e.RemoteEndPoint;
                Flow.Publish(noti);

                RetryInternal(e.RemoteEndPoint);
            }
        }