示例#1
0
        /// <summary>
        /// Ran when the server receives some data from a client.
        /// </summary>
        /// <param name="asyncResult"></param>
        private void ReceiveDataCallback(IAsyncResult asyncResult)
        {
            try
            {
                // This socket is the same socket as in AcceptCallback
                ReloadedSocket clientSocket = (ReloadedSocket)asyncResult.AsyncState;

                // Gets the length of data that has been received.
                int bytesReceived = ClientSocket.Socket.EndReceive(asyncResult);
                clientSocket.AsyncReceivedBytes += bytesReceived;

                // Close if we are disconnected if 0 bytes received
                if (bytesReceived == 0)
                {
                    clientSocket.CloseIfDisconnected();
                    return;
                }

                // If we have not received all of the bytes yet.
                if (clientSocket.AsyncReceivedBytes < clientSocket.AsyncBytesToReceive)
                {
                    // The size of data to receive is the size of a message header.
                    ClientSocket.Socket.BeginReceive(ClientSocket.ReceiveBuffer, clientSocket.AsyncReceivedBytes,
                                                     clientSocket.AsyncBytesToReceive - clientSocket.AsyncReceivedBytes, SocketFlags.None,
                                                     ReceiveDataCallback, clientSocket);
                    return;
                }

                // Create the buffer for the received data.
                byte[] dataBuffer = new byte[clientSocket.AsyncBytesToReceive];

                // Copy the received data.
                Array.Copy(ClientSocket.ReceiveBuffer, dataBuffer, clientSocket.AsyncReceivedBytes);

                // Send the buffer to the subscribing method!
                ProcessBytesMethods(ParseMessage(dataBuffer), clientSocket);

                // Re-set bytes received.
                clientSocket.AsyncReceivedBytes = 0;

                // Reset buffer size (old buffer will be Garbage Collected in due time).
                if (clientSocket.ReceiveBuffer.Length > clientSocket.DefaultBufferSize)
                {
                    clientSocket.ReceiveBuffer = new byte[clientSocket.DefaultBufferSize];
                }

                // Accept connections again!
                // Header Length!
                ClientSocket.Socket.BeginReceive(ClientSocket.ReceiveBuffer, 0, sizeof(UInt32), SocketFlags.None,
                                                 ReceiveSizeCallback, clientSocket);
            }
            // Server was closed.
            catch (Exception ex)
            {
                Bindings.PrintWarning?.Invoke("[libReloaded] Exception thrown while receiving packet data, the host probably closed the connection | " + ex.Message);
            }
        }
示例#2
0
        /// <summary>
        /// Ran when the server receives some data from a client.
        /// </summary>
        /// <param name="asyncResult"></param>
        private void ReceiveSizeCallback(IAsyncResult asyncResult)
        {
            try
            {
                // This socket is the same socket as in AcceptCallback
                ReloadedSocket clientSocket = (ReloadedSocket)asyncResult.AsyncState;

                // Set expected data to be received to 4. (Message length)
                clientSocket.AsyncBytesToReceive = sizeof(UInt32);

                // Gets the length of data that has been received.
                // Increment Received Bytes
                int bytesReceived = ClientSocket.Socket.EndReceive(asyncResult);
                clientSocket.AsyncReceivedBytes += bytesReceived;

                // Close if we are disconnected if 0 bytes received
                if (bytesReceived == 0)
                {
                    clientSocket.CloseIfDisconnected();
                    return;
                }

                // If we have not received all of the bytes yet.
                if (clientSocket.AsyncReceivedBytes < clientSocket.AsyncBytesToReceive)
                {
                    // The size of data to receive is the size of a message header.
                    ClientSocket.Socket.BeginReceive(ClientSocket.ReceiveBuffer, clientSocket.AsyncReceivedBytes,
                                                     clientSocket.AsyncBytesToReceive - clientSocket.AsyncReceivedBytes, SocketFlags.None,
                                                     ReceiveSizeCallback, clientSocket);
                    return;
                }

                // Get the true length of the message to be received.
                clientSocket.AsyncBytesToReceive = BitConverter.ToInt32(ClientSocket.ReceiveBuffer, 0);
                clientSocket.AsyncReceivedBytes  = 0;

                // Increase buffer size if necessary.
                if (clientSocket.AsyncBytesToReceive > clientSocket.ReceiveBuffer.Length)
                {
                    clientSocket.ReceiveBuffer = new byte[clientSocket.AsyncBytesToReceive];
                }

                // The size of data to receive is the size of a message header.
                ClientSocket.Socket.BeginReceive(ClientSocket.ReceiveBuffer, clientSocket.AsyncReceivedBytes,
                                                 clientSocket.AsyncBytesToReceive - clientSocket.AsyncReceivedBytes, SocketFlags.None,
                                                 ReceiveDataCallback, clientSocket);
            }
            // Server was closed.
            catch (Exception ex)
            {
                Bindings.PrintWarning?.Invoke("[libReloaded] Exception thrown while receiving packet size header, the host probably closed the connection | " + ex.Message);
            }
        }