Example #1
0
        /// <summary>
        /// This method is called by the Server_Awaiting_Client method when a new client connection needs to be made. Note : This method
        /// is also part of the even loop described in the Server_Awating_Client method.
        /// </summary>
        /// <param name="result">The IAsyncResult object that holds the state object that was passed from the previous method.</param>
        public static void Accept_a_New_Client(IAsyncResult result)
        {
            // Write to the window to show the player that a new client has been accepted.
            Console.WriteLine("Accepting a new Client....");

            // Convert the state object back into a PreservedState object.
            PreservedState state = (PreservedState)result.AsyncState;

            // Pull out the socket listener.
            Socket listener = state.socket;

            // Create a new socket for the client.
            Socket clienthandler = listener.EndAccept(result);


            // We must pass a new state object into the Begin Accept...
            listener.BeginAccept(Accept_a_New_Client, new PreservedState(listener, state.callBack));
            // This fails:
            //listener.BeginAccept(Accept_a_New_Client, state);

            // Update the state object to hold the new client socket instead of the listener.
            state.socket = clienthandler;

            // Call the callback function stored in the state obj.
            state.callBack(state);
        }
Example #2
0
        /// <summary>
        /// This method is a callback function to the Send method. It ensures all the data that needs to be sent is sent in the correct order. And it ends when there is no more data
        /// to me sent to the server.
        /// </summary>
        /// <param name="result"></param>
        public static void SendCallBack(IAsyncResult result)
        {
            // The number of bytes that were sent.
            int bytes;

            // Pull the Preserved state obj out of the arObject.
            PreservedState state = (PreservedState)result.AsyncState;

            // Store the socket
            Socket socket = state.socket;

            if (socket.Connected)
            {
                // Retrieve the number of bytes sent
                try
                {
                    bytes = socket.EndSend(result);
                }
                catch (Exception e)
                {
                    socket.Close();
                    return;
                }
            }
            else
            {
                return;
            }

            // Lock this method so multiple threads can be sending data at the same time.
            lock (locker)
            {
                // store the PreservedState buffer into a outgoing buffer.
                byte[] outgoingBuffer = state.buffer;

                // Convert all the buffer data into a string.
                String data = encoding.GetString(outgoingBuffer, bytes, outgoingBuffer.Length - bytes);

                // Iif the string is empty end this send callback
                if (data == "")
                {
                    // If the optional callback was provided in Send(), run it. Else, just return
                    if (state.callBack != null)
                    {
                        state.callBack(null);
                    }
                    else
                    {
                        return;
                    }
                }

                // If there is more data to send call Send again.
                Send(socket, data);
            }
        }
Example #3
0
        /// <summary>
        /// This method is a callback from the Connect_To_Server method, it calls the callback stored in the PreservedState obj and then returns control back to Connect_To_Server
        /// </summary>
        /// <param name="arObject"></param>
        public static void Connected_to_Server(IAsyncResult arObject)
        {
            // Pull the Preserved state obj out of the arObject.
            PreservedState state = (PreservedState)arObject.AsyncState;

            // Store the socket
            Socket socket = state.socket;

            // Call the callback function and pass in the state obj.
            state.callBack(state);
        }
Example #4
0
        /// <summary>
        /// This method is called when more data is wanted from the server. It is the callback of the I want more data. It requests more data from the server and if more information
        /// was given it adds it to the PreservedState obj. If nothing is recieved from the server the socket is closed.
        /// </summary>
        /// <param name="arObject"></param>
        public static void ReceiveCallBack(IAsyncResult arObject)
        {
            try
            {
                // Pull the Preserved state obj out of the arObject.
                PreservedState state = (PreservedState)arObject.AsyncState;

                // Store the socket
                Socket socket = state.socket;

                if (socket.Connected == false)
                {
                    return;
                }

                // Keep track of the number of bytes read from the server.
                int bytesRead = socket.EndReceive(arObject);

                // If at least one byte is read in, decode it and append it to the string builder in the PreservedState object.
                if (bytesRead > 0)
                {
                    // Store the decoded string bytes into the string builder
                    state.strBuilder.Append(Encoding.UTF8.GetString(state.buffer, 0, bytesRead));

                    // Call the callback to allow that function to know about the newly recieved information.
                    state.callBack(state);
                }
                // If no bytes were read in, close down the socket connection.
                else
                {
                    socket.Shutdown(SocketShutdown.Both);
                    socket.Close();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }