public Decode ( byte buffer, int offset, int length ) : void | ||
buffer | byte | |
offset | int | |
length | int | |
return | void |
private void EndCheckEncryption(IAsyncResult result) { var id = (PeerId) result.AsyncState; try { byte[] initialData; EncryptorFactory.EndCheckEncryption(result, out initialData); if (initialData != null && initialData.Length == HandshakeMessage.HandshakeLength) { var message = new HandshakeMessage(); message.Decode(initialData, 0, initialData.Length); HandleHandshake(id, message); } else if (initialData == null || initialData.Length > 0) { throw new Exception("Argh. I can't handle this scenario. It also shouldn't happen. Ever."); } else { PeerIO.EnqueueReceiveHandshake(id.Connection, id.Decryptor, _handshakeReceivedCallback, id); } } catch { id.Connection.Dispose(); } }
private static void HandshakeReceived(bool successful, int transferred, object state) { var data = (ReceiveMessageState) state; PeerMessage message = null; if (successful) { data.Decryptor.Decrypt(data.Buffer, 0, transferred); message = new HandshakeMessage(); message.Decode(data.Buffer, 0, transferred); } data.Callback(successful, message, data.State); ClientEngine.BufferManager.FreeBuffer(data.Buffer); ReceiveCache.Enqueue(data); }
private static void HandshakeReceived(bool succeeded, int count, object state) { var result = (EncryptorAsyncResult) state; var connection = result.Id.Connection; try { if (!succeeded) throw new EncryptionException("Couldn't receive the handshake"); result.Available += count; var message = new HandshakeMessage(); message.Decode(result.Buffer, 0, result.Buffer.Length); var valid = message.ProtocolString == VersionInfo.ProtocolStringV100; var usable = CheckRc4(result.Id); var canUseRc4 = Toolbox.HasEncryption(usable, EncryptionTypes.RC4Header) || Toolbox.HasEncryption(usable, EncryptionTypes.RC4Full); // If encryption is disabled and we received an invalid handshake - abort! if (valid) { result.InitialData = result.Buffer; result.Complete(); return; } if (!canUseRc4) { result.Complete(new EncryptionException("Invalid handshake received and no decryption works")); return; } // The data we just received was part of an encrypted handshake and was *not* the BitTorrent handshake result.EncSocket = new PeerBEncryption(result.SKeys, EncryptionTypes.All); result.EncSocket.BeginHandshake(connection, result.Buffer, 0, result.Buffer.Length, CompletedEncryptedHandshakeCallback, result); } catch (Exception ex) { result.Complete(ex); } }