示例#1
0
        private void Coordinator_PacketReceived(object sender, Core.Events.PacketFullyReceivedEventArgs e)
        {
            FyzrPacket packet = FyzrParser.FromData(e.Packet.data.ToArray());

            if (packet.method == FyzrPacket.Method.COMMAND)
            {
                string cmdType = packet.headers["Command-Type"];
                if (cmdType.Equals("usersList"))
                {
                    users.Clear();
                    Encoding enc = Encoding.GetEncoding(packet.headers["Content-Encoding"]);
                    if (packet.body != null)
                    {
                        string body = enc.GetString(packet.body);



                        string[] lines = body.Split('\n');
                        foreach (string line in lines)
                        {
                            string[] splitted = line.Trim().Split('#');
                            string[] endPoint = splitted[1].Split(':');
                            users.Add(new User()
                            {
                                Username = splitted[0], Endpoint = new IPEndPoint(IPAddress.Parse(endPoint[0]), Convert.ToInt32(endPoint[1]))
                            });
                        }

                        var handler = UsersListReceived;
                        if (handler != null)
                        {
                            handler(this, new UsersListReceivedEventArgs());
                        }
                    }
                    else
                    {
                        var handler = UsersListReceived;
                        if (handler != null)
                        {
                            handler(this, new UsersListReceivedEventArgs());
                        }
                    }

                    if (requestThread == null)
                    {
                        requestThread = new Thread(RunRequestThread);
                        requestThread.Start();
                    }
                }
            }
        }
示例#2
0
        private void Client_PacketReceived(object sender, Core.Events.PacketFullyReceivedEventArgs e)
        {
            FyzrPacket packet   = FyzrParser.FromData(e.Packet.data.ToArray());
            Client     client   = sender as Client;
            string     username = packet.headers["Username"];

            if (!chats.ContainsKey(client))
            {
                Application.Current.Dispatcher.Invoke(() =>
                {
                    ChatWindow chatWindow = new ChatWindow();
                    chatWindow.SetTitle(username);
                    chatWindow.ShowWindow();
                    chats.Add(client, chatWindow);
                });
            }
            if (packet.method == FyzrPacket.Method.TEXT)
            {
                Encoding enc = Encoding.GetEncoding(packet.headers["Content-Encoding"]);

                string      message = enc.GetString(packet.body);
                TextMessage msg     = new TextMessage()
                {
                    Side = MessageSide.You,
                    Text = message
                };
                chats[client].AddTextMessage(msg);
            }
            else if (packet.method == FyzrPacket.Method.FILE)
            {
                lock (lockk)
                {
                    string fileName = packet.headers["Filename"];

                    long segmentLength = Convert.ToInt64(packet.headers["Content-Length"]);
                    long segmentCount  = Convert.ToInt64(packet.headers["Segment"]);
                    long totalSegments = Convert.ToInt64(packet.headers["Total-Segments"]);
                    long totalLength   = Convert.ToInt64(packet.headers["Total-Length"]);

                    long pos = Convert.ToInt64(packet.headers["Position"]);


                    string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments) + @"\TestReceived";
                    System.IO.Directory.CreateDirectory(path);
                    path += @"\" + fileName;

                    string fileType = packet.headers["File-Type"];
                    if (fileType.Equals("Image"))
                    {
                        if (!imageFilesMap.ContainsKey(client))
                        {
                            imageFilesMap.Add(client, new List <ImageFile>());
                        }

                        ImageFile imageFile = imageFilesMap[client].FirstOrDefault(x => x.fileName.Equals(fileName));
                        if (imageFile == null)
                        {
                            imageFile = new ImageFile()
                            {
                                fileName      = fileName,
                                segmentsCount = 0,
                                sizeCount     = 0,
                                stream        = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None),
                                imageMessage  = chats[client].CreateImageMessage(fileName)
                            };
                            imageFile.stream.SetLength(totalLength);
                            imageFilesMap[client].Add(imageFile);
                        }

                        imageFile.segmentsCount++;
                        imageFile.sizeCount += segmentLength;

                        imageFile.stream.Seek(pos, SeekOrigin.Begin);

                        imageFile.stream.Write(packet.body, 0, packet.body.Length);

                        if (imageFile.segmentsCount == totalSegments)
                        {
                            imageFile.stream.Flush();
                            imageFile.stream.Close();
                            imageFile.stream = null;
                            imageFile.imageMessage.ImageUrl = path;
                            Console.WriteLine("Done file");
                            imageFilesMap[client].Remove(imageFile);
                        }

                        imageFile.imageMessage.Loading  = ((double)imageFile.segmentsCount * 100.0 / totalSegments);
                        imageFile.imageMessage.Progress = SizeSuffix(imageFile.sizeCount) + "/" + SizeSuffix(totalLength);
                    }

                    else if (fileType.Equals("Media"))
                    {
                        if (!mediaFilesMap.ContainsKey(client))
                        {
                            mediaFilesMap.Add(client, new List <MediaFile>());
                        }

                        MediaFile mediaFile = mediaFilesMap[client].FirstOrDefault(x => x.fileName.Equals(fileName));
                        if (mediaFile == null)
                        {
                            try
                            {
                                mediaFile = new MediaFile()
                                {
                                    fileName      = fileName,
                                    segmentsCount = 0,
                                    sizeCount     = 0,
                                    stream        = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None),
                                    mediaMessage  = chats[client].CreateMediaMessage(fileName)
                                };
                                mediaFile.stream.SetLength(Convert.ToInt64(packet.headers["Total-Length"]));
                                mediaFilesMap[client].Add(mediaFile);
                            } catch (Exception ex)
                            {
                                Console.WriteLine("EXCEPTION: " + ex.ToString());
                                Console.WriteLine("media map size: " + mediaFilesMap[client].Count);
                            }
                        }

                        mediaFile.segmentsCount++;
                        mediaFile.sizeCount += segmentLength;

                        mediaFile.stream.Seek(pos, SeekOrigin.Begin);

                        mediaFile.stream.Write(packet.body, 0, packet.body.Length);

                        if (mediaFile.segmentsCount == totalSegments)
                        {
                            mediaFile.stream.Flush();
                            mediaFile.stream.Close();
                            mediaFile.stream = null;
                            mediaFile.mediaMessage.MediaUrl = path;
                            Console.WriteLine("Done file");
                            mediaFilesMap[client].Remove(mediaFile);
                        }

                        mediaFile.mediaMessage.Loading  = ((double)mediaFile.segmentsCount * 100.0 / totalSegments);
                        mediaFile.mediaMessage.Progress = SizeSuffix(mediaFile.sizeCount) + "/" + SizeSuffix(totalLength);
                    }
                }
            }
        }