Beispiel #1
0
        public async Task SendMessage(CastMessage message, bool includeHeader = true)
        {
            if (this.IsDisposed)
            {
                throw new ObjectDisposedException(this.GetType().Name);
            }

            if (!this.client.IsConnected)
            {
                throw new InvalidOperationException("CastClient must be connected to device before call this method.");
            }

#if DEBUG
            Debug.WriteLine(string.Format("OUT: {0}", message.PayloadUtf8));
#endif
            using (var stream = new MemoryStream())
            {
                Serializer.Serialize(stream, message);
                var binaryMessage = stream.ToArray();
                if (includeHeader)
                {
                    var header  = BitConverter.GetBytes((UInt32)binaryMessage.Length).Reverse().ToArray();
                    var allData = new byte[header.Length + binaryMessage.Length];
                    header.CopyTo(allData, 0);
                    binaryMessage.CopyTo(allData, header.Length);
                    await client.Write(allData);
                }
                else
                {
                    await client.Write(binaryMessage);
                }
            }
        }
Beispiel #2
0
 private void ReceivedMessage(CastMessage castMessage)
 {
     foreach (var channel in Channels.Where(i => i.Namespace == castMessage.Namespace))
     {
         channel.OnMessageReceived(new ChromecastSSLClientDataReceivedArgs(castMessage));
     }
 }
Beispiel #3
0
 private void ReceivedMessage(CastMessage castMessage)
 {
     foreach (var channel in Channels.Where(i => i.Namespace == castMessage.Namespace))
     {
         channel.OnMessageReceived(castMessage);
     }
 }
        /// <summary>
        /// Handle a message from the device.
        /// </summary>
        /// <param name="castMessage">the received message</param>
        public void OnReceiveMessage(CastMessage castMessage)
        {
            if (castMessage == null || device == null || IsDisposed)
            {
                return;
            }

            lastReceivedMessage = DateTime.Now;
            logger.Log($"{Properties.Strings.Log_In} [{DateTime.Now.ToLongTimeString()}] [{device.GetHost()}:{device.GetPort()}] [{device.GetDeviceState()}]: {castMessage.PayloadUtf8}");
            var js = new JavaScriptSerializer();

            var message = new JavaScriptSerializer().Deserialize <PayloadMessageBase>(castMessage.PayloadUtf8);

            if (message.type != "PING" && message.type != "PONG")
            {
                pendingStatusMessage = false;
            }

            switch (message.@type)
            {
            case "RECEIVER_STATUS":
                OnReceiveReceiverStatus(js.Deserialize <MessageReceiverStatus>(castMessage.PayloadUtf8));
                break;

            case "MEDIA_STATUS":
                OnReceiveMediaStatus(js.Deserialize <MessageMediaStatus>(castMessage.PayloadUtf8));
                break;

            case "PING":
                Pong();
                break;

            case "PONG":
                break;

            case "CLOSE":
                OnReceiveCloseMessage();
                break;

            case "LOAD_FAILED":
                device.SetDeviceState(DeviceState.LoadFailed, null);
                break;

            case "LOAD_CANCELLED":
                device.SetDeviceState(DeviceState.LoadCancelled, null);
                break;

            case "INVALID_REQUEST":
                device.SetDeviceState(DeviceState.InvalidRequest, null);
                break;

            case "LAUNCH_ERROR":
                device.SetDeviceState(DeviceState.LoadCancelled, null);
                break;

            default:
                break;
            }
        }
        public void SendMessage(CastMessage castMessage)
        {
            var byteMessage = chromeCastMessages.MessageToByteArray(castMessage);

            sendMessage?.Invoke(byteMessage);

            logger.Log($"{Properties.Strings.Log_Out} [{DateTime.Now.ToLongTimeString()}][{getHost?.Invoke()}:{getPort?.Invoke()}] [{getDeviceState()}]: {castMessage.PayloadUtf8}");
        }
