예제 #1
0
        private void ProcessConnect(SocketAsyncEventArgs e)
        {
            AsyncToken token = (AsyncToken)e.UserToken;

            if (!token.Socket.ConnectAsync(e))
            {
                NetworkOnConnect(null, e);
            }
        }
예제 #2
0
        private void ProcessAccept(SocketAsyncEventArgs e)
        {
            AsyncToken token = (AsyncToken)e.UserToken;

            e.AcceptSocket = null;

            if (!token.Socket.AcceptAsync(e))
            {
                ThreadPool.QueueUserWorkItem(DispatchAccept, e);
            }
        }
예제 #3
0
        private void NetworkOnAccept(object sender, SocketAsyncEventArgs e)
        {
            AsyncToken token = (AsyncToken)e.UserToken;

            Socket socket = e.AcceptSocket;

            ProcessAccept(e); // Start the next accept asap.

            if (socket == null)
            {
                return; // Ignore errors because there is nothing to do
            }

            AsyncState state = new AsyncState(this, socket, AsyncOperation.Accept, token.Interface, token.User); // Now handle the current connection.

            bool result = false;

            try
            {
                result = state.Context.Interface.OnConnect(state.Context);
            }
            catch (Exception) { }
            if (!result)
            {
                try
                {
                    state.Context.Interface.OnError(state.Context, token.User); // Ensure the user can cleanup anything before the object dies
                }
                catch (Exception) { }

                state.Cleanup(); // Cleanup the socket

                return;
            }

            try
            {
                state.Read(); // Begin receiving data on the socket
            }
            catch (Exception)
            {
                state.Cleanup(); // Cleanup the object
                return;
            }

            AddState(state); // Store the state to keep it alive
        }
예제 #4
0
        private void NetworkOnConnect(object sender, SocketAsyncEventArgs e)
        {
            AsyncToken token = (AsyncToken)e.UserToken;

            Socket socket = e.ConnectSocket;

            if (socket == null)                            // The connection failed
            {
                token.Interface.OnError(null, token.User); // There is no state object yet, so pass the user object
                return;
            }

            AsyncState state = new AsyncState(this, socket, AsyncOperation.Connect, token.Interface, token.User); // Now handle the current connection.

            bool result = false;

            try
            {
                result = state.Context.Interface.OnConnect(state.Context);
            }
            catch (Exception) { }

            if (!result)
            {
                try
                {
                    state.Context.Interface.OnError(state.Context, token.User); // Ensure the user can cleanup anything before the object dies
                }
                catch (Exception) { }

                state.Cleanup(); // Cleanup the object
                return;
            }

            try
            {
                state.Read(); // Begin receiving data on the socket
            }
            catch (Exception)
            {
                state.Cleanup(); // Cleanup the object
                return;
            }

            AddState(state); // Store the state to keep it alive
        }