示例#1
0
            public void Connect(TcpClient _socket)
            {
                socket = _socket;
                socket.ReceiveBufferSize = dataBufferSize;
                socket.SendBufferSize    = dataBufferSize;

                stream = socket.GetStream();

                receiveData   = new Packet();
                receiveBuffer = new byte[dataBufferSize];

                stream.BeginRead(receiveBuffer, 0, dataBufferSize, ReceiveCallback, null);

                ServerSend.Welcome(id, $"Welcome to the server {id}!");
            }
示例#2
0
            public void Connect(TcpClient client)
            {
                socket = client;

                socket.ReceiveBufferSize = dataBufferSize;
                socket.SendBufferSize    = dataBufferSize;

                networkStream  = socket.GetStream();
                receiveBuffer  = new byte[dataBufferSize];
                receivedPacket = new Packet();

                networkStream.BeginRead(receiveBuffer, 0, dataBufferSize, new AsyncCallback(ReceiveCallback), null);

                // TODO: send welcome message to client! (done)
                ServerSend.Welcome(id, "Welcome to server!");
            }
示例#3
0
        public static void Message(int from, Packet packet)
        {
            string msg = packet.ReadString();

            if (string.IsNullOrWhiteSpace(msg))
            {
                return;
            }
            msg = msg.Trim();
            Console.Write(from);
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.Write($">{Server.clients[from].name}=-> ");
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine(msg);
            ServerSend.Text(from, msg);
        }
        public void SpawnPlayers()
        {
            player = new Player(id, new Vector3(0f, 0f, 0f));

            foreach (Client _client in Server.clients.Values)
            {
                if (_client.player != null && _client.id != id)
                {
                    ServerSend.TCP_SpawnPlayer(id, _client.player);
                }
            }
            foreach (Client _client in Server.clients.Values)
            {
                if (_client.player != null)
                {
                    ServerSend.TCP_SpawnPlayer(_client.id, player);
                }
            }
        }
示例#5
0
        public void CreateCube(Vector3 position)
        {
            if (Cube != null)
            {
                Console.WriteLine($"Cube {Cube.id} should be destroyed");
                ServerSend.DestroyCube(0, Cube);
            }

            Cube = new Cube(ID, position);

            //foreach (Client client in Server.Clients.Values)
            //{
            //    if (client.player != null && client.ID != ID)
            //        ServerSend.CreateCube(client.Cube);
            //}

            Console.WriteLine($"Created Cube {Cube.id}");
            ServerSend.CreateCube(Cube);
        }
示例#6
0
 public void SendIntoGame(string _playerName)
 {
     player = new Player(id, _playerName, new Vector3(0, 0, 0));
     foreach (Client _client in MainServer.clients.Values)
     {
         if (_client.player != null)
         {
             if (_client.id != id)
             {
                 ServerSend.SpawnPlayer(id, _client.player);
             }
         }
     }
     foreach (Client _client in MainServer.clients.Values)
     {
         if (_client.player != null)
         {
             ServerSend.SpawnPlayer(_client.id, player);
         }
     }
 }
示例#7
0
        public void SendIntoGame(string playerName)
        {
            clientPlayer = new Player(id, playerName, Vector3.Zero);

            foreach (Client client in Server.clients.Values)
            {
                if (client.clientPlayer != null && client.id != id)
                {
                    // Send every spawned player information to the newly connected client
                    ServerSend.SpawnPlayer(id, client.clientPlayer);
                }
            }

            foreach (Client client in Server.clients.Values)
            {
                if (client.clientPlayer != null)
                {
                    // Send the newly connected clients data to every connected client, including the newly connected client too so that it can receive its own spawn information!
                    ServerSend.SpawnPlayer(client.id, clientPlayer);
                }
            }
        }
示例#8
0
        public void SendIntoGame(string playerName)
        {
            player = new Player(ID, playerName, Vector3.Zero);

            foreach (Client client in Server.Clients.Values)
            {
                if (client.player != null && client.ID != ID)
                {
                    ServerSend.SpawnPlayer(ID, client.player);
                    if (client.Cube != null)
                    {
                        ServerSend.CreateCube(ID, client.Cube);
                    }
                }
            }

            foreach (Client client in Server.Clients.Values)
            {
                if (client.player != null)
                {
                    ServerSend.SpawnPlayer(client.ID, player);
                }
            }
        }
示例#9
0
 private void Connect()
 {
     ServerSend.Welcome(id, "Welcome to the server!");
 }
        /// <summary>Receives incoming UDP data.</summary>
        public static void UDPReceiveCallback(IAsyncResult _result)
        {
            try
            {
                IPEndPoint clientEndPoint = new IPEndPoint(IPAddress.Any, 0);
                byte[]     data           = udpListener.EndReceive(_result, ref clientEndPoint);
                udpListener.BeginReceive(UDPReceiveCallback, null);

                if (data.Length < 4)
                {
                    return;
                }

                //Handle received Data
                using (Packet _packet = new Packet(data))
                {
                    int clientId = _packet.ReadInt();

                    //// If this is a new connection
                    if (clientId == 0)
                    {
                        for (int i = 1; i < users.Count; i++)
                        {
                            if (users[i].udp.endPoint == null)
                            {
                                users[i].udp.Connect(clientEndPoint);
                                users[i].name = _packet.ReadString();
                                clientId      = i;
                                break;
                            }
                        }

                        //using (Packet _pack = new Packet((int)ServerPackets.welcome))
                        //{
                        //    //you have to catch these info at client on the same order
                        //    _pack.Write(clientId);
                        //    _pack.Write("Welcome");
                        //    _pack.WriteLength();


                        //    UDPServer.SendUDPData(clientEndPoint, _pack);

                        //}

                        //sent to own client a Welcome message
                        ServerSend.Welcome(clientId);

                        return;
                    }


                    if (users[clientId].udp.endPoint.ToString() == clientEndPoint.ToString())
                    {
                        users[clientId].udp.HandleData(_packet);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error receiving UDP data: {ex}");
            }
        }