Пример #1
0
        /// <summary>
        /// This function is "called" by the operating system when data is available on the socket
        /// </summary>
        /// <param name="ar"></param>
        private static void ReceiveCallback(IAsyncResult ar)
        {
            SocketState state = (SocketState)ar.AsyncState;

            try
            {
                int bytesRead = state.theSocket.EndReceive(ar);

                // If the socket is still open
                if (bytesRead > 0)
                {
                    string theMessage = Encoding.UTF8.GetString(state.messageBuffer, 0, bytesRead);

                    // Append the received data to the growable buffer.
                    // It may be an incomplete message, so we need to start building it up piece by piece
                    state.sBuilder.Append(theMessage);

                    // We can't process the message directly, because different users of this library might have different
                    // processing needs.
                    // ProcessMessage(state);

                    // Instead, just invoke the client's delegate, so it can take whatever action it desires.
                    state.callMe(state);
                }
            }
            catch (Exception)
            {
                state.hasError = true;
                state.callMe(state);
            }
        }
Пример #2
0
        /// <summary>
        /// This function is "called" by the operating system when data arrives on the socket
        /// Move this function to a standalone networking library.
        /// </summary>
        /// <param name="stateAsArObject"></param>
        private static void ReceiveCallback(IAsyncResult stateAsArObject)
        {
            SocketState ss        = (SocketState)stateAsArObject.AsyncState;
            int         bytesRead = 0;

            //this gets the number of bytes passed in by the socket
            try
            {
                bytesRead = ss.theSocket.EndReceive(stateAsArObject);
            }
            catch (Exception e)
            {
                ss.theSocket.Shutdown(SocketShutdown.Both);
            }

            // If the socket is still open
            if (bytesRead > 0)
            {
                string theMessage = Encoding.UTF8.GetString(ss.messageBuffer, 0, bytesRead);
                // Append the received data to the growable buffer.
                // It may be an incomplete message, so we need to start building it up piece by piece
                ss.sb.Append(theMessage);
                //we call on our call me to finish the handshake to start using that data retreived
                ss.callMe(ss);
            }

            // Continue the "event loop" that was started on line 100.
            // Start listening for more parts of a message, or more new messages
            //  ss.theSocket.BeginReceive(ss.messageBuffer, 0, ss.messageBuffer.Length, SocketFlags.None, ReceiveCallback, ss);
        }
Пример #3
0
        /// <summary>
        /// This method is invoked on its own thread when data arrives from the server.
        /// </summary>
        /// <param name="ar"></param>
        private static void ReceiveCallback(IAsyncResult ar)
        {
            // Get the SocketState representing the connection on which data was received
            SocketState ss = (SocketState)ar.AsyncState;

            int numBytes = ss.theSocket.EndReceive(ar);

            // Convert the raw bytes to a string
            if (numBytes > 0)
            {
                string message = Encoding.UTF8.GetString(ss.messageBuffer, 0, numBytes);

                lock (ss.sb)
                {
                    ss.sb.Append(message);
                }
            }
            else
            {
                // Close the socket if no data is received
                ss.theSocket.Close();
            }

            ss.callMe(ss);
        }
Пример #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="ar"></param>
        public static void AcceptNewClient(IAsyncResult ar)
        {
            ConnectionState cs = (ConnectionState)ar.AsyncState;
            Socket          s  = cs.listener.EndAcceptSocket(ar);
            SocketState     ss = new SocketState(s, -1);

            ss.theSocket = s;
            ss.callMe    = cs.callMe;
            ss.callMe(ss);
            cs.listener.BeginAcceptSocket(AcceptNewClient, cs);
        }
Пример #5
0
        /// <summary>
        /// Method called when a client attempts to connect to the server
        /// </summary>
        /// <param name="ar"></param>
        public static void AcceptNewClient(IAsyncResult ar)
        {
            // Get the current connection state
            ConnectionState cs     = (ConnectionState)ar.AsyncState;
            Socket          socket = cs.listener.EndAcceptSocket(ar);
            // Create a new socket state for the client
            SocketState ss = new SocketState(socket, cs.callMe);

            // Send the new socketstate to the server
            ss.callMe(ss);
            // Open the server to allow other clients to connect to it
            cs.listener.BeginAcceptSocket(AcceptNewClient, cs);
        }
