/// <summary> /// Handles the servers's auth reply /// </summary> static public void Handle_SC_Auth(SC_Auth <Database> pkt, Database db) { //If it's a failure.. if (pkt.result != SC_Auth <Database> .LoginResult.Success) { switch (pkt.result) { case SC_Auth <Database> .LoginResult.Failure: //General failure.. if (pkt.message != "") { Log.write(TLog.Error, "Failure to connect to database: {0}", pkt.message); } else { Log.write(TLog.Error, "Unknown login failure."); } break; case SC_Auth <Database> .LoginResult.BadCredentials: Log.write(TLog.Error, "Invalid login credentials."); break; default: Log.write(TLog.Error, "Unknown login result."); break; } db._bLoginSuccess = false; } else { //Display additional info if present Log.write(TLog.Normal, "Connected to database server."); if (pkt.message != "") { Log.write(TLog.Normal, "Login info: {0}", pkt.message); } db._bLoginSuccess = true; } //Signal our completion db._syncStart.Set(); }
/// <summary> /// Handles the zone login request packet /// </summary> static public void Handle_CS_Auth(CS_Auth <Zone> pkt, Client <Zone> client) { //Note the login request Log.write(TLog.Normal, "Login request from ({0}): {1} / {2}", client._ipe, pkt.zoneID, pkt.password); //Attempt to find the associated zone DBServer server = client._handler as DBServer; Data.DB.zone dbZone; using (InfantryDataContext db = server.getContext()) dbZone = db.zones.SingleOrDefault(z => z.id == pkt.zoneID); //Does the zone exist? if (dbZone == null) { //Reply with failure SC_Auth <Zone> reply = new SC_Auth <Zone>(); reply.result = SC_Auth <Zone> .LoginResult.Failure; reply.message = "Invalid zone."; client.sendReliable(reply); return; } //Are the passwords a match? if (dbZone.password != pkt.password) { //Oh dear. SC_Auth <Zone> reply = new SC_Auth <Zone>(); reply.result = SC_Auth <Zone> .LoginResult.BadCredentials; client.sendReliable(reply); return; } //Great! Escalate our client object to a zone Zone zone = new Zone(client, server, dbZone); client._obj = zone; server._zones.Add(zone); //Called on connection close / timeout zone._client.Destruct += delegate(NetworkClient nc) { zone.destroy(); }; //Success! SC_Auth <Zone> success = new SC_Auth <Zone>(); success.result = SC_Auth <Zone> .LoginResult.Success; success.message = dbZone.notice; client.sendReliable(success); using (InfantryDataContext db = zone._server.getContext()) { //Update and activate the zone for our directory server //TODO: Don't know why it only works like this, //modifying dbZone and submitting changes doesn't reflect //in the database right away Data.DB.zone zoneentry = db.zones.SingleOrDefault(z => z.id == pkt.zoneID); zoneentry.name = pkt.zoneName; zoneentry.description = pkt.zoneDescription; zoneentry.ip = pkt.zoneIP; zoneentry.port = pkt.zonePort; zoneentry.advanced = Convert.ToInt16(pkt.zoneIsAdvanced); zoneentry.active = 1; db.SubmitChanges(); } Log.write("Successful login from {0} ({1})", dbZone.name, client._ipe); }
/// <summary> /// Creates a new packet based on the typeID and the received content /// inside the buffer. The user has to create an own implementation /// of this interface. /// </summary> public PacketBase createPacket(NetworkClient client, ushort typeID, byte[] buffer, int offset, int size) { //Ready our packet base PacketBase packet = null; size--; //Was it a system packet? if (buffer[offset++] == 0) { //Yes, find the appropriate type return(createSystemPacket(typeID, buffer, offset, size)); } //So what was the typeid? switch (typeID) { case SC_Auth <T> .TypeID: packet = new SC_Auth <T>(typeID, buffer, offset, size); break; case SC_PlayerLogin <T> .TypeID: packet = new SC_PlayerLogin <T>(typeID, buffer, offset, size); break; case SC_PlayerStatsResponse <T> .TypeID: packet = new SC_PlayerStatsResponse <T>(typeID, buffer, offset, size); break; case SC_Whisper <T> .TypeID: packet = new SC_Whisper <T>(typeID, buffer, offset, size); break; case SC_JoinChat <T> .TypeID: packet = new SC_JoinChat <T>(typeID, buffer, offset, size); break; case SC_LeaveChat <T> .TypeID: packet = new SC_LeaveChat <T>(typeID, buffer, offset, size); break; case SC_PrivateChat <T> .TypeID: packet = new SC_PrivateChat <T>(typeID, buffer, offset, size); break; case SC_Chat <T> .TypeID: packet = new SC_Chat <T>(typeID, buffer, offset, size); break; case SC_Zones <T> .TypeID: packet = new SC_Zones <T>(typeID, buffer, offset, size); break; case Disconnect <T> .TypeID: packet = new Disconnect <T>(typeID, buffer, offset, size); break; default: //An undefined packet. packet = new PacketDummy(typeID, buffer, offset, size); break; } return(packet); }