public void Update() { if (tcpClient.Available != 0) { var buffer = new byte[tcpClient.Available]; tcpClient.GetStream().Read(buffer, 0, buffer.Length); if (CurrentState == ClientStates.Handshaking) { if (sMemory.Length < HANDSHAKE_RAND_LENGTH * 2) { sWriter.Write(buffer); if (sMemory.Length >= HANDSHAKE_RAND_LENGTH * 2) { ParseS1Handshake(); } } else { ParseS1Handshake(); } } else { var memory = new MemoryStream(buffer); var reader = new EndianBinaryReader(EndianBitConverter.Big, memory); while (memory.Position < memory.Length) { reader.ReadBytes(4); //as of now not used data var bodySizeBytes = new byte[] { 0, reader.ReadByte(), reader.ReadByte(), reader.ReadByte() }; var bodySize = converter.ToUInt32(bodySizeBytes, 0); var messageId = (RtmpMessageTypeId)reader.ReadByte(); reader.ReadInt32(); //stream id is not needed as of now switch (messageId) { case RtmpMessageTypeId.SetChunkSize: { ParseSetChunkSize(reader.ReadInt32()); } break; case RtmpMessageTypeId.UserControlMessage: { //No fawking clue why it's six bytes atm ParseUserControlMessage(reader.ReadBytes(6)); if (CurrentState == ClientStates.WaitForStreamBeginControl) { //Console.WriteLine("SWITCh3"); CurrentState = ClientStates.WaitForConnectResult; } if (CurrentState == ClientStates.WaitForPublishStreamBeginResult) { //Console.WriteLine("Switch6"); CurrentState = ClientStates.Streaming; SendChunkSize(100); } } break; case RtmpMessageTypeId.ServerBandwidth: { if (CurrentState == ClientStates.WaitingForAcknowledge) { //Console.WriteLine("SWITCH1"); SendWindowAcknowledgementSize(); CurrentState = ClientStates.WaitForPeerBandwidth; } ParseServerBandwidth(reader.ReadInt32()); } break; case RtmpMessageTypeId.ClientBandwitdh: { if (CurrentState == ClientStates.WaitForPeerBandwidth) { //Console.WriteLine("SWITCh2"); CurrentState = ClientStates.WaitForStreamBeginControl; } ParseClientBandwidth(reader.ReadInt32(), reader.ReadByte()); } break; case RtmpMessageTypeId.Audio: break; case RtmpMessageTypeId.Video: break; case RtmpMessageTypeId.AMF3: break; case RtmpMessageTypeId.Invoke: break; case RtmpMessageTypeId.AMF0: { var amfReader = new AmfReader(); amfReader.Parse(reader, bodySize); if (CurrentState == ClientStates.WaitForConnectResult) { if (amfReader.amfData.Strings.Contains("_result")) { //Console.WriteLine("SWITCH4"); createStream(); CurrentState = ClientStates.WaitForCreateStreamResponse; } } if (CurrentState == ClientStates.WaitForCreateStreamResponse) { if (amfReader.amfData.Strings.Contains("_result")) { //Console.WriteLine("SWITCH5"); publish(PublisherId); CurrentState = ClientStates.WaitForPublishStreamBeginResult; } } ParseAmf(amfReader.amfData); } break; case RtmpMessageTypeId.Acknowledgement: ParseAcknowledgement(reader.ReadInt32()); break; default: Console.WriteLine(messageId); break; } ParseMessage(messageId, reader); } } } }
public void Update() { if(tcpClient.Available != 0) { var buffer = new byte[tcpClient.Available]; tcpClient.GetStream().Read(buffer, 0, buffer.Length); if(CurrentState == ClientStates.Handshaking) { if(sMemory.Length < HANDSHAKE_RAND_LENGTH * 2) { sWriter.Write(buffer); if(sMemory.Length >= HANDSHAKE_RAND_LENGTH * 2) { ParseS1Handshake(); } } else { ParseS1Handshake(); } } else { var memory = new MemoryStream(buffer); var reader = new EndianBinaryReader(EndianBitConverter.Big, memory); while (memory.Position < memory.Length) { reader.ReadBytes(4); //as of now not used data var bodySizeBytes = new byte[] {0, reader.ReadByte(), reader.ReadByte(), reader.ReadByte()}; var bodySize = converter.ToUInt32(bodySizeBytes, 0); var messageId = (RtmpMessageTypeId) reader.ReadByte(); reader.ReadInt32(); //stream id is not needed as of now switch (messageId) { case RtmpMessageTypeId.SetChunkSize: { ParseSetChunkSize(reader.ReadInt32()); } break; case RtmpMessageTypeId.UserControlMessage: { //No fawking clue why it's six bytes atm ParseUserControlMessage(reader.ReadBytes(6)); if(CurrentState == ClientStates.WaitForStreamBeginControl) { //Console.WriteLine("SWITCh3"); CurrentState = ClientStates.WaitForConnectResult; } if(CurrentState == ClientStates.WaitForPublishStreamBeginResult) { //Console.WriteLine("Switch6"); CurrentState = ClientStates.Streaming; SendChunkSize(100); } } break; case RtmpMessageTypeId.ServerBandwidth: { if(CurrentState == ClientStates.WaitingForAcknowledge) { //Console.WriteLine("SWITCH1"); SendWindowAcknowledgementSize(); CurrentState = ClientStates.WaitForPeerBandwidth; } ParseServerBandwidth(reader.ReadInt32()); } break; case RtmpMessageTypeId.ClientBandwitdh: { if(CurrentState == ClientStates.WaitForPeerBandwidth) { //Console.WriteLine("SWITCh2"); CurrentState = ClientStates.WaitForStreamBeginControl; } ParseClientBandwidth(reader.ReadInt32(), reader.ReadByte()); } break; case RtmpMessageTypeId.Audio: break; case RtmpMessageTypeId.Video: break; case RtmpMessageTypeId.AMF3: break; case RtmpMessageTypeId.Invoke: break; case RtmpMessageTypeId.AMF0: { var amfReader = new AmfReader(); amfReader.Parse(reader, bodySize); if(CurrentState == ClientStates.WaitForConnectResult) { if (amfReader.amfData.Strings.Contains("_result")) { //Console.WriteLine("SWITCH4"); createStream(); CurrentState = ClientStates.WaitForCreateStreamResponse; } } if(CurrentState == ClientStates.WaitForCreateStreamResponse) { if (amfReader.amfData.Strings.Contains("_result")) { //Console.WriteLine("SWITCH5"); publish(PublisherId); CurrentState = ClientStates.WaitForPublishStreamBeginResult; } } ParseAmf(amfReader.amfData); } break; case RtmpMessageTypeId.Acknowledgement: ParseAcknowledgement(reader.ReadInt32()); break; default: Console.WriteLine(messageId); break; } ParseMessage(messageId, reader); } } } }