private async void discconectUserInactivity(bool withPing = true) { await Task.Delay(120000); if (this.socketClosed == true && this.destroyedClass != true) { using (MySQLManager.Database.Session_Details.Interfaces.IQueryAdapter dbClient = Program.manager.getQueryreactor()) { var json = new System.Web.Script.Serialization.JavaScriptSerializer(); dbClient.query("UPDATE server_1_players SET mapId = '" + this.mapId + "', x = '" + this.Ship.x + "', y = '" + this.Ship.y + "', settings = @settings WHERE playerID = '" + this.userId + "'", (new string[] { "settings" }), (new string[] { json.Serialize(this.Config) })); } this.socketClosed = true; this.destroyedClass = true; this.handler = null; this.Config = null; this.Ship = null; Out.WriteLine("userId " + this.userId + " disconnected from game", this.IP, ConsoleColor.DarkYellow); Program.Maps[this.mapId].RemoveUser(this.userId); Program.removeUser(this.userId); } else if (withPing) { checkPing(); } }
public void discconectUser(int Reason = 0) { try { this.handler.Shutdown(SocketShutdown.Both); this.handler.Close(); this.handler = null; this.socketClosed = true; } catch (Exception) { } if (Reason == -1 || Reason == 1) { using (MySQLManager.Database.Session_Details.Interfaces.IQueryAdapter dbClient = Program.manager.getQueryreactor()) { var json = new System.Web.Script.Serialization.JavaScriptSerializer(); dbClient.query("UPDATE server_1_players SET mapId = '" + this.mapId + "', x = '" + this.Ship.x + "', y = '" + this.Ship.y + "', settings = @settings WHERE playerID = '" + this.userId + "'", (new string[] { "settings" }), (new string[] { json.Serialize(this.Config) })); } } this.Config = null; if (Reason == 0) { Out.WriteLine("userId " + this.userId + " reconnected in another site", this.IP, ConsoleColor.DarkYellow); } else if (Reason == 1) { Out.WriteLine("userId " + this.userId + " has been destroyed", this.IP, ConsoleColor.DarkYellow); } else if (Reason == 2) { Out.WriteLine("Ivalid login", this.IP, ConsoleColor.DarkYellow); } if (Reason != -1) { this.socketClosed = true; this.destroyedClass = true; Program.removeUser(this.userId); } }
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 async void discconectUserInactivity(bool withPing = true) { await Task.Delay(120000); if (this.socketClosed == true && this.destroyedClass != true) { using (MySQLManager.Database.Session_Details.Interfaces.IQueryAdapter dbClient = Program.manager.getQueryreactor()) { var json = new System.Web.Script.Serialization.JavaScriptSerializer(); dbClient.query("UPDATE server_1_players SET mapId = '" + this.mapId + "', x = '" + this.Ship.x + "', y = '" + this.Ship.y + "', settings = @settings WHERE playerID = '" + this.userId + "'", (new string[] { "settings" }), (new string[] { json.Serialize(this.Config) } )); } this.socketClosed = true; this.destroyedClass = true; this.handler = null; this.Config = null; this.Ship = null; Out.WriteLine("userId " + this.userId + " disconnected from game", this.IP, ConsoleColor.DarkYellow); Program.Maps[this.mapId].RemoveUser(this.userId); Program.removeUser(this.userId); } else if(withPing) { checkPing(); } }
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) { } } }