예제 #1
0
        /*
         * Run the client.
         * Starts server connection and packet sending.
         */
        public void Run()
        {
            SetUpServerConnection();
            Console.WriteLine("Connected to server!");

            //Start listening to input on secondary thread.
            Thread inputThread = new Thread(new ThreadStart(HandleInput));

            inputThread.Start();

            //Accept packets from server in async.
            _ = AcceptServerPacketAsync(_tcpClient, _cancellationTokenSource.Token);

            Console.WriteLine("Starting packet sending..");

            //At this point we have connected. Let's send some messages until we aren't.
            while (_tcpClient.Connected)
            {
                //Go through all PacketsToSend and send them.
                Packet        packet;
                NetworkStream stream = _tcpClient.GetStream();

                while (_packetsToSend.TryDequeue(out packet))
                {
                    //Convert packet to byte array.
                    byte[] packetBuffer = PacketConverter.CreatePacketByteArray(packet.PacketType, packet.Data);
                    //Write to stream.
                    stream.Write(packetBuffer, 0, packetBuffer.Length);
                }

                while (_packetsRecieved.TryDequeue(out packet))
                {
                    //Grab packet handler if it exists.
                    HandlePacketDelegate packetDelegate;
                    if (_packetHandlers.TryGetValue(packet.PacketType, out packetDelegate))
                    {
                        packetDelegate.Invoke(packet);
                    }
                }

                Thread.Sleep(50);
            }

            Console.WriteLine("Disconnected from server. Press ENTER to exit.");
            Console.ReadLine();
        }
예제 #2
0
        /*
         * Run Server.
         * Starts networking and main loop.
         */
        public void Run()
        {
            Console.WriteLine("Server has started.");

            //Accept new clients in async.
            _ = AcceptConnectionsAsync();

            while (_isRunning)
            {
                Packet packet;

                //Handle recieved packets.
                while (_packetsRecieved.TryDequeue(out packet))
                {
                    //Grab packet handler if it exists.
                    List <HandlePacketDelegate> packetDelegate;
                    if (_packetHandlers.TryGetValue(packet.PacketType, out packetDelegate))
                    {
                        foreach (HandlePacketDelegate pDel in packetDelegate)
                        {
                            pDel.Invoke(packet);
                        }
                    }
                }

                //Send packets we have queued up to send.
                while (_packetsToSend.TryDequeue(out packet))
                {
                    foreach (TcpClient tcpClient in _connectedClients)
                    {
                        byte[] packetByteArray = PacketConverter.CreatePacketByteArray(packet.PacketType, packet.Data);
                        tcpClient.GetStream().Write(packetByteArray, 0, packetByteArray.Length);
                    }
                }

                Thread.Sleep(50);
            }

            CloseServer();
        }