コード例 #1
0
ファイル: SockBase.cs プロジェクト: lanicon/SocketApp
        // 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
ファイル: SockBase.cs プロジェクト: lanicon/SocketApp
        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
ファイル: SockBase.cs プロジェクト: lanicon/SocketApp
 public SocketAcceptEventArgs(AcceptStateObject state, SockBase handler)
 {
     State = state; Handler = handler;
 }