public RtmpConnect(Socket client_socket, Stream stream, RtmpServer server, ushort client_id, SerializationContext context, ObjectEncoding objectEncoding = ObjectEncoding.Amf0, bool asyncMode = false) { ClientId = client_id; clientSocket = client_socket; this.server = server; this.objectEncoding = objectEncoding; writer = new RtmpPacketWriter(new AmfWriter(stream, context, ObjectEncoding.Amf0, asyncMode), ObjectEncoding.Amf0); reader = new RtmpPacketReader(new AmfReader(stream, context, asyncMode)); reader.EventReceived += EventReceivedCallback; reader.Disconnected += OnPacketProcessorDisconnected; writer.Disconnected += OnPacketProcessorDisconnected; callbackManager = new TaskCallbackManager <int, object>(); }
public void EstablishThreads(Stream stream) { this._writer = new RtmpPacketWriter(new AmfWriter(stream, this._serializationContext), ObjectEncoding.Amf3); this._reader = new RtmpPacketReader(new AmfReader(stream, this._serializationContext)); this._reader.EventReceived += new EventHandler <EventReceivedEventArgs>(this.EventReceivedCallback); this._reader.Disconnected += new EventHandler <ExceptionalEventArgs>(this.OnPacketProcessorDisconnected); this._writer.Disconnected += new EventHandler <ExceptionalEventArgs>(this.OnPacketProcessorDisconnected); this._writerThread = new Thread(new ThreadStart(this._reader.ReadLoop)) { IsBackground = true }; this._readerThread = new Thread(new ThreadStart(this._writer.WriteLoop)) { IsBackground = true }; this._writerThread.Start(); this._readerThread.Start(); }
public void EstablishThreads(Stream stream) { writer = new RtmpPacketWriter(new AmfWriter(stream, serializationContext), ObjectEncoding.Amf3); reader = new RtmpPacketReader(new AmfReader(stream, serializationContext)); reader.EventReceived += EventReceivedCallback; reader.Disconnected += OnPacketProcessorDisconnected; writer.Disconnected += OnPacketProcessorDisconnected; writerThread = new Thread(reader.ReadLoop) { IsBackground = true }; readerThread = new Thread(writer.WriteLoop) { IsBackground = true }; writerThread.Start(); readerThread.Start(); }
private void WriteMessageHeader(RtmpHeader header, RtmpHeader previousHeader) { ChunkMessageHeaderType messageHeaderType = RtmpPacketWriter.GetMessageHeaderType(header, previousHeader); this.WriteBasicHeader(messageHeaderType, header.StreamId); int num = header.Timestamp < 16777215 ? header.Timestamp : 16777215; switch (messageHeaderType) { case ChunkMessageHeaderType.New: this._writer.WriteUInt24(num); this._writer.WriteUInt24(header.PacketLength); this._writer.WriteByte((byte)header.MessageType); this._writer.WriteReverseInt(header.MessageStreamId); goto case ChunkMessageHeaderType.Continuation; case ChunkMessageHeaderType.SameSource: this._writer.WriteUInt24(num); this._writer.WriteUInt24(header.PacketLength); this._writer.WriteByte((byte)header.MessageType); goto case ChunkMessageHeaderType.Continuation; case ChunkMessageHeaderType.TimestampAdjustment: this._writer.WriteUInt24(num); goto case ChunkMessageHeaderType.Continuation; case ChunkMessageHeaderType.Continuation: if (num < 16777215) { break; } this._writer.WriteInt32(header.Timestamp); break; default: throw new ArgumentException("headerType"); } }
public async Task ConnectAsync() { if (hasConnected) { return; } var client = CreateTcpClient(); client.NoDelay = NoDelay; client.ReceiveTimeout = ReceiveTimeout; client.SendTimeout = SendTimeout; client.ExclusiveAddressUse = ExclusiveAddressUse; await client.ConnectAsync(uri.Host, uri.Port); var stream = await GetRtmpStreamAsync(client); var random = new Random(); var randomBytes = new byte[1528]; random.NextBytes(randomBytes); // write c0+c1 var c01 = new Handshake() { Version = 3, Time = (uint)Environment.TickCount, Time2 = 0, Random = randomBytes }; await Handshake.WriteAsync(stream, c01, true); // read s0+s1 var s01 = await Handshake.ReadAsync(stream, true); // write c2 var c2 = s01.Clone(); c2.Time2 = (uint)Environment.TickCount; await Handshake.WriteAsync(stream, c2, false); // read s2 var s2 = await Handshake.ReadAsync(stream, false); // handshake check if (!c01.Random.SequenceEqual(s2.Random) || c01.Time != s2.Time) { throw new ProtocolViolationException(); } writer = new RtmpPacketWriter(new AmfWriter(stream, context), ObjectEncoding.Amf3); reader = new RtmpPacketReader(new AmfReader(stream, context)); reader.EventReceived += EventReceivedCallback; reader.Disconnected += OnPacketProcessorDisconnected; writer.Disconnected += OnPacketProcessorDisconnected; writerThread = new Thread(reader.ReadLoop) { IsBackground = true }; readerThread = new Thread(writer.WriteLoop) { IsBackground = true }; writerThread.Start(); readerThread.Start(); // call `connect` var connectResult = await ConnectInvokeAsync(null, null, uri.ToString()); object cId; if (connectResult.TryGetValue("clientId", out cId)) { ClientId = cId as string; } hasConnected = true; }