/// <summary> /// Create a new netIO class using a given socket. /// </summary> /// <param name="sock">The socket for this netIO class.</param> public NetIO(Socket sock, Dictionary<ushort, Packet> commandTable, Client client ,ClientManager manager) { this.sock = sock; this.stream = new NetworkStream(sock); this.commandTable = commandTable; this.client = client; this.currentClientManager = manager; Crypt = new Encryption(); this.callbackSize = new AsyncCallback(this.ReceiveSize); this.callbackData = new AsyncCallback(this.ReceiveData); this.callbackKeyExchange= new AsyncCallback(this.ReceiveKeyExchange); // Use the static key untill the keys have been exchanged this.isDisconnected = false; }
public NetIO(Socket sock, Dictionary<ushort, Packet> commandTable, Client client, bool isgateway) { this.sock = sock; this.stream = new NetworkStream(sock); this.commandTable = commandTable; this.client = client; this.isGateway = isgateway; this.callbackSize = new AsyncCallback(this.ReceiveSize); this.callbackData = new AsyncCallback(this.ReceiveData); this.nlock = new ReaderWriterLock(); // Use the static key untill the keys have been exchanged this.clientKey = new byte[16]; Encryption.StaticKey.CopyTo(this.clientKey, 0); this.serverKey = new byte[16]; Encryption.StaticKey.CopyTo(this.serverKey, 0); this.isDisconnected = false; // Receive the size of the next packet and call ReceiveSize when finished if (sock.Connected) { try { stream.BeginRead(buffer, 0, 2, this.callbackSize, null); } catch (Exception ex) { Logger.ShowError(ex, null); try//this could crash the gateway somehow,so better ignore the Exception { this.Disconnect(); } catch (Exception) { } Logger.ShowWarning("Invalid packet head from:" + sock.RemoteEndPoint.ToString(), null); return; } } else { this.Disconnect(); return; } }
public override void Parse(SagaLib.Client client) { ((MapClient)(client)).OnAddShortcut(this); }
public override void Parse(SagaLib.Client client) { ((MapClient)(client)).OnSetSpecialSkill(this); }
public override void OnClientDisconnect(Client client) { MapClient client_ = (MapClient)client; if (this.clients.ContainsKey(client_.SessionID)) this.clients.Remove(client_.SessionID); }
public override void OnClientDisconnect(Client client_t) { LoginClient client = (LoginClient)client_t; if (client.isMapServer) { Logger.ShowWarning("A map server just disconnected.",null); LoginServer.charServerList[client.mapServer.worldID].DeleteMapServer(client.mapServer); } this.clients.Remove(client.SessionID); }
/// <summary> /// Parse this packet (only used for GetPackets) /// </summary> public virtual void Parse(Client client) { return; }
public override void Parse(SagaLib.Client client) { ((MapClient)(client)).OnSendJump(this); }
public override void Parse(SagaLib.Client client) { ((MapClient)(client)).OnPartyQuit(this); }
public override void Parse(SagaLib.Client client) { ((MapClient)(client)).OnWeaponUpgrade(this); }
public override void Parse(SagaLib.Client client) { ((MapClient)(client)).OnNPCShopSell(this); }
public override void Parse(SagaLib.Client client) { ((MapClient)(client)).OnMarketUpdateComment(this); }
public override void Parse(SagaLib.Client client) { ((LoginClient)(client)).OnPing(this); }
public override void Parse(SagaLib.Client client) { MapClient cli = (MapClient)client; cli.OnSupplyExchange(this); }
public override void Parse(SagaLib.Client client) { ((MapSession)(client)).OnSendKey(this); }
public override void Parse(SagaLib.Client client) { ((MapClient)(client)).OnMotion(this); }
public override void Parse(SagaLib.Client client) { ((LoginSession)(client)).OnSendGUID(this); }
public override void Parse(SagaLib.Client client) { ((MapClient)(client)).OnGetMail(this); }
public override void Parse(SagaLib.Client client) { ((MapClient)(client)).OnJobChange(this); }
public override void Parse(SagaLib.Client client) { ((MapClient)(client)).OnLogout(this); }
public override void OnClientDisconnect(Client client_t) { clients.Remove((LoginClient)client_t); }
public override void Parse(SagaLib.Client client) { ((MapClient)(client)).OnSkillCast(this); }
private void ReceiveData(IAsyncResult ar) { try { this.nlock.AcquireWriterLock(Timeout.Infinite); if (this.isDisconnected) { this.nlock.ReleaseWriterLock(); return; } if (!sock.Connected) { this.nlock.ReleaseWriterLock(); ClientManager.EnterCriticalArea(); this.Disconnect(); ClientManager.LeaveCriticalArea(); return; } try { stream.EndRead(ar); } catch (Exception) { this.nlock.ReleaseWriterLock(); ClientManager.EnterCriticalArea(); this.Disconnect(); ClientManager.LeaveCriticalArea(); return; } byte[] raw = (byte[])ar.AsyncState; if (this.isGateway) raw = Encryption.Decrypt(raw, 2, this.ClientKey); if (!isGateway) { ushort messageID = (ushort)(raw[7] + (raw[6] << 8)); if (!this.commandTable.ContainsKey((messageID))) { if (!this.fullHeader) { Logger.ShowWarning(string.Format("Got unknown packet {0:X} {1:X} from " + sock.RemoteEndPoint.ToString(), raw[6], raw[7]), null); } else { if (commandTable.ContainsKey((ushort)0xFFFF)) { if (this.commandTable[(ushort)0xFFFF].SizeIsOk((ushort)raw.Length)) { Packet p = this.commandTable[(ushort)0xFFFF].New(); p.data = raw; p.size = (ushort)(raw.Length); ClientManager.EnterCriticalArea(); try { p.Parse(this.client); } catch (Exception ex) { Logger.ShowError(ex); } ClientManager.LeaveCriticalArea(); } else { string error = "Invalid packet size from client " + sock.RemoteEndPoint.ToString(); Console.WriteLine(error); Log.WriteLog(error); ClientManager.EnterCriticalArea(); this.Disconnect(); ClientManager.LeaveCriticalArea(); return; } } else { Logger.ShowWarning("Universal Packet 0xFFFF not defined!", null); } } } else { if (this.commandTable[messageID].SizeIsOk((ushort)raw.Length)) { Packet p = this.commandTable[messageID].New(); p.data = raw; p.size = (ushort)(raw.Length); Client client; if (p.SessionID != 0) { client = this.currentClientManager.GetClient(p.SessionID); if (client == null) client = this.client; } else { client = this.client; } ClientManager.EnterCriticalArea(); try { if (client.netIO == null) client.netIO = this; p.Parse(client); } catch (Exception ex) { Logger.ShowError(ex); } ClientManager.LeaveCriticalArea(); } else { string error = string.Format("Invalid packet size(Packet:{0:X4}) from client {1}",messageID, sock.RemoteEndPoint.ToString()); Console.WriteLine(error); Log.WriteLog(error); this.nlock.ReleaseWriterLock(); ClientManager.EnterCriticalArea(); this.Disconnect(); ClientManager.LeaveCriticalArea(); return; } } } else { ushort messageID; if (!fullHeader) { messageID = (ushort)(raw[7] + (raw[6] << 8)); if (!this.commandTable.ContainsKey((messageID))) { Logger.ShowWarning(string.Format("Got unknown packet {0:X} {1:X} from " + sock.RemoteEndPoint.ToString(), raw[6], raw[7]), null); } else { if (this.commandTable[messageID].SizeIsOk((ushort)raw.Length)) { Packet p = this.commandTable[messageID].New(); p.data = raw; p.size = (ushort)(raw.Length); p.isGateway = this.isGateway; ClientManager.EnterCriticalArea(); try { p.Parse(this.client); } catch (Exception ex) { Logger.ShowError(ex); } ClientManager.LeaveCriticalArea(); } else { string error = "Invalid packet size from client " + sock.RemoteEndPoint.ToString(); Console.WriteLine(error); Log.WriteLog(error); this.nlock.ReleaseWriterLock(); ClientManager.EnterCriticalArea(); this.Disconnect(); ClientManager.LeaveCriticalArea(); return; } } } else { if (commandTable.ContainsKey((ushort)0xFFFF)) { if (this.commandTable[(ushort)0xFFFF].SizeIsOk((ushort)raw.Length)) { Packet p = this.commandTable[(ushort)0xFFFF].New(); p.data = raw; p.size = (ushort)(raw.Length); p.isGateway = this.isGateway; ClientManager.EnterCriticalArea(); try { p.Parse(this.client); } catch (Exception ex) { Logger.ShowError(ex); } ClientManager.LeaveCriticalArea(); } else { string error = "Invalid packet size from client " + sock.RemoteEndPoint.ToString(); Console.WriteLine(error); Log.WriteLog(error); this.nlock.ReleaseWriterLock(); ClientManager.EnterCriticalArea(); this.Disconnect(); ClientManager.LeaveCriticalArea(); return; } } else { Logger.ShowWarning("Universal Packet 0xFFFF not defined!", null); } } } try { this.nlock.ReleaseWriterLock(); stream.BeginRead(buffer, 0, 2, this.callbackSize, null); } catch (Exception) { ClientManager.EnterCriticalArea(); this.Disconnect(); ClientManager.LeaveCriticalArea(); return; } } catch (Exception e) { Logger.ShowError(e, null); } }
public override void Parse(SagaLib.Client client) { MapClient cli = (MapClient)client; cli.OnWarp(this); }
public virtual void OnClientDisconnect(Client client){ }
public override void Parse(SagaLib.Client client) { ((LoginClient)(client)).OnCharCreate(this); }
public override void OnClientDisconnect(Client client_t) { GatewayClient client = (GatewayClient)client_t; this.clients.Remove(client.SessionID); }
public override void Parse(SagaLib.Client client) { ((MapClient)(client)).OnTradeMoney(this); }