private void receivedPeerNameMessage(PeerNameMessagePacket message, IpEndpoint remoteEndpoint) { var peer = _peers.FirstOrDefault(p => p.Ip == remoteEndpoint.Address && p.Name == message.Name); if (peer != null) { peer.AddTcpConnection(remoteEndpoint.Port); _log.LogInfo($"Known CITP Peer '{peer}' identified on TCP port {remoteEndpoint.Port}"); } else { peer = new CitpPeer(remoteEndpoint.Address, message.Name); peer.AddTcpConnection(remoteEndpoint.Port); _peers.Add(peer); _log.LogInfo($"New CITP Peer '{peer}' identified from on TCP port {remoteEndpoint.Port}"); } peer.Name = message.Name; peer.LastUpdateReceived = DateTime.Now; }
public static CitpPacket FromByteArray(byte[] data, IpEndpoint?remoteEndpoint = null) { CitpPacket packet; var layerType = getLayerType(data); if (layerType == null) { var layerTypeArray = new byte[4]; Buffer.BlockCopy(data, CitpContentTypePosition, layerTypeArray, 0, 4); throw new InvalidOperationException( $"Unrecognised CITP content type: {Encoding.UTF8.GetString(layerTypeArray, 0, layerTypeArray.Length)}"); } switch (layerType) { case CitpLayerType.PeerInformationLayer: { var messageType = PinfPacket.GetMessageType(data); if (messageType == null) { var messageTypeArray = new byte[4]; Buffer.BlockCopy(data, PinfPacket.CitpMessageTypePosition, messageTypeArray, 0, 4); throw new InvalidOperationException( $"Unrecognised PING message type: {Encoding.UTF8.GetString(messageTypeArray, 0, messageTypeArray.Length)}"); } switch (messageType) { case PinfMessageType.PeerLocationMessage: packet = new PeerLocationMessagePacket(); break; case PinfMessageType.PeerNameMessage: packet = new PeerNameMessagePacket(); break; default: throw new NotImplementedException("Unimplemented PINF message type"); } break; } case CitpLayerType.MediaServerExtensionsLayer: { var messageType = MsexPacket.GetMessageType(data); if (messageType == null) { var messageTypeArray = new byte[4]; Buffer.BlockCopy(data, MsexPacket.CitpMessageTypePosition, messageTypeArray, 0, 4); throw new InvalidOperationException( $"Unrecognised MSEX message type: {Encoding.UTF8.GetString(messageTypeArray, 0, messageTypeArray.Length)}"); } switch (messageType) { case MsexMessageType.ClientInformationMessage: packet = new ClientInformationMessagePacket(); break; case MsexMessageType.ServerInformationMessage: packet = new ServerInformationMessagePacket(); break; case MsexMessageType.NegativeAcknowledgeMessage: packet = new NegativeAcknowledgeMessagePacket(); break; case MsexMessageType.LayerStatusMessage: packet = new LayerStatusMessagePacket(); break; case MsexMessageType.GetElementLibraryInformationMessage: packet = new GetElementLibraryInformationMessagePacket(); break; case MsexMessageType.ElementLibraryInformationMessage: packet = new ElementLibraryInformationMessagePacket(); break; case MsexMessageType.ElementLibraryUpdatedMessage: packet = new ElementLibraryUpdatedMessagePacket(); break; case MsexMessageType.GetElementInformationMessage: packet = new GetElementInformationMessagePacket(); break; case MsexMessageType.MediaElementInformationMessage: packet = new MediaElementInformationMessagePacket(); break; case MsexMessageType.EffectElementInformationMessage: packet = new EffectElementInformationMessagePacket(); break; case MsexMessageType.GenericElementInformationMessage: packet = new GenericElementInformationMessagePacket(); break; case MsexMessageType.GetElementLibraryThumbnailMessage: packet = new GetElementLibraryThumbnailMessagePacket(); break; case MsexMessageType.ElementLibraryThumbnailMessage: packet = new ElementLibraryThumbnailMessagePacket(); break; case MsexMessageType.GetElementThumbnailMessage: packet = new GetElementThumbnailMessagePacket(); break; case MsexMessageType.ElementThumbnailMessage: packet = new ElementThumbnailMessagePacket(); break; case MsexMessageType.GetVideoSourcesMessage: packet = new GetVideoSourcesMessagePacket(); break; case MsexMessageType.RequestStreamMessage: packet = new RequestStreamMessagePacket(); break; case MsexMessageType.StreamFrameMessage: packet = new StreamFrameMessagePacket(); break; default: throw new NotImplementedException("Unimplemented MSEX message type"); } break; } default: throw new NotImplementedException("Unimplemented CITP content type"); } using (var reader = new CitpBinaryReader(new MemoryStream(data))) packet.DeserializeFromStream(reader); packet.RemoteEndpoint = remoteEndpoint; return(packet); }