Exemplo n.º 1
0
 public static new ClientInfo decode(ref Lidgren.Network.NetIncomingMessage msg)
 {
     ClientInfo ci = new ClientInfo (Packet.decode (ref msg).ClientId);
     ci.Pseudo = msg.ReadString ();
     ci.Color = new Color(msg.ReadByte(), msg.ReadByte(), msg.ReadByte());
     return ci;
 }
Exemplo n.º 2
0
        public void ReadIncomingMsg()
        {
            NetIncomingMessage msg;
            while ((msg = server.ReadMessage()) != null) {
                switch (msg.MessageType) {
                    case NetIncomingMessageType.VerboseDebugMessage:
                    case NetIncomingMessageType.DebugMessage:
                    case NetIncomingMessageType.WarningMessage:
                    case NetIncomingMessageType.ErrorMessage:
                        Console.WriteLine(msg.ReadString());
                        break;

                    case NetIncomingMessageType.StatusChanged:
                        NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte();
                        if (status == NetConnectionStatus.Connected) {
                            int id = GetUniqueClientId();
                            NetworkClient newClient = new NetworkClient(id, msg.SenderConnection);
                            clientList.Add(msg.SenderConnection, newClient);
                            Console.WriteLine(newClient.Pseudo + " connected");

                            ClientInfo ci = new ClientInfo(id);
                            ci.Pseudo = newClient.Pseudo;
                            SendPkt(ci, msg.SenderConnection, true);

                            Player p = new Player(map, imageManager);
                            map.AddPlayer(p);
                            newClient.Player = p;
                            SendFullWorldUpdate(msg.SenderConnection);
                            SendFullClientInfo(msg.SenderConnection);

                        } else if (status == NetConnectionStatus.Disconnected) {
                            if (clientList.ContainsKey(msg.SenderConnection)) {
                                NetworkClient c = clientList[msg.SenderConnection];
                                clientList.Remove(c.Connection);
                                map.DeletePlayer(c.Player);
                                Console.WriteLine("Client " + c.Pseudo + " disconnected");

                                ClientDisconnect cd = new ClientDisconnect(c.ClientId);
                                SendPktToAll(cd, true);
                            }

                        }
                        break;

                    case NetIncomingMessageType.Data:
                        ReadPacket(msg);
                        break;

                    default:
                        Console.WriteLine("Unhandled type: " + msg.MessageType);
                        break;

                }
                server.Recycle(msg);
            }
        }
Exemplo n.º 3
0
        public void Connect()
        {
            Reset();

            client.Start();
            client.Connect (ip, 55017);

            DateTime start = DateTime.Now;
            while (client.ConnectionStatus == NetConnectionStatus.Disconnected) {
                Thread.Sleep (10);
                if((DateTime.Now - start).TotalSeconds > 20)
                    return;
            }

            Console.WriteLine ("Connecting... Waiting for ID");

            while (clientId == -1) {
                ReadIncomingMsg ();
                Thread.Sleep (10);
            }

            Console.WriteLine ("Client connected");

            NetOutgoingMessage msg = client.CreateMessage ();
            ClientInfo ci = new ClientInfo (clientId);
            ci.Color = player.Color;
            ci.Pseudo = pseudo;
            SendPacket(ci, true);
        }
Exemplo n.º 4
0
 private void SendFullClientInfo(NetConnection client)
 {
     foreach (NetworkClient c in clientList.Values) {
         ClientInfo ci = new ClientInfo(c.ClientId);
         ci.Color = c.Player.Color;
         ci.Pseudo = c.Pseudo;
         SendPkt(ci, client, true);
     }
 }