private void ReceiveData(IAsyncResult ar) { try { if (this.isDisconnected) { return; } if (!sock.Connected) { ClientManager.EnterCriticalArea(); this.Disconnect(); ClientManager.LeaveCriticalArea(); return; } try { stream.EndRead(ar); } catch (Exception) { ClientManager.EnterCriticalArea(); this.Disconnect(); ClientManager.LeaveCriticalArea(); return; } byte[] raw = (byte[])ar.AsyncState; if (alreadyReceived < lastSize) { int left = lastSize - alreadyReceived; if (left > 1024) { left = 1024; } if (left > sock.Available) { left = sock.Available; } try { stream.BeginRead(raw, 4 + alreadyReceived, left, this.callbackData, raw); } catch (Exception ex) { Logger.ShowError(ex); ClientManager.EnterCriticalArea(); this.Disconnect(); ClientManager.LeaveCriticalArea(); return; } alreadyReceived += left; return; } raw = Crypt.Decrypt(raw, 8); Packet p = new Packet(); p.data = raw; uint length = p.GetUInt(4); uint offset = 0; while (offset < length) { uint size; if (firstLevelLenth == 4) { size = p.GetUInt((ushort)(8 + offset)); } else { size = p.GetUShort((ushort)(8 + offset)); } offset += firstLevelLenth; if (size + offset > length) { break; } Packet p2 = new Packet(); p2.data = p.GetBytes((ushort)size, (ushort)(8 + offset)); offset += size; ProcessPacket(p2); } try { stream.BeginRead(buffer, 0, 4, this.callbackSize, null); } catch (Exception) { ClientManager.EnterCriticalArea(); this.Disconnect(); ClientManager.LeaveCriticalArea(); return; } } catch (Exception e) { Logger.ShowError(e, null); } }
private void ReceiveKeyExchange(IAsyncResult ar) { try { if (this.isDisconnected) { return; } if (!sock.Connected) { ClientManager.EnterCriticalArea(); this.Disconnect(); ClientManager.LeaveCriticalArea(); return; } try { stream.EndRead(ar); } catch (Exception) { ClientManager.EnterCriticalArea(); this.Disconnect(); ClientManager.LeaveCriticalArea(); return; } byte[] raw = (byte[])ar.AsyncState; if (raw.Length == 8) { Packet p1 = new Packet(529); p1.PutUInt(1, 4); p1.PutByte(0x32, 8); p1.PutUInt(0x100, 9); Crypt.MakePrivateKey(); string bufstring = Conversions.bytes2HexString(Encryption.Module.getBytes()); p1.PutBytes(System.Text.Encoding.ASCII.GetBytes(bufstring.ToLower()), 13); p1.PutUInt(0x100, 269); bufstring = Conversions.bytes2HexString(Crypt.GetKeyExchangeBytes()); p1.PutBytes(System.Text.Encoding.ASCII.GetBytes(bufstring), 273); SendPacket(p1, true, true); try { byte[] data = new byte[260]; stream.BeginRead(data, 0, 260, this.callbackKeyExchange, data); } catch (Exception) { ClientManager.EnterCriticalArea(); this.Disconnect(); ClientManager.LeaveCriticalArea(); return; } } else if (raw.Length == 260) { Packet p1 = new Packet(); p1.data = raw; byte[] keyBuf = p1.GetBytes(256, 4); Crypt.MakeAESKey(System.Text.Encoding.ASCII.GetString(keyBuf)); StartPacketParsing(); } else if (raw.Length == 529) { Packet p1 = new Packet(); p1.data = raw; byte[] keyBuf = p1.GetBytes(256, 273); Crypt.MakePrivateKey(); Packet p2 = new Packet(260); p2.PutUInt(0x100, 0); string bufstring = Conversions.bytes2HexString(Crypt.GetKeyExchangeBytes()); p2.PutBytes(System.Text.Encoding.ASCII.GetBytes(bufstring), 4); SendPacket(p2, true, true); Crypt.MakeAESKey(System.Text.Encoding.ASCII.GetString(keyBuf)); StartPacketParsing(); } } catch (Exception ex) { Logger.ShowError(ex); } }
private void ReceiveData(IAsyncResult ar) { try { if (this.isDisconnected) { return; } if (!sock.Connected) { ClientManager.EnterCriticalArea(); this.Disconnect(); ClientManager.LeaveCriticalArea(); return; } try { stream.EndRead(ar); } catch (Exception) { ClientManager.EnterCriticalArea(); this.Disconnect(); ClientManager.LeaveCriticalArea(); return; } byte[] raw = (byte[])ar.AsyncState; if (alreadyReceived < lastSize) { int left = lastSize - alreadyReceived; if (left > 1024) left = 1024; if (left > sock.Available) left = sock.Available; try { stream.BeginRead(raw, 4 + alreadyReceived, left, this.callbackData, raw); } catch (Exception ex) { Logger.ShowError(ex); ClientManager.EnterCriticalArea(); this.Disconnect(); ClientManager.LeaveCriticalArea(); return; } alreadyReceived += left; return; } raw = Crypt.Decrypt(raw, 8); Packet p = new Packet(); p.data = raw; uint length = p.GetUInt(4); uint offset = 0; while (offset < length) { uint size; if(firstLevelLenth ==4) size= p.GetUInt((ushort)(8 + offset)); else size = p.GetUShort((ushort)(8 + offset)); offset += firstLevelLenth; if (size + offset > length) break; Packet p2 = new Packet(); p2.data = p.GetBytes((ushort)size, (ushort)(8 + offset)); offset += size; ProcessPacket(p2); } try { stream.BeginRead(buffer, 0, 4, this.callbackSize, null); } catch (Exception) { ClientManager.EnterCriticalArea(); this.Disconnect(); ClientManager.LeaveCriticalArea(); return; } } catch (Exception e) { Logger.ShowError(e, null); } }