Exemple #1
0
        /// <summary>
        /// Creates and runs a <seealso cref="Server"/>.
        /// </summary>
        /// <param name="_maxPlayers">The maximum number of clients that can connect to the server.</param>
        /// <param name="_port">The port to run on.</param>
        public static void Start(int _maxPlayers, int _port)
        {
            maxPlayers = _maxPlayers;
            port       = _port;

            LogMan.Initialize();
            LogMan.Info($"Copyright (c) {DateTime.Now.Year} Gideon Grinberg. Subject to the terms of the Mozilla PublicLicense, v. 2.0.");
            LogMan.Info($"Starting server.");

            try
            {
                tcpListener = new TcpListener(IPAddress.Any, port);

                tcpListener.Start();
                tcpListener.BeginAcceptTcpClient(new AsyncCallback(TCPConnectCallback), null);

                LogMan.Info($"Listening for connections on port {port}. Max players is currently set to {_maxPlayers}");
            }
            catch (Exception ex)
            {
                LogMan.Info($"ERROR: Failed to start TCP server. Exception: \n {ex}");
            }

            InitializeServerData();
            LogMan.Info("Finished initializing server!");
        }
Exemple #2
0
        /// <summary>
        /// Connects a new client to the server.
        /// </summary>
        private static void TCPConnectCallback(IAsyncResult _result)
        {
            TcpClient _client = tcpListener.EndAcceptTcpClient(_result);

            tcpListener.BeginAcceptTcpClient(new AsyncCallback(TCPConnectCallback), null);

            LogMan.Info($"Incoming connection from {_client.Client.RemoteEndPoint}");
            for (int i = 1; i <= maxPlayers; i++)
            {
                if (clients[i].tcp.socket == null)
                {
                    clients[i].tcp.Connect(_client);
                    LogMan.Info($"Connected client {clients[i].tcp.socket.Client.RemoteEndPoint}. Client ID: {i}");
                    return;
                }
            }

            LogMan.Info($"Failed to connect client. Server is full. (Players: {maxPlayers}/{maxPlayers} connections used).");
        }
Exemple #3
0
            /// <summary>
            /// Read TCP data from stream.
            /// </summary>
            private void ReceiveCallback(IAsyncResult _result)
            {
                try
                {
                    int _byteLength = stream.EndRead(_result);

                    if (_byteLength <= 0)
                    {
                        // TODO: disconnect client.
                        LogMan.Info("Received 0 bytes of data from client.");
                    }

                    byte[] _data = new byte[_byteLength];
                    Array.Copy(receiveBuffer, _data, dataBufferSize);

                    // TODO: handle data

                    stream.BeginRead(receiveBuffer, 0, dataBufferSize, ReceiveCallback, null);
                }
                catch (Exception ex)
                {
                    LogMan.Info($"\u001b[31m Error receiving TCP data: \u001b[0m {ex}");
                }
            }