Beispiel #6
0
 private void ProcessMessage(ArraySegment <byte> message)
 {
     using (var ms = new MemoryStream(message.Array, message.Offset, message.Count))
     {
         var castMessage = CastMessage.ParseFrom(ms);
         MessageReceived?.Invoke(castMessage);
     }
 }
 internal override void OnMessageReceived(CastMessage castMessage)
 {
     if (castMessage.GetJsonType() == "CLOSE")
     {
         Task.Run(() => closeReceived.Cancel());
     }
     ;
 }
Beispiel #8
0
        public void SendMessage(CastMessage castMessage)
        {
            var byteMessage = chromeCastMessages.MessageToByteArray(castMessage);

            sendMessage?.Invoke(byteMessage);

            logger.Log(string.Format("out [{2}][{0}]: {1}", getHost?.Invoke(), castMessage.PayloadUtf8, DateTime.Now.ToLongTimeString()));
        }
Beispiel #9
0
        private async Task SendMessageAndWaitResponse <T>(CastMessage <T> message, int millisecondsTimeout = 5000, bool includeHeader = true) where T : MessageRequestWithId
        {
            this.barriers.Add(message.Payload.RequestId, new Barrier(2));
            await this.SubscribedClient.SendMessage(message, includeHeader);

            this.barriers[message.Payload.RequestId].SignalAndWait(millisecondsTimeout);
            this.barriers.Remove(message.Payload.RequestId);
        }
 public async Task Write(CastMessage message, bool includeNameSpace = true)
 {
     if (includeNameSpace)
     {
         message.Namespace = Namespace;
     }
     var bytes = message.ToProto();
     await Client.ChromecastSocketService.Write(bytes, Client.CancellationTokenSource.Token);
 }
        /// <summary>
        /// Callback for the receive buffer, a complete message is received.
        /// </summary>
        /// <param name="castMessage">the message that's received</param>
        private void OnReceiveMessage(CastMessage castMessage)
        {
            if (IsDisposed)
            {
                return;
            }

            onReceiveMessage?.Invoke(castMessage);
        }
Beispiel #12
0
 internal override void OnMessageReceived(CastMessage castMessage)
 {
     Trace.WriteLine(castMessage.GetJsonType());
     if (Client.Connected || castMessage.GetJsonType() != "PONG")
     {
         return;
     }
     Client.Connected = true;
 }
Beispiel #13
0
 public async Task Write(CastMessage message, CancellationToken token, bool includeNameSpace = true)
 {
     if (includeNameSpace)
     {
         message.Namespace = Namespace;
     }
     var bytes = message.ToProto();
     await Client.ChromecastSocketService.Write(bytes, token).ConfigureAwait(false);
 }
