public void ConnectionHandler(object tcp) { var client = (TcpClient)tcp; SoftwareIncClient s = null; bool closing = false; while (!closing) { if (client.Connected) { try { Packet p = new Packet(client); Command cmd = commandFromPacket(p); switch (cmd.commandType) { case CommandType.Login: s = new SoftwareIncClient(client, cmd.source, cmd.money, cmd.year); SoftwareIncClient.generateId(s); clients.TryAdd(s.id, s); EfficientLogger.Log("New Login: "******" | " + s.id); break; case CommandType.Logout: clients.TryRemove(s.id, out s); client.Close(); closing = true; EfficientLogger.Log("Logout: " + s.name + " | " + s.id); break; case CommandType.Hack: break; case CommandType.Blame: break; case CommandType.Update: commands.Enqueue(cmd); EfficientLogger.Log("New Update: " + s.name + " | " + s.id); break; default: break; } } catch (Exception ex) { EfficientLogger.Log("Exception Caused by: " + s.name + " | " + s.id); EfficientLogger.Log(ex.Message); clients.TryRemove(s.id, out s); if (client.Connected) { client.Close(); } EfficientLogger.Log("Removed from active Connections"); break; } } } }
public static SoftwareIncClient getByName(IEnumerable <SoftwareIncClient> list, string name) { SoftwareIncClient res = null; foreach (var item in list) { if (item.name == name) { res = item; break; } } return(res); }
public void CommandDistributor(object o) { while (true) { Command c; if (commands.Count > 0) { commands.TryDequeue(out c); if (c != null) { SoftwareIncClient s = SoftwareIncClient.getByName(clients.Values, c.source); EfficientLogger.Log("Command " + c.commandType + " Issued by " + s.name + " | " + s.id); distribute(c, s); } } } }
public static void generateId(SoftwareIncClient s) { IPEndPoint remoteIpEndPoint = s.client.Client.RemoteEndPoint as IPEndPoint; if (remoteIpEndPoint != null) { s.id = ""; Random r = new Random(remoteIpEndPoint.Address.GetHashCode()); for (int i = 0; i < 20; i++) { s.id += (char)r.Next(48, 90); } } else { throw new Exception("Invalid IP-Address for ID-generation"); } }
public void distribute(Command c, SoftwareIncClient s) { Packet p = packetFromCommand(c); foreach (var item in clients.Keys) { SoftwareIncClient sic = null; switch (c.commandType) { case CommandType.Login: break; case CommandType.Logout: clients.TryGetValue(item, out sic); clients.TryRemove(sic.id, out sic); if (sic.client.Connected) { sic.client.Close(); } EfficientLogger.Log(item + " logout."); break; case CommandType.Hack: break; case CommandType.Blame: break; case CommandType.Update: if (s.id != item) { clients.TryGetValue(item, out sic); p.send(sic.client); } break; default: break; } } }