async Task StartRead(CancellationToken ct) { int read = -1; while (read != 0 && !ct.IsCancellationRequested) { read = await SSLStream.ReadAsync(ReceiveBuffer, 0, BufferSize); OnReceive(ReceiveBuffer, 0, read); } throw new EndOfStreamException(string.Format("Socket closed")); }
void SendQueue_Process(object sender, ProcessQueueEventArgs <Tuple <byte[], int, int> > e) { try { var ar = SSLStream.BeginWrite(e.Item.Item1, e.Item.Item2, e.Item.Item3, null, null); using (ar.AsyncWaitHandle) { if (ar.AsyncWaitHandle.WaitOne(-1)) { SSLStream.EndWrite(ar); } } } catch { } }
public virtual void Disconnect() { try { if (UseSSL) { SSLStream.Close(); } else { NetworkStream.Close(); } TcpClient.Close(); } // ReSharper disable once EmptyGeneralCatchClause catch { // ignore } }
protected void RTMPHandshake() { Random rand = new Random(); byte C0 = 0x03; SSLStream.WriteByte(C0); int TimestampC1 = TimeUtil.EpochTime(); byte[] RandC1 = new byte[1528]; rand.NextBytes(RandC1); SSLStream.Write(BitConverter.GetBytes(TimestampC1)); SSLStream.Write(BitConverter.GetBytes((int)0)); SSLStream.Write(RandC1); SSLStream.Flush(); byte S0 = (byte)SSLStream.ReadByte(); if (S0 != 0x03) { throw new IOException("Server returned incorrect version in handshake: " + S0); } byte[] S1 = new byte[1536]; for (int i = 0; i < 1536; i++) { S1[i] = (byte)SSLStream.ReadByte(); } int TimestampS1 = TimeUtil.EpochTime(); SSLStream.Write(S1, 0, 4); SSLStream.Write(BitConverter.GetBytes(TimestampS1)); SSLStream.Write(S1, 8, 1528); SSLStream.Flush(); byte[] S2 = new byte[1536]; for (int i = 0; i < 1536; i++) { S2[i] = (byte)SSLStream.ReadByte(); } bool Valid = true; for (int i = 8; i < 1536; i++) { if (RandC1[i - 8] != S2[i]) { Valid = false; break; } } if (!Valid) { throw new IOException("Server returned invalid handshake"); } base.StartReading(); }