public static bool GetContactRequests(Client client, out string message)
        {
            try
            {
                // Prepare the packet to send
                Packets.BasicReqPacket packet = new Packets.BasicReqPacket(
                    Packets.PacketTypes.ListContactReq,
                    client.Alea,
                    client.Profile.Username
                    );

                // send the package
                client.Udp.SendMessage(packet.Pack());

                // Recieve packet
                byte[] data = client.Udp.RecieveMessage();

                // Unpack the data and check the type
                Packets.Packet p = Packets.Packet.Unpack <Packets.Packet>(data);
                switch ((Packets.PacketTypes)p.Type)
                {
                case Packets.PacketTypes.ContactAck:
                    // Update contact requests list
                    Packets.ContactReqListPacket contactsRequests =
                        Packets.Packet.Unpack <Packets.ContactReqListPacket>(data);

                    client.Profile.RecievedContactRequests = contactsRequests.Recieved;
                    client.Profile.SentContactRequests     = contactsRequests.Sent;

                    message = "Recieved list correctly";
                    return(true);

                case Packets.PacketTypes.Error:
                    message = "Error: " + Packets.Packet.Unpack <Packets.AckErrorPacket>(data).Message;
                    client.DebugInfo("Contact request list: " + message);
                    break;

                default:
                    client.DebugInfo("Contact request list: Unexpected type.");
                    message = "Error, unexpected type.";
                    break;
                }
                return(false);
            }
            catch (System.Net.Sockets.SocketException)
            {
                client.DebugInfo("Server is offline.");
                message = "Server is offline.";
                return(false);
            }
        }
Exemplo n.º 2
0
        public static bool Logout(Client client, out string message)
        {
            try
            {
                // Pack logut request packet
                // use alive packet because contains the same information as logut request
                Packets.BasicReqPacket logout = new Packets.BasicReqPacket(
                    Packets.PacketTypes.LogoutReq,
                    client.Alea,
                    client.Profile.Username
                    );

                // Send logut request package
                client.Udp.SendMessage(logout.Pack());

                // Recieve the data
                var data = client.Udp.RecieveMessage();
                // Unpack the data and check the type
                Packets.Packet p = Packets.Packet.Unpack <Packets.Packet>(data);
                switch ((Packets.PacketTypes)p.Type)
                {
                case Packets.PacketTypes.LogoutAck:
                    // Complete logut
                    client.DebugInfo("Logut: Done.");
                    message = "I'm NOT logged in now.";
                    client.AliveTimer.Enabled = false;
                    return(true);

                case Packets.PacketTypes.Error:
                    message = "Error: " + Packets.Packet.Unpack <Packets.AckErrorPacket>(data).Message;
                    client.DebugInfo("Logut: " + message);
                    break;

                default:
                    client.DebugInfo("Logout: Unexpected type.");
                    message = "Error, unexpected type.";
                    break;
                }
                return(false);
            }
            catch (System.Net.Sockets.SocketException)
            {
                client.DebugInfo("Server is offline.");
                message = "Server is offline.";
                return(false);
            }
        }
Exemplo n.º 3
0
        public static bool DeleteAccount(Client client, out string message)
        {
            try
            {
                // Pack logut request packet
                // use alive packet because contains the same information as delete account request
                Packets.BasicReqPacket delete = new Packets.BasicReqPacket(
                    Packets.PacketTypes.DeleteAccountReq,
                    client.Alea,
                    client.Profile.Username
                    );

                // Send delete account request package
                client.Udp.SendMessage(delete.Pack());

                // Recieve the data
                var data = client.Udp.RecieveMessage();
                // Unpack the data and check the type
                Packets.Packet p = Packets.Packet.Unpack <Packets.Packet>(data);
                switch ((Packets.PacketTypes)p.Type)
                {
                case Packets.PacketTypes.DeleteAccountAck:
                    // Complete Deletion
                    client.DebugInfo("Account delete: Done.");
                    message = "";
                    return(true);

                case Packets.PacketTypes.Error:
                    message = "Error: " + Packets.Packet.Unpack <Packets.AckErrorPacket>(data).Message;
                    client.DebugInfo("Delete account error: " + message);
                    break;

                default:
                    client.DebugInfo("Delete account: Unexpected type.");
                    message = "Error, unexpected type.";
                    break;
                }
                return(false);
            }
            catch (System.Net.Sockets.SocketException)
            {
                client.DebugInfo("Server is offline.");
                message = "Server is offline.";
                return(false);
            }
        }
Exemplo n.º 4
0
        public static void UpdateProfile(Client client)
        {
            try
            {
                // Pack alive info packet
                byte[] alive = new Packets.BasicReqPacket(
                    Packets.PacketTypes.AliveInf,
                    client.Alea,
                    client.Profile.Username
                    ).Pack();

                // Send alive packet
                client.Udp.SendMessage(alive);

                // Recieve the data
                var data = client.Udp.RecieveMessage();

                // Unpack the data and check the type
                Packets.Packet p = Packets.Packet.Unpack <Packets.Packet>(data);
                switch ((Packets.PacketTypes)p.Type)
                {
                case Packets.PacketTypes.AliveAck:
                    // ALIVE CORRECT
                    client.DebugInfo("Alive: Alive ack recieved");
                    break;

                case Packets.PacketTypes.Error:
                    client.DebugInfo("Alive: " + Packets.Packet.Unpack <Packets.AckErrorPacket>(data).Message);
                    client.DebugInfo("Login: Alive timer is disabled.");
                    client.AliveTimer.Enabled = false;
                    return;

                default:
                    client.DebugInfo("Alive unexpected alive type");
                    client.DebugInfo("Login: Alive timer is disabled.");
                    client.AliveTimer.Enabled = false;
                    return;
                }
            }
            catch (System.Net.Sockets.SocketException)
            {
                client.DebugInfo("Server is offline.");
                return;
            }
        }