Example #1
0
        private void ReceiveBufferCallback(IAsyncResult result)
        {
            try
            {
                //Gets the length of the data packet from connection.
                int readBytes = ClientNetworkStream.EndRead(result);

                //If we are receiving nothing then the connection is closed.
                if (readBytes <= 0)
                {
                    //Properly close the connection from the server
                    CloseConnection();
                    return;
                }
                //resizing the byte array with the length of the received data.
                byte[] newBytesRead = new byte[readBytes];
                //copying the packet information with the received length to a new array 'newBytesRead'.
                Buffer.BlockCopy(_clientReceiveBuffer, Constants.NETWORK_STREAM_OFFSET, newBytesRead, Constants.NETWORK_STREAM_OFFSET, readBytes);

                //checking which 'packet' we got.
                ServerHandleData.HandleDataFromClient(ConnectionId, newBytesRead);
                //Start listening to connections stream, to allow getting other data over the network.
                ClientNetworkStream.BeginRead(_clientReceiveBuffer, Constants.NETWORK_STREAM_OFFSET, Socket.ReceiveBufferSize, ReceiveBufferCallback, null);
            }
            catch (Exception e)
            {
                //Properly close connection to the server
                CloseConnection();
                Text.WriteLine($"Error occured in ReceiveBufferCallback with message {e.Message}", TextType.ERROR);
            }
        }
Example #2
0
        public static void InitServer()
        {
            Text.WriteLine("Loading server ...", TextType.DEBUG);

            int start = GetTickCount();

            InitClients();
            ServerHandleData.InitPacketsFromClient();
            ServerTcp.InitServer();

            int end = GetTickCount();

            Text.WriteLine("Server loaded in {0} ms", TextType.DEBUG, end - start);
        }