public static bool AnswerContactRequest(ContactRequest req, bool ack, Client client, out string message)
        {
            try
            {
                // Prepare the packet to send
                Packets.ContactReqPacket packet = new Packets.ContactReqPacket(
                    (ack) ? Packets.PacketTypes.AcceptNewContact : Packets.PacketTypes.RegNewContact,
                    client.Alea,
                    req.From.Username,
                    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:
                    if (ack)
                    {
                        client.DebugInfo("Answer Contact Request: Accepted " + req.From.Username + " correctly.");
                    }
                    else
                    {
                        client.DebugInfo("Answer Contact Request: Refused " + req.From.Username + " correctly.");
                    }

                    // Update the request list
                    client.GetContactRequestList(out message);

                    message = "Answered correctly";
                    return(true);

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

                default:
                    client.DebugInfo("Answer Contact Request: 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);
            }
        }
Beispiel #2
0
        public static bool AgendaEvent(string eventName, string eventInfo, DateTime date, bool delete, Client client, out string message)
        {
            try
            {
                // Pack register request packet
                Packets.AgendaEventPacket packet =
                    new Packets.AgendaEventPacket(
                        delete ? Packets.PacketTypes.DeleteAgendaEventReq : Packets.PacketTypes.NewAgendaEventReq,
                        client.Alea,         //> For registering always 7 0's
                        client.Profile.Username,
                        eventName,
                        eventInfo,
                        date
                        );

                // Send register request package
                client.Udp.SendMessage(packet.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.NewAgendaEventAck:
                    // New event added correctly
                    client.DebugInfo("New Agenda event: Added new event!");
                    message = "New Agenda event added correctly.";
                    return(true);

                case Packets.PacketTypes.DeleteAgendaEventAck:
                    // Event deleted correctly
                    client.DebugInfo("Delete Agenda event: Deleted correctly!");
                    message = "Agenda event has been deleted";
                    return(true);

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

                default:
                    client.DebugInfo("Agenda event error: 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);
            }
        }
Beispiel #3
0
        public static bool Register(Profile newProfile, Client client, out string message)
        {
            try
            {
                // Pack register request packet
                Packets.ProfilePacket reg =
                    new Packets.ProfilePacket(
                        Packets.PacketTypes.RegisterReq,
                        "0000000",         //> For registering always 7 0's
                        newProfile.FirstName,
                        newProfile.LastName,
                        newProfile.Age,
                        newProfile.PhoneNumber,
                        (Profile.Sex)newProfile.Gender,
                        newProfile.Username,
                        newProfile.Password,
                        newProfile.Email
                        );

                // Send register request package
                client.Udp.SendMessage(reg.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.RegisterAck:
                    // Complete registration
                    client.DebugInfo("Register: Done!");
                    message = "Registration successfully completed. Now You can login.";
                    return(true);

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

                default:
                    client.DebugInfo("Register: 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);
            }
        }
        public static bool UpdateProfile(Profile newProfile, Client client, out string message)
        {
            try
            {
                // Pack login request packet
                Packets.ProfilePacket profilePacket = new Packets.ProfilePacket(
                    Packets.PacketTypes.ProfileUpdateReq,
                    client.Alea,
                    newProfile
                    );

                // Send login request package
                client.Udp.SendMessage(profilePacket.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.ProfileUpdateAck:
                    // Complete profile update
                    Packets.ProfilePacket profileP =
                        Packets.Packet.Unpack <Packets.ProfilePacket>(data);

                    // No set new messages when alive
                    profileP.Messages = client.Profile.Messages;

                    client.Profile.SetFromPacket(profileP);
                    message = "Profile Updated";
                    return(true);

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

                default:
                    client.DebugInfo("Update Profile: Unexpected type.");
                    message = "Error, unexpected type.";
                    break;
                }
                return(false);
            }
            catch (System.Net.Sockets.SocketException e)
            {
                client.DebugInfo("Server is offline.");
                message = e.ToString();
                return(false);
            }
        }
        public static bool ClientQuery(string query, ref List <Profile> profiles, Client client, out string message)
        {
            try
            {
                // Prepare the packet to send
                Packets.ClientQueryPacket packet = new Packets.ClientQueryPacket(
                    Packets.PacketTypes.ClientsQueryReq,
                    client.Alea,
                    client.Profile.Username,
                    query
                    );

                // 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.ClientsQueryAck:
                    // Update contact requests list
                    Packets.ClientQueryPacket queryResult =
                        Packets.Packet.Unpack <Packets.ClientQueryPacket>(data);


                    profiles = queryResult.Profiles;

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

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

                default:
                    client.DebugInfo("Client query 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);
            }
        }
Beispiel #6
0
        public static bool Login(string username, string password, Client client, out string message)
        {
            try
            {
                // Pack login request packet
                Packets.LoginReqPacket log = new Packets.LoginReqPacket(
                    Packets.PacketTypes.LoginReq,
                    "0000000",
                    username,
                    password
                    );

                // Send login request package
                client.Udp.SendMessage(log.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.LoginAck:
                    // Complete login
                    Packets.ProfilePacket profileP = Packets.Packet.Unpack <Packets.ProfilePacket>(data);
                    client.Profile.SetFromPacket(profileP);
                    client.Alea = p.Alea;
                    client.DebugInfo("Login: Done.");
                    client.DebugInfo("Login: Alive timer is enabled.");
                    client.AliveTimer.Enabled = true;
                    message = "I'm logged in.";
                    return(true);

                case Packets.PacketTypes.Error:
                    message = "Error: " + Packets.Packet.Unpack <Packets.AckErrorPacket>(data).Message;
                    client.DebugInfo("Login: "******"Login: 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);
            }
        }
        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);
            }
        }
Beispiel #8
0
        public static bool DeleteLinkSocialNetwork(SocialNetwork sn, Client client, out string message)
        {
            try
            {
                // Pack register request packet
                Packets.LinkSocialNetworkPacket packet =
                    new Packets.LinkSocialNetworkPacket(
                        Packets.PacketTypes.DeleteLinkSocialNetReq,
                        client.Alea,
                        client.Profile.Username,
                        sn.Name,
                        sn.Username,
                        sn.Password);


                // Send register request package
                client.Udp.SendMessage(packet.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.DeleteLinkSocialNetAck:
                    // New event added correctly
                    client.DebugInfo("Social Network Link: New social net deleted.");
                    message = "Deleted social net correctly.";
                    return(true);

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

                default:
                    client.DebugInfo("Social Network Link error: 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);
            }
        }
Beispiel #9
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);
            }
        }
Beispiel #10
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);
            }
        }
        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;
            }
        }