public Server(ArgumentOptions options) { //队伍数量在 1~4 之间,总人数不超过 8 if (options.TeamCount > 4) { options.TeamCount = 4; } if (options.TeamCount < 1) { options.TeamCount = 1; } if (options.PlayerCountPerTeam * options.TeamCount > 8) { options.PlayerCountPerTeam = (ushort)(8 / options.TeamCount); } if (options.PlayerCountPerTeam < 1) { options.PlayerCountPerTeam = 1; } this.options = options; game = new Map(MapInfo.map, options.TeamCount); communicationToGameID = new long[options.TeamCount, options.PlayerCountPerTeam]; for (int i = 0; i < communicationToGameID.GetLength(0); ++i) { for (int j = 0; j < communicationToGameID.GetLength(1); ++j) { communicationToGameID[i, j] = GameObject.invalidID; //初始值为无效ID,代表还未进入游戏 } } serverCommunicator = new Communication.CommServer.CommServer(); while (!serverCommunicator.Listen(options.ServerPort)) { Console.WriteLine("Server listen failed!"); Thread.Sleep(1000); } Console.WriteLine("Server begins to listen!"); serverCommunicator.OnConnect += delegate() { Console.WriteLine("Successfully connected!"); }; serverCommunicator.OnReceive += delegate() { if (serverCommunicator.TryTake(out IMsg msg) && msg.PacketType == PacketType.MessageToServer) { this.OnReceive((MessageToServer)msg.Content); } }; Thread.Sleep(int.MaxValue); }
public ServerBase(ArgumentOptions options) { //队伍数量在 1~4 之间,总人数不超过 8 if (options.TeamCount > 4) { options.TeamCount = 4; } if (options.TeamCount < 1) { options.TeamCount = 1; } if (options.PlayerCountPerTeam * options.TeamCount > 8) { options.PlayerCountPerTeam = (ushort)(8 / options.TeamCount); } if (options.PlayerCountPerTeam < 1) { options.PlayerCountPerTeam = 1; } this.options = options; serverCommunicator = new Communication.CommServer.CommServer(); while (!serverCommunicator.Listen(options.ServerPort)) { Console.WriteLine("Server listen failed!"); Thread.Sleep(1000); } Console.WriteLine("Server begins to listen!"); serverCommunicator.OnConnect += delegate() { Console.WriteLine("Successfully connected!"); }; serverCommunicator.OnReceive += delegate() { if (serverCommunicator.TryTake(out IMsg msg) && msg.PacketType == PacketType.MessageToServer) { this.OnReceive((MessageToServer)msg.Content); } }; }