Пример #6
0
        /// <summary>
        /// This is the callback that BeginAcceptSocket to use.
        /// This code will be invoked by the OS when a connection request comes in.
        /// </summary>
        public static void AcceptNewClient(IAsyncResult ar)
        {
            ConnectionState conState = (ConnectionState)ar.AsyncState;

            // set up new client
            Socket      socket      = conState.listener.EndAcceptSocket(ar);
            SocketState socketState = new SocketState(socket, -1);

            socketState.callMe = conState.callMe;
            socketState.callMe(socketState);

            // start accepting other clients
            conState.listener.BeginAcceptSocket(AcceptNewClient, conState);
        }
Пример #7
0
        /// <summary>
        /// Adds a new client connection to our server
        /// </summary>
        /// <param name="ar"></param>
        public static void AcceptNewClient(IAsyncResult ar)
        {
            ConnectionState connecState = (ConnectionState)ar.AsyncState;
            Socket          socket      = connecState.listener.EndAcceptSocket(ar);
            SocketState     ss          = new SocketState();

            //setting up its socket
            ss.theSocket = socket;
            //setting up its event delegate
            ss.callMe = connecState.callMe;
            ss.callMe(ss);
            //starting accepting data from the client
            connecState.listener.BeginAcceptSocket(AcceptNewClient, connecState);
        }
        ///The ReceiveCallback method is called by the OS when new data arrives.
        ///This method should check to see how much data has arrived.
        ///If 0, the connection has been closed (presumably by the server).
        ///On greater than zero data, this method should call the callback function provided above.
        public static void RecieveCallback(IAsyncResult state_in_an_ar_object)
        {
            stateToPass = (SocketState)state_in_an_ar_object.AsyncState;

            //error might throw?

            int bytesRead = stateToPass.theSocket.EndReceive(state_in_an_ar_object);

            if (bytesRead > 0)
            {
                string theMessage = Encoding.UTF8.GetString(stateToPass.messageBuffer, 0, bytesRead);
                stateToPass.sb.Append(theMessage);
                stateToPass.callMe(stateToPass);
            }
        }
Пример #9
0
        /// <summary>
        /// This method is automatically invoked on its own thread when a connection is made.
        /// </summary>
        /// <param name="ar"></param>
        private static void ConnectedCallback(IAsyncResult ar)
        {
            Console.WriteLine("contact from server");

            // Get the SocketState associated with this connection
            SocketState ss = (SocketState)ar.AsyncState;

            // This is required to complete the "handshake" with the server. Both parties agree a connection is made.
            // Only EndConnect if the socket is truly connected
            if (ss.theSocket.Connected)
            {
                ss.theSocket.EndConnect(ar);
            }

            ss.callMe(ss);
        }
Пример #10
0
        /// <summary>
        /// This function is "called" by the operating system when the remote site acknowledges the connect request
        /// Move this function to a standalone networking library.
        /// </summary>
        /// <param name="ar"></param>
        private static void ConnectedCallback(IAsyncResult stateAsArObject)
        {
            SocketState ss = (SocketState)stateAsArObject.AsyncState;

            try
            {
                // Complete the connection.
                ss.theSocket.EndConnect(stateAsArObject);
                ss.callMe(ss);
            }
            catch (Exception e)
            {
                networkException();
                //System.Diagnostics.Debug.WriteLine("Unable to connect to server. Error occured: " + e);
                return;
            }
        }
Пример #11
0
        // callback method used to connect to the server
        public static void ConnectedCallback(IAsyncResult stateAsArObject)
        {
            //connect to the server
            SocketState state = (SocketState)stateAsArObject.AsyncState;

            try
            {
                // Complete the connection.
                state.theSocket.EndConnect(stateAsArObject);
                state.callMe(state);
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Unable to connect to server. Error occured: " + e);
                return;
            }
        }
Пример #12
0
        public static void AcceptNewClient(IAsyncResult ar)
        {
            NewConnectionState cs = (NewConnectionState)ar.AsyncState;

            socket = cs.listener.EndAcceptSocket(ar);

            SocketState ss = new SocketState(socket, AssignID());

            ss.theSocket = socket;

            ss.callMe = cs.callMe;

            ss.callMe(ss);

            Console.WriteLine("Connection Made!");

            cs.listener.BeginAcceptSocket(AcceptNewClient, cs);
        }
        /// <summary>
        /// This function is referenced by the BeginConnect method above and is "called" by the OS when the socket
        /// connects to the server. The "state_in_an_ar_object" object contains a field "AsyncState" which contains
        /// the "state" object saved away in the above function.
        //  Once a connection is established the "saved away" callbackFunction needs to called.
        /// This function is saved in the socket state, and was originally passed in to ConnectToServer.
        /// </summary>
        /// <param name="state_in_an_ar_object"></param>
        private static void ConnectedToServer(IAsyncResult ar)
        {
            stateToPass = (SocketState)ar.AsyncState;

            try
            {
                // Complete the connection.
                stateToPass.theSocket.EndConnect(ar);
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Unable to connect to server. Error occured: " + e);
                return;
            }
            stateToPass.callMe(stateToPass);
            // TODO: If we had a "EventProcessor" delagate stored in the state, we could call that,
            //       instead of hard-coding a method to call.
            stateToPass.theSocket.BeginReceive(stateToPass.messageBuffer, 0, stateToPass.messageBuffer.Length, SocketFlags.None, RecieveCallback, stateToPass);
        }
