public void ReadCallback(IAsyncResult ar) { String content = String.Empty; try { if (handler == null || !handler.Connected) { if (!this.socketClosed) { Out.WriteLine("Socket closed, waiting for a reconnection (2 min limit)", this.IP, ConsoleColor.DarkYellow); this.socketClosed = true; this.discconectUserInactivity(); } return; } int bytesRead = handler.EndReceive(ar); //handler.rea if (bytesRead > 0) { content = System.Text.Encoding.UTF8.GetString(buffer, 0, bytesRead); if (Program.Settings["flushPackets"] == "true") { Console.WriteLine("'" + content + "'"); } if (content.StartsWith("<policy-file-request/>")) { Out.WriteLine("Sended policy file", handler.RemoteEndPoint.ToString().Split(':')[0]); Send("<?xml version=\"1.0\"?><cross-domain-policy xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://www.adobe.com/xml/schemas/PolicyFileSocket.xsd\"><allow-access-from domain=\"*\" to-ports=\"*\" secure=\"false\" /><site-control permitted-cross-domain-policies=\"master-only\" /></cross-domain-policy>"); handler.Shutdown(SocketShutdown.Both); this.handler.Close(); GC.Collect(); return; } else { Do.Core.parserPacket packetParser = new Do.Core.parserPacket(content); string packetHeader = packetParser.getString(); if (packetHeader == "LOGIN" && this.userAuth == false) { Out.WriteLine("Received petition connection", IP); this.userId = packetParser.getUInt(); ulong sessionId = packetParser.getULong(); this.userAuth = true; DataRow userRow = Program.checkSession(userId, sessionId); if (userRow == null) { //this.Send("ERR|2"); // EXTREME LOL this.discconectUser(2); return; } if (Program.Users.ContainsKey(this.userId)) { this.Config = Program.Users[this.userId].Config; this.Ship = Program.Users[this.userId].Ship; this.mapId = Program.Users[this.userId].mapId; Program.Maps[this.mapId].RemoveUser(this.userId); Program.Users[this.userId].discconectUser(); Program.Users.Add(this.userId, this); this.sendConfig(); Program.Maps[this.mapId].AddUser(this.userId); } else { this.mapId = Convert.ToUInt16(userRow["mapId"]); Program.Users.Add(userId, this); if (string.IsNullOrEmpty(userRow["settings"].ToString())) { this.Config = new usersClass.Config(); } else { try { var json = new System.Web.Script.Serialization.JavaScriptSerializer(); this.Config = json.Deserialize<usersClass.Config>(userRow["settings"].ToString()); } catch (Exception) { this.Config = new usersClass.Config(); } } this.Ship = new usersClass.Ship(Convert.ToUInt32(userRow["shipId"]), userRow["Name"].ToString(), Convert.ToUInt16(userRow["factionId"]), userRow["x"].ToString(), userRow["y"].ToString()); this.sendConfig(); Program.Maps[this.mapId].AddUser(this.userId); } this.AttackShip(); Out.WriteLine("Is connected now with userId: " + this.userId, IP, ConsoleColor.DarkGreen); this.LastPing = Program.GetUnixTimestamp; this.checkPing(); } else if (this.userAuth == true) { if (packetHeader.StartsWith("PNG")) { this.socketClosed = false; this.LastPing = Program.GetUnixTimestamp; Program.Maps[this.mapId].sendNPC(this.userId); } else if (packetHeader.StartsWith("RDY")) { string packetSubHeader = packetParser.getString(); if (packetSubHeader.StartsWith("MAP")) { } } else if (packetHeader.StartsWith("SEL")) { uint _userId = packetParser.getUInt(); if (_userId < 1000) { if (Program.Maps[this.mapId].Aliens.ContainsKey(_userId)) { this.Ship.selectedUserId = _userId; this.Send("0|N|" + _userId + "|" + Program.NPCS[Program.Maps[this.mapId].Aliens[_userId].typeId].Name + "|" + Program.Maps[this.mapId].Aliens[_userId].shield + "|" + Program.NPCS[Program.Maps[this.mapId].Aliens[_userId].typeId].Shield + "|" + Program.Maps[this.mapId].Aliens[_userId].HP + "|" + Program.NPCS[Program.Maps[this.mapId].Aliens[_userId].typeId].HP); } } else if (_userId != this.userId && Program.Users.ContainsKey(_userId) && Program.Users[_userId].mapId == this.mapId) { this.Ship.selectedUserId = _userId; this.Send("0|N|" + _userId + "|" + Program.Users[_userId].Ship.Name + "|" + Program.Users[_userId].Ship.shield + "|" + Program.Users[_userId].Ship.maxShield + "|" + Program.Users[_userId].Ship.HP + "|" + Program.Users[_userId].Ship.maxHP); } } else if (packetHeader.StartsWith("1")) { Program.Maps[this.mapId].moveShip(this.userId, packetParser.getString(), packetParser.getString(), 1000); } else if (packetHeader.StartsWith("a")) { if (this.Ship.isAttacking == false) { this.Ship.isAttacking = true; } } else if (packetHeader.StartsWith("j")) { Program.Maps[this.mapId].usePortal(this.userId, this.Ship.x, this.Ship.y); } else if (packetHeader.StartsWith("x")) { Program.Maps[this.mapId].pickBonusBox(this.userId, packetParser.getUInt()); } else if (packetHeader.StartsWith("u")) { ushort _laserId = packetParser.getUShort(); if (_laserId < 7) { this.laserId = _laserId; } } else if (packetHeader.StartsWith("7")) { changeSettings(packetParser); } else if (packetHeader.StartsWith("A")) { changeSettings(packetParser); } else if (packetHeader.StartsWith("G")) { this.Ship.isAttacking = false; } } packetParser.Dispose(); } } } catch (SocketException e) { if (e.NativeErrorCode.Equals(10035)) Out.WriteLine("Socket Exception Event: " + e.Message, "Socket.Exception", ConsoleColor.DarkRed); else { Console.WriteLine(e.ToString()); Out.WriteLine("Socket closed, waiting for a reconnection (2 min limit)", this.IP, ConsoleColor.DarkYellow); this.socketClosed = true; this.discconectUserInactivity(); } } catch (Exception e) { Out.WriteLine("Handler Error: " + e.ToString(), this.IP, ConsoleColor.DarkRed); } finally { try { if (handler != null && handler.Connected) { handler.BeginReceive(buffer, 0, buffer.Length, 0, new AsyncCallback(ReadCallback), this); } } catch (Exception) { } } }
private void changeSettings(parserPacket packet) { try { string settingName = packet.getString().Replace("\n", ""); settingName = (settingName == "7") ? packet.getString().Replace("\n", "") : settingName; string[] settingsArray = {}; if (settingName.Contains(",")) { settingsArray = settingName.Split(','); settingName = settingsArray[0]; } if (settingName == "SET") { this.Config.SET = packet.getInt() + "|" + packet.getInt() + "|" + packet.getInt() + "|" + packet.getInt() + "|" + packet.getInt() + "|" + packet.getInt() + "|" + packet.getInt() + "|" + packet.getInt() + "|" + packet.getInt() + "|" + packet.getInt() + "|" + packet.getInt() + "|" + packet.getInt() + "|" + packet.getInt() + "|" + packet.getInt() + "|" + packet.getInt() + "|" + packet.getInt() + "|" + packet.getInt() + "|" + packet.getInt() + "|" + packet.getInt() + "|" + packet.getInt() + "|" + packet.getInt() + "|" + packet.getInt() + "|" + packet.getInt() + "|" + packet.getInt() + "|" + packet.getInt(); } else if (settingName == "MINIMAP_SCALE") { this.Config.MINIMAP_SCALE = settingsArray[1] + "|" + packet.getUInt(); } else if (settingName == "DISPLAY_PLAYER_NAMES") { this.Config.DISPLAY_PLAYER_NAMES = Program.ToBool(packet.getString()); } else if (settingName == "DISPLAY_CHAT") { this.Config.DISPLAY_CHAT = Program.ToBool(packet.getString()); } else if (settingName == "PLAY_MUSIC") { this.Config.PLAY_MUSIC = Program.ToBool(packet.getString()); } else if (settingName == "PLAY_SFX") { this.Config.PLAY_SFX = Program.ToBool(packet.getString()); } else if (settingName == "BAR_STATUS") { string[] barStatusArray = packet.getString().Split(','); this.Config.BAR_STATUS = "23," + Program.ToEnum(Program.ToBool(barStatusArray[1])) + ",24," + Program.ToEnum(Program.ToBool(barStatusArray[3])) + ",25," + Program.ToEnum(Program.ToBool(barStatusArray[5])) + ",26," + Program.ToEnum(Program.ToBool(barStatusArray[7])) + ",27," + Program.ToEnum(Program.ToBool(barStatusArray[9])) + ""; } else if (settingName == "WINDOW_SETTINGS") { this.Config.WINDOW_SETTINGS = settingsArray[1] + "|" + packet.getString(); } else if (settingName == "AUTO_REFINEMENT") { this.Config.AUTO_REFINEMENT = Program.ToBool(packet.getString()); } else if (settingName == "QUICKSLOT_STOP_ATTACK") { this.Config.QUICKSLOT_STOP_ATTACK = Program.ToBool(packet.getString()); } else if (settingName == "QUICKBAR_SLOT") { this.Config.QUICKBAR_SLOT = packet.getString(); } if (packet.MoreToRead) { changeSettings(packet); } } catch (Exception) { if (this.handler != null) { this.handler.Shutdown(SocketShutdown.Both); } this.socketClosed = true; discconectUserInactivity(false); } }
public void ReadCallback(IAsyncResult ar) { String content = String.Empty; try { if (handler == null || !handler.Connected) { if (!this.socketClosed) { Out.WriteLine("Socket closed, waiting for a reconnection (2 min limit)", this.IP, ConsoleColor.DarkYellow); this.socketClosed = true; this.discconectUserInactivity(); } return; } int bytesRead = handler.EndReceive(ar); //handler.rea if (bytesRead > 0) { content = System.Text.Encoding.UTF8.GetString(buffer, 0, bytesRead); if (Program.Settings["flushPackets"] == "true") { Console.WriteLine("'" + content + "'"); } if (content.StartsWith("<policy-file-request/>")) { Out.WriteLine("Sended policy file", handler.RemoteEndPoint.ToString().Split(':')[0]); Send("<?xml version=\"1.0\"?><cross-domain-policy xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://www.adobe.com/xml/schemas/PolicyFileSocket.xsd\"><allow-access-from domain=\"*\" to-ports=\"*\" secure=\"false\" /><site-control permitted-cross-domain-policies=\"master-only\" /></cross-domain-policy>"); handler.Shutdown(SocketShutdown.Both); this.handler.Close(); GC.Collect(); return; } else { Do.Core.parserPacket packetParser = new Do.Core.parserPacket(content); string packetHeader = packetParser.getString(); if (packetHeader == "LOGIN" && this.userAuth == false) { Out.WriteLine("Received petition connection", IP); this.userId = packetParser.getUInt(); ulong sessionId = packetParser.getULong(); this.userAuth = true; DataRow userRow = Program.checkSession(userId, sessionId); if (userRow == null) { //this.Send("ERR|2"); // EXTREME LOL this.discconectUser(2); return; } if (Program.Users.ContainsKey(this.userId)) { this.Config = Program.Users[this.userId].Config; this.Ship = Program.Users[this.userId].Ship; this.mapId = Program.Users[this.userId].mapId; Program.Maps[this.mapId].RemoveUser(this.userId); Program.Users[this.userId].discconectUser(); Program.Users.Add(this.userId, this); this.sendConfig(); Program.Maps[this.mapId].AddUser(this.userId); } else { this.mapId = Convert.ToUInt16(userRow["mapId"]); Program.Users.Add(userId, this); if (string.IsNullOrEmpty(userRow["settings"].ToString())) { this.Config = new usersClass.Config(); } else { try { var json = new System.Web.Script.Serialization.JavaScriptSerializer(); this.Config = json.Deserialize <usersClass.Config>(userRow["settings"].ToString()); } catch (Exception) { this.Config = new usersClass.Config(); } } this.Ship = new usersClass.Ship(Convert.ToUInt32(userRow["shipId"]), userRow["Name"].ToString(), Convert.ToUInt16(userRow["factionId"]), userRow["x"].ToString(), userRow["y"].ToString()); this.sendConfig(); Program.Maps[this.mapId].AddUser(this.userId); } this.AttackShip(); Out.WriteLine("Is connected now with userId: " + this.userId, IP, ConsoleColor.DarkGreen); this.LastPing = Program.GetUnixTimestamp; this.checkPing(); } else if (this.userAuth == true) { if (packetHeader.StartsWith("PNG")) { this.socketClosed = false; this.LastPing = Program.GetUnixTimestamp; Program.Maps[this.mapId].sendNPC(this.userId); } else if (packetHeader.StartsWith("RDY")) { string packetSubHeader = packetParser.getString(); if (packetSubHeader.StartsWith("MAP")) { } } else if (packetHeader.StartsWith("SEL")) { uint _userId = packetParser.getUInt(); if (_userId < 1000) { if (Program.Maps[this.mapId].Aliens.ContainsKey(_userId)) { this.Ship.selectedUserId = _userId; this.Send("0|N|" + _userId + "|" + Program.NPCS[Program.Maps[this.mapId].Aliens[_userId].typeId].Name + "|" + Program.Maps[this.mapId].Aliens[_userId].shield + "|" + Program.NPCS[Program.Maps[this.mapId].Aliens[_userId].typeId].Shield + "|" + Program.Maps[this.mapId].Aliens[_userId].HP + "|" + Program.NPCS[Program.Maps[this.mapId].Aliens[_userId].typeId].HP); } } else if (_userId != this.userId && Program.Users.ContainsKey(_userId) && Program.Users[_userId].mapId == this.mapId) { this.Ship.selectedUserId = _userId; this.Send("0|N|" + _userId + "|" + Program.Users[_userId].Ship.Name + "|" + Program.Users[_userId].Ship.shield + "|" + Program.Users[_userId].Ship.maxShield + "|" + Program.Users[_userId].Ship.HP + "|" + Program.Users[_userId].Ship.maxHP); } } else if (packetHeader.StartsWith("1")) { Program.Maps[this.mapId].moveShip(this.userId, packetParser.getString(), packetParser.getString(), 1000); } else if (packetHeader.StartsWith("a")) { if (this.Ship.isAttacking == false) { this.Ship.isAttacking = true; } } else if (packetHeader.StartsWith("j")) { Program.Maps[this.mapId].usePortal(this.userId, this.Ship.x, this.Ship.y); } else if (packetHeader.StartsWith("x")) { Program.Maps[this.mapId].pickBonusBox(this.userId, packetParser.getUInt()); } else if (packetHeader.StartsWith("u")) { ushort _laserId = packetParser.getUShort(); if (_laserId < 7) { this.laserId = _laserId; } } else if (packetHeader.StartsWith("7")) { changeSettings(packetParser); } else if (packetHeader.StartsWith("A")) { changeSettings(packetParser); } else if (packetHeader.StartsWith("G")) { this.Ship.isAttacking = false; } } packetParser.Dispose(); } } } catch (SocketException e) { if (e.NativeErrorCode.Equals(10035)) { Out.WriteLine("Socket Exception Event: " + e.Message, "Socket.Exception", ConsoleColor.DarkRed); } else { Console.WriteLine(e.ToString()); Out.WriteLine("Socket closed, waiting for a reconnection (2 min limit)", this.IP, ConsoleColor.DarkYellow); this.socketClosed = true; this.discconectUserInactivity(); } } catch (Exception e) { Out.WriteLine("Handler Error: " + e.ToString(), this.IP, ConsoleColor.DarkRed); } finally { try { if (handler != null && handler.Connected) { handler.BeginReceive(buffer, 0, buffer.Length, 0, new AsyncCallback(ReadCallback), this); } } catch (Exception) { } } }