Пример #1
0
        // async Accept
        public void StartAccept(SocketAcceptEventHandler externalCallback = null, object externalCallbackState = null)
        {
            AcceptStateObject state = new AcceptStateObject();

            state.workSocket            = _socket;
            state.externalCallback      = externalCallback;
            state.externalCallbackState = externalCallbackState;
            _socket.BeginAccept(
                new System.AsyncCallback(AcceptCallback), state);
        }
Пример #2
0
        private void AcceptCallback(IAsyncResult ar)
        {
            try
            {
                AcceptStateObject state = (AcceptStateObject)ar.AsyncState;
                // Get the socket that handles the client request.
                Socket listener = state.workSocket;
                Socket handler  = listener.EndAccept(ar);

                SockBase sockBase = new SockBase(handler, SocketRole.Client, true);
                sockBase.IsConnected = true;
                sockBase.StartReceive();

                SocketAcceptEvent?.Invoke(this, new SocketAcceptEventArgs(state, sockBase));
                if (state.externalCallback != null)
                {
                    state.externalCallback(this, new SocketAcceptEventArgs(state, sockBase));
                }

                listener.BeginAccept(
                    new System.AsyncCallback(AcceptCallback), state);
            }
            catch (ObjectDisposedException) { }  // listener closed
        }
Пример #3
0
 public SocketAcceptEventArgs(AcceptStateObject state, SockBase handler)
 {
     State = state; Handler = handler;
 }