Пример #14
0
        /// <summary>
        /// This function is "called" by the operating system when the remote site acknowledges
        /// connect request.
        /// </summary>
        /// <param name="ar"></param>
        private static void ConnectedCallback(IAsyncResult ar)
        {
            // Make a new state for the data
            SocketState state = (SocketState)ar.AsyncState;

            try
            {
                // Complete the connection.
                state.theSocket.EndConnect(ar);
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Unable to connect to server. Error occured: " + e);
                return;
            }

            // Invoke the client's delegate so it can take whatever action it desires
            state.callMe(state);
        }
Пример #15
0
        /// <summary>
        /// This function is "called" by the operating system when the remote site acknowledges connect request
        /// </summary>
        /// <param name="ar"></param>
        private static void ConnectedCallback(IAsyncResult ar)
        {
            SocketState state = (SocketState)ar.AsyncState;

            try
            {
                // Complete the connection.
                state.theSocket.EndConnect(ar);
            }
            catch (Exception e)
            {
                state.hasError = true;
                throw e; // TODO take this out?
            }

            // Don't start an event loop to receive data from the server. The client might not want to do that.
            // state.theSocket.BeginReceive(state.messageBuffer, 0, state.messageBuffer.Length, SocketFlags.None, ReceiveCallback, state);
            // Instead, just invoke the client's delegate so it can take whatever action it desires.
            state.callMe(state);
        }
Пример #16
0
        /// <summary>
        /// This function is "called" by the operating system when the remote site acknowledges connect request
        /// </summary>
        /// <param name="ar"></param>
        private static void ConnectedCallback(IAsyncResult ar)
        {
            SocketState state = (SocketState)ar.AsyncState;

            try
            {
                // Complete the connection.
                state.theSocket.EndConnect(ar);
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Unable to connect to server. Error occured: " + e);
                return;
            }

            // Don't start an event loop to receive data from the server. The client might not want to do that.
            //state.theSocket.BeginReceive(state.messageBuffer, 0, state.messageBuffer.Length, SocketFlags.None, ReceiveCallback, state);

            // Instead, just invoke the client's delegate so it can take whatever action it desires.
            state.callMe(state);
        }
Пример #17
0
        /// <summary>
        /// callback to end receiving
        /// </summary>
        /// <param name="stateAsArObject"></param>
        public static void ReceiveCallback(IAsyncResult stateAsArObject)
        {
            try
            {
                SocketState state = (SocketState)stateAsArObject.AsyncState;

                int bytesRead = state.theSocket.EndReceive(stateAsArObject);

                // If the socket is still open
                if (bytesRead > 0)
                {
                    string theMessage = Encoding.UTF8.GetString(state.messageBuffer, 0, bytesRead);
                    // Append the received data to the growable buffer.
                    // It may be an incomplete message, so we need to start building it up piece by piece
                    state.sb.Append(theMessage);
                    state.callMe(state);
                }
            }catch (Exception e)
            {
            }
        }
Пример #18
0
        /// <summary>
        /// This function is "called" by the operating system when data is available on the socket.
        /// </summary>
        /// <param name="ar"></param>
        private static void ReceiveCallback(IAsyncResult ar)
        {
            SocketState state     = (SocketState)ar.AsyncState;
            int         bytesRead = 0;

            try
            {
                bytesRead = state.theSocket.EndReceive(ar);
            }
            catch { }

            // If the socket is still open
            if (bytesRead > 0)
            {
                string theMessage = Encoding.UTF8.GetString(state.messageBuffer, 0, bytesRead);
                // Append the received data to the growable buffer.
                // It may be an incomplete message, so we need to start building it up piece by piece
                state.sb.Append(theMessage);

                // Invoke the client's delegate, so it can take whatever action it desires.
                state.callMe(state);
            }
        }