Beispiel #14
0
        /// <summary>
        /// A message from the device is received.
        /// </summary>
        public void OnReceiveMessage(CastMessage castMessage)
        {
            if (deviceCommunication == null)
            {
                return;
            }

            deviceCommunication.OnReceiveMessage(castMessage);
        }
        public void OnReceiveMessage(CastMessage castMessage)
        {
            logger.Log($"{Properties.Strings.Log_In} [{DateTime.Now.ToLongTimeString()}] [{getHost?.Invoke()}:{getPort?.Invoke()}] [{getDeviceState()}]: {castMessage.PayloadUtf8}");
            var js = new JavaScriptSerializer();

            var message = new JavaScriptSerializer().Deserialize <PayloadMessageBase>(castMessage.PayloadUtf8);

            switch (message.@type)
            {
            case "RECEIVER_STATUS":
                OnReceiveReceiverStatus(js.Deserialize <MessageReceiverStatus>(castMessage.PayloadUtf8));
                break;

            case "MEDIA_STATUS":
                OnReceiveMediaStatus(js.Deserialize <MessageMediaStatus>(castMessage.PayloadUtf8));
                break;

            case "PING":
                Pong();
                break;

            case "PONG":
                var pongMessage = js.Deserialize <PayloadMessageBase>(castMessage.PayloadUtf8);
                break;

            case "CLOSE":
                var previousState = getDeviceState();
                var closeMessage  = js.Deserialize <PayloadMessageBase>(castMessage.PayloadUtf8);
                OnReceiveCloseMessage(closeMessage);
                break;

            case "LOAD_FAILED":
                var loadFailedMessage = js.Deserialize <MessageLoadFailed>(castMessage.PayloadUtf8);
                setDeviceState(DeviceState.LoadFailed, null);
                break;

            case "LOAD_CANCELLED":
                var loadCancelledMessage = js.Deserialize <MessageLoadCancelled>(castMessage.PayloadUtf8);
                setDeviceState(DeviceState.LoadCancelled, null);
                break;

            case "INVALID_REQUEST":
                var invalidRequestMessage = js.Deserialize <PayloadMessageBase>(castMessage.PayloadUtf8);
                setDeviceState(DeviceState.InvalidRequest, null);
                break;

            case "LAUNCH_ERROR":
                setDeviceState(DeviceState.LoadCancelled, null);
                break;

            default:
                break;
            }
        }
Beispiel #16
0
 public void SendMessage(string ns, ByteString byteString, string destinationId)
 {
     SendMessage(CastMessage.CreateBuilder()
                 .SetProtocolVersion(CastMessage.Types.ProtocolVersion.CASTV2_1_0)
                 .SetSourceId(_sourceId)
                 .SetDestinationId(destinationId)
                 .SetNamespace(ns)
                 .SetPayloadType(CastMessage.Types.PayloadType.BINARY)
                 .SetPayloadBinary(byteString)
                 .Build());
 }
Beispiel #17
0
        /// <summary>
        /// Process a message.
        /// </summary>
        /// <param name="message">the message</param>
        private void ProcessMessage(byte[] message)
        {
            if (message == null)
            {
                return;
            }

            var castMessage = CastMessage.ParseFrom(message);

            onReceiveMessage?.Invoke(castMessage);
        }
Beispiel #18
0
        private void SendMessage(CastMessage message)
        {
            byte[] bytes = BitConverter.GetBytes(message.SerializedSize);
            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(bytes);
            }

            _sslStream.Write(bytes);
            message.WriteTo(_sslStream);
        }
Beispiel #19
0
 public void SendMessage(string ns, Message command, string destinationId)
 {
     SendMessage(CastMessage.CreateBuilder()
                 .SetProtocolVersion(CastMessage.Types.ProtocolVersion.CASTV2_1_0)
                 .SetSourceId(_sourceId)
                 .SetDestinationId(destinationId)
                 .SetNamespace(ns)
                 .SetPayloadType(CastMessage.Types.PayloadType.STRING)
                 .SetPayloadUtf8(JsonConvert.SerializeObject(command))
                 .Build());
 }
