Esempio n. 1
0
        private void SendFile(string absolutePath, string fileName, Client client)
        {
            FileStream fileStream = new FileStream(absolutePath, FileMode.Open, FileAccess.Read);

            byte[] buffer = new byte[maxSegmentSize];

            long length        = fileStream.Length;
            long segmentCount  = 1;
            long totalSegments = (long)Math.Ceiling((double)length / buffer.Length);

            long pos = 0;

            Console.WriteLine("File length: " + length);
            while (pos < length)
            {
                int bytesRead = fileStream.Read(buffer, 0, buffer.Length);

                byte[] data = new byte[bytesRead];
                Array.Copy(buffer, 0, data, 0, bytesRead);

                FyzrPacket packet = FileProtocol.CreateFilePacket(data, fileName, MessageProtocol.MessageType.PRIVATE, null);
                packet.headers.Add("Total-Length", "" + length);
                packet.headers.Add("Total-Segments", "" + totalSegments);
                packet.headers.Add("Segment", "" + segmentCount);
                packet.headers.Add("Position", "" + pos);

                client.SendRandom(FyzrParser.ToData(packet));
                pos += buffer.Length;
                Console.WriteLine("Count: " + segmentCount++);
                Console.WriteLine("Bytes read: " + bytesRead);
            }
        }
Esempio n. 2
0
        internal void SendMessage(ChatWindow chatWindow, TextMessage msg)
        {
            var client = chats.FirstOrDefault(x => x.Value == chatWindow).Key;
            var packet = TextProtocol.CreateTextPacket(msg.Text, MessageProtocol.MessageType.PRIVATE, null);

            packet.headers.Add("Username", ownUsername);
            client.SendOrdered(FyzrParser.ToData(packet));
        }
Esempio n. 3
0
        public void SendDisconnect()
        {
            FyzrPacket packet = new FyzrPacket();

            packet.method = FyzrPacket.Method.COMMAND;
            packet.headers.Add("Command-Type", "disconnect");
            packet.headers.Add("Username", ownUsername);

            coordinator.SendOrdered(FyzrParser.ToData(packet));
        }
Esempio n. 4
0
        public void RequestUsersList()
        {
            FyzrPacket packet = new FyzrPacket();

            packet.method = FyzrPacket.Method.COMMAND;
            packet.headers.Add("Command-Type", "requestUsersUpdate");
            packet.headers.Add("Listener-Port", "" + ownServer.listener.LocalEndpoint.ToString().Split(':')[1]);

            coordinator.SendOrdered(FyzrParser.ToData(packet));
        }
Esempio n. 5
0
        private void btnServerSend_Click(object sender, RoutedEventArgs e)
        {
            if (txtServerMessage.Text.Trim().Length == 0)
            {
                MessageBox.Show("Enter some text first");
                return;
            }
            string message = txtServerMessage.Text;


            Client client = fromClients[0];

            client.SendOrdered(
                FyzrParser.ToData(
                    TextProtocol.CreateTextPacket(message, MessageProtocol.MessageType.PRIVATE, null)
                    ));
            Dispatcher.Invoke(() => { listServerMessages.Items.Add("Alice: " + message); });
        }
Esempio n. 6
0
        public bool RegisterNickname(string nickname, string coordinatorIp, int coordinatorPort)
        {
            ownUsername         = nickname;
            coordinatorEndpoint = new IPEndPoint(IPAddress.Parse(coordinatorIp), coordinatorPort);
            coordinator.StartConnect(coordinatorEndpoint);
            Thread.Sleep(10);
            coordinator.PacketReceived += Coordinator_PacketReceived;
            FyzrPacket packet = new FyzrPacket();

            packet.method = FyzrPacket.Method.COMMAND;
            packet.headers.Add("Command-Type", "requestUsers");
            packet.headers.Add("Username", nickname);
            packet.headers.Add("Listener-Port", "" + ownServer.listener.LocalEndpoint.ToString().Split(':')[1]);

            coordinator.SendOrdered(FyzrParser.ToData(packet));


            return(true);
        }
