Beispiel #1
0
        /// <summary>
        /// To be used as the callback for accepting a new client that was initiated by StartServer, and
        /// continues an event-loop to accept additional clients.
        ///
        /// Uses EndAcceptSocket to finalize the connection and create a new SocketState. The SocketState's
        /// OnNetworkAction should be set to the delegate that was passed to StartServer.
        /// Then invokes the OnNetworkAction delegate with the new SocketState so the user can take action.
        ///
        /// If anything goes wrong during the connection process (such as the server being stopped externally),
        /// the OnNetworkAction delegate should be invoked with a new SocketState with its ErrorOccured flag set to true
        /// and an appropriate message placed in its ErrorMessage field. The event-loop should not continue if
        /// an error occurs.
        ///
        /// If an error does not occur, after invoking OnNetworkAction with the new SocketState, an event-loop to accept
        /// new clients should be continued by calling BeginAcceptSocket again with this method as the callback.
        /// </summary>
        /// <param name="ar">The object asynchronously passed via BeginAcceptSocket. It must contain a tuple with
        /// 1) a delegate so the user can take action (a SocketState Action), and 2) the TcpListener</param>
        private static void AcceptNewClient(IAsyncResult ar)
        {
            //initializes the variables used in this method
            SocketState          socketState = null;
            Action <SocketState> action      = null;
            TcpListener          listener    = null;
            Socket socket = null;

            try
            {
                //Creates a tuple from the given parameter and sets the listener and action to the given items
                Tuple <Action <SocketState>, TcpListener> tuplePassed = (Tuple <Action <SocketState>, TcpListener>)ar.AsyncState;
                action   = tuplePassed.Item1;
                listener = tuplePassed.Item2;

                socket = listener.EndAcceptSocket(ar);
                //creates a new socketstate with the socketstate and the socket from the tuple
                socketState = new SocketState(action, socket);
            }
            //Catches any Exception Thrown and creates a new scoket state to nofify the user
            catch (Exception e)
            {
                socketState = new SocketState(action, socket);
                socketState.ErrorOccured = true;
                socketState.ErrorMessage = e.Message;
                action(socketState);
                return;
            }

            socketState.OnNetworkAction(socketState);
            try
            {
                //starts accepting new clients from the listener inside of the tuple
                Tuple <Action <SocketState>, TcpListener> tuple = new Tuple <Action <SocketState>, TcpListener>(action, listener);
                listener.BeginAcceptSocket(AcceptNewClient, tuple);
            }
            catch (Exception e)
            {
            }
        }