Beispiel #20
0
        public void Write(CastMessage message, DeviceState state)
        {
            if (Disposed)
            {
                return;
            }

            logger.Log($"out [{DateTime.Now.ToLongTimeString()}] [{state}] [{ipAddress}:{port}] {message.PayloadUtf8}");
            var byteArray = ChromeCastMessages.MessageToByteArray(message);

            sslStream.BeginWrite(byteArray, 0, byteArray.Length, WriteAsyncCallback, sslStream);
        }
        private async void Connection_MessageReceived(EndpointConnection con, CastMessage castMessage)
        {
            var message = JsonConvert.DeserializeObject <PayloadMessageBase>(castMessage.PayloadUtf8);

            switch (message.@type)
            {
            case "RECEIVER_STATUS":
                OnReceiveReceiverStatus(JsonConvert.DeserializeObject <MessageReceiverStatus>(castMessage.PayloadUtf8));
                break;

            case "MEDIA_STATUS":
                OnReceiveMediaStatus(JsonConvert.DeserializeObject <MessageMediaStatus>(castMessage.PayloadUtf8));
                break;

            case "PING":
                await PongAsync();

                break;

            case "PONG":
                var pongMessage = JsonConvert.DeserializeObject <PayloadMessageBase>(castMessage.PayloadUtf8);
                break;

            case "CLOSE":
                var closeMessage = JsonConvert.DeserializeObject <PayloadMessageBase>(castMessage.PayloadUtf8);
                DeviceState = DeviceState.Closed;
                //if (applicationLogic.GetAutoRestart())
                //{
                //    await Task.Delay(5000);
                //    OnClickDeviceButton(DeviceState.Closed);
                //}
                break;

            case "LOAD_FAILED":
                var loadFailedMessage = JsonConvert.DeserializeObject <MessageLoadFailed>(castMessage.PayloadUtf8);
                DeviceState = DeviceState.LoadFailed;
                break;

            case "LOAD_CANCELLED":
                var loadCancelledMessage = JsonConvert.DeserializeObject <MessageLoadCancelled>(castMessage.PayloadUtf8);
                DeviceState = DeviceState.LoadCancelled;
                break;

            case "INVALID_REQUEST":
                var invalidRequestMessage = JsonConvert.DeserializeObject <PayloadMessageBase>(castMessage.PayloadUtf8);
                DeviceState = DeviceState.InvalidRequest;
                break;

            default:
                break;
            }
        }
Beispiel #22
0
        /// <summary>
        /// Do send a message.
        /// </summary>
        /// <param name="castMessage">the message to send</param>
        public void SendMessage(CastMessage castMessage)
        {
            if (chromeCastMessages == null || IsDisposed)
            {
                return;
            }

            var byteMessage = chromeCastMessages.MessageToByteArray(castMessage);

            sendMessage?.Invoke(byteMessage);

            logger.Log($"{Properties.Strings.Log_Out} [{DateTime.Now.ToLongTimeString()}][{device.GetHost()}:{device.GetPort()}] [{device.GetDeviceState()}]: {castMessage.PayloadUtf8}");
        }
        internal override void OnMessageReceived(CastMessage castMessage)
        {
            var json     = castMessage.PayloadUtf8;
            var response = JsonConvert.DeserializeObject <ChromecastStatusResponse>(json);

            if (response.RequestId != 0)
            {
                if (TryRemoveRequestTracking(response.RequestId, out var completed))
                {
                    completed.SetResult(response.ChromecastStatus);
                }
            }
        }
        public byte[] MessageToByteArray(CastMessage message)
        {
            var messageStream = new MemoryStream();

            message.WriteTo(messageStream);
            var bufMsg = messageStream.ToArray();

            var bufLen = new byte[4];

            bufLen = BitConverter.GetBytes(bufMsg.Length);
            bufLen = bufLen.Reverse().ToArray();

            return(bufLen.Concat(bufMsg).ToArray());
        }
