Example #1
0
        public Server(int port)
        {
            _serverThread = new Thread(() =>
            {
                _stopping = false;
                ClientIdsManager idsManager = new ClientIdsManager();
                Debug.Log("[Server Thread] Hi.");

                TcpListener listener = TcpListener.Create(27015);

                listener.Start();
                onServerReady?.Invoke();

                while (!_stopping)
                {
                    while (listener.Pending())
                    {
                        Debug.Log("[Server Thread] Accepting new client.");
                        TcpClient tcpListener = listener.AcceptTcpClient();

                        try
                        {
                            byte newClientId    = idsManager.GetAvailableId();
                            ServerClient client = new ServerClient(newClientId, tcpListener);
                            _clients.Add(newClientId, client);
                            Packet clientId = new Packet(PacketType.GIVE_CLIENT_ID);
                            clientId.Write(newClientId);
                            SendPacketToClient(clientId, client);
                        }
                        catch (Exception e)
                        {
                            Debug.Log("[Server thread] an exception occured.");
                            Debug.Log(e.Message);
                            Debug.Log(e.Source);
                            Debug.Log(e.StackTrace);
                        }
                    }

                    foreach (KeyValuePair <byte, ServerClient> kvp in _clients)
                    {
                        ServerClient client  = kvp.Value;
                        NetworkStream stream = client.NetworkStream;

                        lock (client.packetsToSend)
                        {
                            while (stream.CanWrite && client.packetsToSend.Count > 0)
                            {
                                client.packetsToSend.Dequeue().SendToStream(stream);
                            }
                        }

                        while (stream.CanRead && stream.DataAvailable)
                        {
                            Packet packet = Packet.ReadFrom(stream);
                            if (packet.SenderClientId != client.Id)
                            {
                                Debug.Log("A client sent a packet as someone else.");
                                stream.Close();
                                stream.Dispose();
                                client.Tcp.Close();
                                client.Tcp.Dispose();
                            }
                            else
                            {
                                NetworkManager.Instance.ServerReceivePacket(packet);
                            }
                        }
                    }

                    Thread.Sleep(1);
                }

                listener.Stop();
                _stopping = false;
            })
            {
                IsBackground = true,
            };

            _serverThread.Start();
        }
Example #2
0
        public Server(int port)
        {
            _serverThread = new Thread(() =>
            {
                _stopping = false;
                Debug.Log("[Server Thread] Hi.");

                TcpListener listener = new TcpListener(IPAddress.Any, port);

                listener.Start();
                onServerReady?.Invoke();
                while (!_stopping)
                {
                    while (listener.Pending())
                    {
                        Debug.Log("[Server Thread] Accepting new client.");
                        TcpClient tcpListener = listener.AcceptTcpClient();

                        try
                        {
                            byte newClientId    = ClientIdsManager.GetAvailableId();
                            ServerClient client = new ServerClient(newClientId, tcpListener);
                            _clientsList.Add(client);
                            SendPacketToClient(PacketType.GIVE_CLIENT_ID, client, new byte[] { newClientId });
                        }
                        catch (Exception e)
                        {
                            Debug.Log(e.Message);
                            Debug.Log(e.Source);
                            Debug.Log(e.StackTrace);
                        }
                    }

                    _clientsList.RemoveAll(client => client == null);

                    foreach (ServerClient client in _clientsList)
                    {
                        NetworkStream stream = client.NetworkStream;

                        while (stream.CanWrite && client.packetsToSend.Count > 0)
                        {
                            client.packetsToSend.Dequeue().SendToStream(stream);
                        }

                        while (stream.CanRead && stream.DataAvailable)
                        {
                            Packet packet = Packet.ReadFrom(stream);
                            if (packet.SenderClientId != client.Id)
                            {
                                Debug.Log("A client sent a packet as someone else.");
                                stream.Close();
                                stream.Dispose();
                                client.Tcp.Close();
                                client.Tcp.Dispose();
                            }
                            else
                            {
                                NetworkManager.Singleton.ServerReceivePacket(packet);
                            }
                        }
                    }

                    Thread.Sleep(7);
                }

                listener.Stop();
                _stopping = false;
            })
            {
                IsBackground = true,
            };

            _serverThread.Start();
        }