void ProcessCommand(TileBaseCmd command) { switch (command) { case TileCMD_GetServername cmd: cmd.Servername = Settings.Servername; client.SendMessage(cmd.SID, serializer.Serialize(cmd)); break; case TileCMD_GetMapInfo cmd: cmd.MapInfo = Settings.MapInfo; client.SendMessage(cmd.SID, serializer.Serialize(cmd)); break; case TileCMD_UpdatePlayerInfo cmd: lock (tileClients) { PlayerInfo pi = tileClients.FirstOrDefault(p => p.ID == cmd.SID); if (pi == null) { tileClients.Add(pi = new PlayerInfo() { ID = cmd.SID }); } pi.Populate(cmd.Playerinfo); } break; } }
void Work() { State nextState = State.Initial; State actState = State.Initial; State prevState = State.Initial; while (true) { Events events = (Events)notify.WaitOne(); switch (actState) { case State.Initial: if (socket.IsConnected) { nextState = State.ConnectedNoLease; } else if (socket.IsConnecting) { } else { socket.ConnectAsync(Settings.ConnectionServer); } break; case State.ConnectedNoLease: if (events.HasFlag(Events.Disconnected)) { nextState = State.Disconnected; } else if (client.HasLease) { nextState = State.ConnectedWithLease; } break; case State.ConnectedWithLease: if (events.HasFlag(Events.Disconnected)) { nextState = State.Disconnected; } else if (!client.HasLease) { nextState = State.ConnectedNoLease; } else { //We are connected and have a lease. //Find all servers and wait for the user to connect to one. client.RequestSoftwareID(SoftwareID.TileMapServer); lock (Servers) { foreach (TileServer serv in Servers.Where(a => a.Name == null)) { client.SendMessage(serv.ID, serializer.Serialize(new TileCMD_GetServername())); } } if (commands.Any()) { TileBaseCmd cmd = commands.Dequeue(); ProcessCommand(cmd); if (commands.Any()) { notify.SetBits((UInt32)Events.CommandRecieved); } } if (SelectedServer != null) { nextState = State.ConnectedToServer; } } break; case State.ConnectedToServer: if (events.HasFlag(Events.Disconnected)) { nextState = State.Disconnected; } else if (!client.HasLease) { nextState = State.ConnectedNoLease; } else { if (events.HasFlag(Events.StateChanged)) { //Request the map from the server client.SendMessage(SelectedServer.ID, serializer.Serialize(new TileCMD_GetMapInfo())); } if (events.HasFlag(Events.UpdatePlayerInfo)) { playerinfo.ID = client.ID; client.SendMessage(SelectedServer.ID, serializer.Serialize(new TileCMD_UpdatePlayerInfo() { Playerinfo = playerinfo })); } if (commands.Any()) { TileBaseCmd cmd = commands.Dequeue(); ProcessCommand(cmd); if (commands.Any()) { notify.SetBits((UInt32)Events.CommandRecieved); } } } break; } if (actState != nextState) { notify.SetBits((UInt32)Events.StateChanged); prevState = actState; actState = nextState; Console.WriteLine($"{this.GetType().Name}: State change '{prevState}' => {actState}"); } } }