Beispiel #25
0
        public void OnReceiveMessage(CastMessage castMessage)
        {
            logger.Log(string.Format("in [{2}] [{0}]: {1}", getHost?.Invoke(), castMessage.PayloadUtf8, DateTime.Now.ToLongTimeString()));
            var js = new JavaScriptSerializer();

            var message = new JavaScriptSerializer().Deserialize <PayloadMessageBase>(castMessage.PayloadUtf8);

            switch (message.@type)
            {
            case "RECEIVER_STATUS":
                OnReceiveReceiverStatus(js.Deserialize <MessageReceiverStatus>(castMessage.PayloadUtf8));
                break;

            case "MEDIA_STATUS":
                OnReceiveMediaStatus(js.Deserialize <MessageMediaStatus>(castMessage.PayloadUtf8));
                break;

            case "PING":
                Pong();
                break;

            case "PONG":
                var pongMessage = js.Deserialize <PayloadMessageBase>(castMessage.PayloadUtf8);
                break;

            case "CLOSE":
                var closeMessage = js.Deserialize <PayloadMessageBase>(castMessage.PayloadUtf8);
                setDeviceState(DeviceState.Closed, null);
                break;

            case "LOAD_FAILED":
                var loadFailedMessage = js.Deserialize <MessageLoadFailed>(castMessage.PayloadUtf8);
                setDeviceState(DeviceState.LoadFailed, null);
                break;

            case "LOAD_CANCELLED":
                var loadCancelledMessage = js.Deserialize <MessageLoadCancelled>(castMessage.PayloadUtf8);
                setDeviceState(DeviceState.LoadCancelled, null);
                break;

            case "INVALID_REQUEST":
                var invalidRequestMessage = js.Deserialize <PayloadMessageBase>(castMessage.PayloadUtf8);
                setDeviceState(DeviceState.InvalidRequest, null);
                break;

            default:
                break;
            }
        }
 private static void FillUserInfo(StickMan_Users user, CastMessage message)
 {
     message.User = new UserModel
     {
         UserId    = user.UserID,
         ImagePath = user.ImagePath,
         UserName  = user.UserName,
         DOB       = user.DOB,
         DeviceId  = user.DeviceId,
         Email     = user.EmailID,
         FullName  = user.FullName,
         MobileNo  = user.MobileNo,
         Sex       = user.Sex
     };
 }
Beispiel #27
0
 protected override void OnMessageReceived(byte[] data, CastMessage message)
 {
     if (message.PayloadUtf8.Contains("MEDIA_STATUS"))
     {
         var mediaStatusRequest = ((CastMessage <Messaging.StandardRequests.MediaStatus>)message).Payload;
         this.CurrentMediaStatus = mediaStatusRequest.Status;
         if (barriers.ContainsKey(mediaStatusRequest.RequestId))
         {
             barriers[mediaStatusRequest.RequestId].SignalAndWait(1000);
         }
         if (this.MediaStatusChanged != null)
         {
             this.MediaStatusChanged(this, new MediaStatusChangedEventArgs(this.CurrentMediaStatus));
         }
     }
 }
Beispiel #28
0
 protected override async void OnMessageReceived(byte[] data, CastMessage message)
 {
     if (message.PayloadUtf8.Contains("RECEIVER_STATUS"))
     {
         var receiverStatusMessage = (CastMessage <Messaging.StandardRequests.ReceiverStatus>)message;
         this.CurrentReceiverStatus = receiverStatusMessage.Payload.Status;
         if (this.launchRequestId == receiverStatusMessage.Payload.RequestId)
         {
             this.autoStartSessionBarrier.SignalAndWait(500);
         }
         if (this.ReceiverStatusChanged != null)
         {
             this.ReceiverStatusChanged(this, new ReceiverStatusChangedEventArgs(this.CurrentReceiverStatus));
         }
     }
 }
Beispiel #29
0
        /// <summary>
        /// Process a message.
        /// </summary>
        /// <param name="message">the message</param>
        private void ProcessMessage(byte[] message)
        {
            if (message == null)
            {
                return;
            }

            try
            {
                var castMessage = CastMessage.ParseFrom(message);
                onReceiveMessage?.Invoke(castMessage);
            }
            catch (Exception)
            {
            }
        }
Beispiel #30
0
 internal override void OnMessageReceived(CastMessage castMessage)
 {
     if (castMessage.GetJsonType() == "CLOSE")
     {
         Task.Run(async() =>
         {
             try
             {
                 await Client.Abort().ConfigureAwait(false);
             }
             catch (ObjectDisposedException)
             { }
         });
     }
     ;
 }
Beispiel #31
0
 public override NotifyResult notified(EditorComponent from, CastMessage msg)
 {
     return NotifyResult.Succeed;
 }