Пример #1
0
        private static bool OnProcessHappyLogin(NetServer server, int id, NetIncomingMessage msg)
        {
            var r = new Msg_HappyPlayerLogin();

            r.R(msg);

            uint uid     = HappyGameId;
            int  x       = RandomMaker.Next(10);
            int  y       = RandomMaker.Next(10);
            uint sid     = (uint)RandomMaker.Next(83);
            var  selfMsg = new Msg_HappyPlayerLoginRsp();

            selfMsg.Uid      = uid;
            selfMsg.X        = x;
            selfMsg.Y        = y;
            selfMsg.SpriteId = sid;
            SendMessage(server, selfMsg, msg.SenderConnection);

            // 更新链接对应的ID
            HappyConnMgr.Modify(msg.SenderConnection, uid);

            var playerPackMsg = new Msg_HappyPlayerPack();

            // 为新玩家推送旧玩家信息
            var playerList     = HappyPlayerMgr.ToList();
            var playerPackList =
                from f
                in playerList
                select Tuple.Create <uint, int, int, bool, uint>(f.Key, f.Value.X, f.Value.Y, f.Value.IsAlive, f.Value.SpriteId);

            playerPackMsg.PlayerPack = playerPackList.ToList();
            SendMessage(server, playerPackMsg, msg.SenderConnection);

            // 为旧玩家推送新玩家信息
            var otherMsg = new Msg_HappyOtherPlayerLogin();

            otherMsg.Uid      = uid;
            otherMsg.X        = x;
            otherMsg.Y        = y;
            otherMsg.SpriteId = sid;
            SendMessageExceptOne(server, otherMsg, msg.SenderConnection);

            // 添加新玩家到玩家管理中
            var player = new HappyPlayer();

            player.X        = x;
            player.Y        = y;
            player.IsAlive  = true;
            player.SpriteId = sid;
            HappyPlayerMgr.Add(uid, player);

            return(true);
        }
Пример #2
0
        static void Main(string[] args)
        {
            OnAgarServerProgerss();
            OnHappyServerProgerss();

            while (!Console.KeyAvailable || Console.ReadKey().Key != ConsoleKey.Escape)
            {
                NetIncomingMessage msg;
                while ((msg = AgarServer.ReadMessage()) != null)
                {
                    switch (msg.MessageType)
                    {
                    case NetIncomingMessageType.DiscoveryRequest:
                        AgarServer.SendDiscoveryResponse(null, msg.SenderEndPoint);
                        break;

                    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)
                        {
                            Console.WriteLine(NetUtility.ToHexString(msg.SenderConnection.RemoteUniqueIdentifier) + " connected!");
                            Console.WriteLine(AgarServer.Connections.Count);
                            AgarConnMgr.Add(msg.SenderConnection);
                        }
                        else if (status == NetConnectionStatus.Disconnected)
                        {
                            Console.WriteLine(NetUtility.ToHexString(msg.SenderConnection.RemoteUniqueIdentifier) + " disconnect!");
                            uint removeId = AgarConnMgr.Get(msg.SenderConnection);
                            PlayerBallMgr.Remove(removeId);
                            AgarConnMgr.Remove(msg.SenderConnection);
                            Console.WriteLine(AgarServer.Connections.Count);
                        }
                        break;

                    case NetIncomingMessageType.Data:
                        int id = msg.ReadInt32();
                        if (!ProcessPacket(AgarServer, id, msg))
                        {
                            DispatchPacket(AgarServer, id, msg);
                        }
                        break;
                    }
                }
                while ((msg = HappyServer.ReadMessage()) != null)
                {
                    switch (msg.MessageType)
                    {
                    case NetIncomingMessageType.DiscoveryRequest:
                        HappyServer.SendDiscoveryResponse(null, msg.SenderEndPoint);
                        break;

                    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)
                        {
                            Console.WriteLine(NetUtility.ToHexString(msg.SenderConnection.RemoteUniqueIdentifier) + " connected!");
                            Console.WriteLine(HappyServer.Connections.Count);
                            HappyConnMgr.Add(msg.SenderConnection);
                        }
                        else if (status == NetConnectionStatus.Disconnected)
                        {
                            Console.WriteLine(NetUtility.ToHexString(msg.SenderConnection.RemoteUniqueIdentifier) + " disconnect!");
                            uint removeId = HappyConnMgr.Get(msg.SenderConnection);
                            HappyPlayerMgr.Remove(removeId);
                            HappyConnMgr.Remove(msg.SenderConnection);
                            Console.WriteLine(HappyServer.Connections.Count);
                        }
                        break;

                    case NetIncomingMessageType.Data:
                        int id = msg.ReadInt32();
                        if (!ProcessPacket(HappyServer, id, msg))
                        {
                            DispatchPacket(HappyServer, id, msg);
                        }
                        break;
                    }
                }
                Thread.Sleep(1);
            }
            AgarServer.Shutdown("app exiting");
        }