コード例 #1
0
        public async Task Write(CastMessage message)
        {
            message.Namespace = this.Namespace;

            var bytes = CastHelper.ToProto(message);
            await _client.Write(bytes);
        }
コード例 #2
0
        public async Task Write(CastMessage message)
        {
            var bytes = CastHelper.ToProto(message);

            await Write(bytes);
        }
コード例 #3
0
        private void ReadPacket()
        {
            try
            {
                List <byte> header = new List <byte>();
                List <byte> data   = new List <byte>();

                byte[] buffer = new byte[2048];

                var escape = true;

                while (escape)
                {
                    // tricky byte order for messages
                    var bytesRead = _stream.Read(buffer, 0, buffer.Length);
                    if (bytesRead == 1)
                    {
                        // Incoming series of header /data
                        header.Add(buffer[0]);

                        bytesRead = _stream.Read(buffer, 0, buffer.Length);
                        if (bytesRead == 3)
                        {
                            header.Add(buffer[0]);
                            header.Add(buffer[1]);
                            header.Add(buffer[2]);

                            bytesRead = _stream.Read(buffer, 0, buffer.Length);
                            if (bytesRead == 1)
                            {
                                header.Add(buffer[0]);
                                bytesRead = _stream.Read(buffer, 0, buffer.Length);
                                data.AddRange(buffer.Take(bytesRead));
                                escape = false;
                            }
                            else
                            {
                                escape = false;
                            }
                        }
                        else
                        {
                            escape = false;
                        }
                    }
                    else
                    {
                        escape = false;
                    }
                }

                var entireMessage = new List <byte>();
                entireMessage.AddRange(header);
                entireMessage.AddRange(data);

                header = null;
                data   = null;

                try
                {
                    var entireMessageArray = entireMessage.ToArray();
                    var castMessage        = CastHelper.ToCastMessage(entireMessageArray);
                    if (castMessage != null)
                    {
                        OnMessageReceived(new ChromecastSSLClientDataReceivedArgs(entireMessageArray, castMessage));

                        // Check if a channel exists that this message needs to go to
                        if (!String.IsNullOrEmpty(castMessage.Namespace))
                        {
                            foreach (var channel in this.Channels.Where(i => i.Namespace == castMessage.Namespace))
                            {
                                channel.OnMessageReceived(new ChromecastSSLClientDataReceivedArgs(entireMessageArray, castMessage));
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    // Log these bytes?
                    Console.WriteLine(ex);
                }
            }
            catch (Exception ex)
            {
                // Log these bytes?
                Console.WriteLine(ex);
            }
        }