Ejemplo n.º 1
0
        private void UDPMessageReceived(object source, MessegeEventArgs args)
        {
            byte[] bytes = args.Messege;
            string msg   = Encoding.UTF8.GetString(bytes, 0, bytes.Length);

            Debug.WriteLine("UDP Received: {0}", msg);
            string command    = msg.Substring(0, msg.IndexOf(':'));
            int    startIndex = Encoding.UTF8.GetBytes(command + ": ").Length;

            switch (command)
            {
            case "Queue":
                using (MemoryStream ms = new MemoryStream(bytes, startIndex, bytes.Length - startIndex))
                    using (BsonReader reader = new BsonReader(ms))
                    {
                        //reader.ReadRootValueAsArray = true;
                        JsonSerializer serializer = new JsonSerializer();
                        YoutubeQueued  yt         = serializer.Deserialize <YoutubeQueued>(reader);
                        if (yt.userId == User.DeviceUser.Id)
                        {
                            return;
                        }
                        Debug.WriteLine("UDP/ Adding to Queue {0}", yt.title);
                        OnUDPQueued(yt);
                    }
                break;

            case "Delete":
                int qPosDelete = int.Parse(msg.Substring(msg.IndexOf(':') + 1));
                //to int
                OnDeletepiece(qPosDelete);
                break;

            case "PlayedNow":
                int qPosPlayedNow = BitConverter.ToInt32(bytes, startIndex);
                //int qPosPlayedNow = int.Parse(msg.Substring(msg.IndexOf(':') + 1));
                OnPlayedNow(qPosPlayedNow);
                break;

            default:
                Debug.WriteLine("UDP/ Cannot interpret right...");
                break;
            }
        }
Ejemplo n.º 2
0
        private void OnServerFound(object source, MessegeEventArgs args)
        {
            string messege          = System.Text.Encoding.UTF8.GetString(args.Messege, 0, args.Messege.Length);
            var    ipAddressBuilder = new StringBuilder();

            for (int i = 0; i < 4; i++)
            {
                ipAddressBuilder.Append(args.Messege[i]);
                if (i != 3)
                {
                    ipAddressBuilder.Append('.');
                }
            }
            serverAddress = ipAddressBuilder.ToString();
            //tutaj sprawdzanie czy to ip itd
            int userID = BitConverter.ToInt32(args.Messege, 4);

            User.InitializeDeviceUser(userID);
            OnServerConnected();
            tcpConnector.MessegeReceived -= OnServerFound;
        }