Esempio n. 7
0
        internal void SendMediaFile(ChatWindow chatWindow, string absolutePath, string fileName, MediaMessage msg)
        {
            var        client     = chats.FirstOrDefault(x => x.Value == chatWindow).Key;
            FileStream fileStream = new FileStream(absolutePath, FileMode.Open, FileAccess.Read);

            byte[] buffer = new byte[maxSegmentSize];

            long length        = fileStream.Length;
            long segmentCount  = 1;
            long sizeCount     = 0;
            long totalSegments = (long)Math.Ceiling((double)length / buffer.Length);

            long pos = 0;

            Console.WriteLine("File length: " + length);
            while (pos < length)
            {
                int bytesRead = fileStream.Read(buffer, 0, buffer.Length);
                sizeCount += bytesRead;
                byte[] data = new byte[bytesRead];
                Array.Copy(buffer, 0, data, 0, bytesRead);

                FyzrPacket packet = FileProtocol.CreateFilePacket(data, fileName, MessageProtocol.MessageType.PRIVATE, null);
                packet.headers.Add("Total-Length", "" + length);
                packet.headers.Add("Total-Segments", "" + totalSegments);
                packet.headers.Add("Segment", "" + segmentCount);
                packet.headers.Add("Position", "" + pos);
                packet.headers.Add("File-Type", "Media");
                packet.headers.Add("Username", ownUsername);

                client.SendRandom(FyzrParser.ToData(packet));
                msg.Loading  = ((double)segmentCount * 100.0 / totalSegments);
                msg.Progress = SizeSuffix(sizeCount) + "/" + SizeSuffix(length);
                pos         += buffer.Length;
                Console.WriteLine("Count: " + segmentCount++);
                Console.WriteLine("Bytes read: " + bytesRead);
                Thread.Sleep(2);
            }
        }
Esempio n. 8
0
        private void Client_PacketReceived(object sender, DS_Chat_CS1.Core.Events.PacketFullyReceivedEventArgs e)
        {
            //Console.WriteLine("Received something");
            var        client = sender as Client;
            FyzrPacket packet = FyzrParser.FromData(e.Packet.data.ToArray());

            if (packet.method == FyzrPacket.Method.COMMAND)
            {
                string cmdType = packet.headers["Command-Type"];
                if (cmdType.Equals("requestUsers"))
                {
                    if (!packet.headers.ContainsKey("Username"))
                    {
                        return;
                    }
                    string username = packet.headers["Username"].Trim();
                    if (username.Length == 0)
                    {
                        return;
                    }
                    int port = Convert.ToInt32(packet.headers["Listener-Port"]);

                    /*
                     * Username#Endpoint#Room
                     *
                     */
                    string response = "";
                    foreach (var user in users)
                    {
                        response += user.ToString() + '\n';
                    }
                    response = response.Trim();

                    IPEndPoint endPoint = new IPEndPoint(client.GetRemoteEndPoint().Address, port);
                    var        u        = new UserObject()
                    {
                        Username = username, Endpoint = endPoint, Room = null, client = client
                    };
                    users.Add(u);
                    u.lastReceived = DateTime.Now;

                    FyzrPacket responsePacket = new FyzrPacket();
                    responsePacket.method = FyzrPacket.Method.COMMAND;
                    responsePacket.headers.Add("Content-Encoding", Encoding.Default.WebName);
                    responsePacket.body = Encoding.Default.GetBytes(response);
                    responsePacket.headers.Add("Content-Length", "" + responsePacket.body.Length);
                    responsePacket.headers.Add("Command-Type", "usersList");

                    client.SendOrdered(FyzrParser.ToData(responsePacket));
                }
                else if (cmdType.Equals("requestUsersUpdate"))
                {
                    int        port     = Convert.ToInt32(packet.headers["Listener-Port"]);
                    IPEndPoint endPoint = new IPEndPoint(client.GetRemoteEndPoint().Address, port);
                    var        u        = users.Find(x => x.Endpoint.Equals(endPoint));
                    u.lastReceived = DateTime.Now;

                    string response = "";
                    foreach (var user in users)
                    {
                        if (user != u)
                        {
                            response += user.ToString() + '\n';
                        }
                    }
                    response = response.Trim();

                    FyzrPacket responsePacket = new FyzrPacket();
                    responsePacket.method = FyzrPacket.Method.COMMAND;
                    responsePacket.headers.Add("Content-Encoding", Encoding.Default.WebName);
                    responsePacket.body = Encoding.Default.GetBytes(response);
                    responsePacket.headers.Add("Content-Length", "" + responsePacket.body.Length);
                    responsePacket.headers.Add("Command-Type", "usersList");

                    client.SendOrdered(FyzrParser.ToData(responsePacket));
                }
                else if (cmdType.Equals("disconnect"))
                {
                    Console.WriteLine("Disconnect received");
                    client.Disconnect();
                }
            }
        }