public bool AnswerContactRequest(ContactRequest req, bool ack, out string message) { try { // Prepare the packet to send Packets.ContactReqPacket packet = new Packets.ContactReqPacket( (ack) ? Packets.PacketTypes.AcceptNewContact : Packets.PacketTypes.RegNewContact, alea, req.From.Username, profile.Username ); // send the package udp.SendMessage(packet.Pack()); // Recieve packet byte[] data = 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) { // Todo: Get Contacts info DebugInfo("Answer Contact Request: Accepted " + req.From.Username + " correctly."); } else { DebugInfo("Answer Contact Request: Refused " + req.From.Username + " correctly."); } // Update the request list GetContactRequestList(out message); message = "Answered correctly"; return(true); case Packets.PacketTypes.Error: message = "Error: " + Packets.Packet.Unpack <Packets.AckErrorPacket>(data).Message; DebugInfo("Answer Contact Request: " + message); break; default: DebugInfo("Answer Contact Request: Unexpected type."); message = "Error, unexpected type."; break; } return(false); } catch (System.Net.Sockets.SocketException) { DebugInfo("Server is offline."); message = "Server is offline."; return(false); } }
public static bool AckOrRegContactReq(Packets.ContactReqPacket packet, ClientStatus current, bool ack, out string message) { try { if (CheckBasics(current, ClientStatus.Status.Disconnected, packet.Alea, out message)) { using (var db = new Models.ServerDatabase()) { // Check if requests exists on any of both sides if (!db.ContactRequests.Any(c => (c.From.Username == packet.From && c.To.Username == packet.To))) { message = "Contact request doesn't exists."; return(false); } // Remove the contact request db.ContactRequests.DeleteOnSubmit( db.ContactRequests .Single(r => r.From.Username == packet.From && r.To.Username == packet.To) ); if (ack) { // Create new record to Contact Table db.Contacts.InsertOnSubmit(new Models.Contact() { Client1 = db.Clients.Single(c => c.Username == packet.From), Client2 = db.Clients.Single(c => c.Username == packet.To) }); message = "Contact added correctly."; } else { message = "Contact request refused correctly."; } db.SubmitChanges(); } } else { return(false); } return(true); } catch (SqlException) { message = "New contact request error: Database error"; return(false); } }
public bool SendContactRequest(string to, out string message) { try { // Prepare the packet to send Packets.ContactReqPacket packet = new Packets.ContactReqPacket( Packets.PacketTypes.NewContactReq, alea, profile.Username, to ); // send the package udp.SendMessage(packet.Pack()); // Recieve packet byte[] data = 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: DebugInfo("Send Contact request: Sent succesful to " + to); // Update the request list GetContactRequestList(out message); message = "Request sent correctly."; return(true); case Packets.PacketTypes.Error: message = "Error: " + Packets.Packet.Unpack <Packets.AckErrorPacket>(data).Message; DebugInfo("Send Contact request: " + message); break; default: DebugInfo("Send Contact request: Unexpected type."); message = "Error, unexpected type."; break; } return(false); } catch (System.Net.Sockets.SocketException) { DebugInfo("Server is offline."); message = "Server is offline."; return(false); } }
public static bool NewContactRequest(Packets.ContactReqPacket packet, ClientStatus current, out string message) { try { if (packet.To == packet.From) { message = "You can't add yourself as a friend...."; return(false); } if (CheckBasics(current, ClientStatus.Status.Disconnected, packet.Alea, out message)) { using (var db = new Models.ServerDatabase()) { // Check if destination exists if (!db.Clients.Any(c => c.Username == packet.To)) { message = "Destination doesn't exist."; return(false); } // Check if contact exists if (db.Contacts.Any(c => (c.Client1.Username == packet.To && c.Client2.Username == packet.From) || (c.Client1.Username == packet.From && c.Client2.Username == packet.To))) { message = "Contact already exists."; return(false); } // Check if requests exists on any of both sides if (db.ContactRequests.Any(c => (c.From.Username == packet.To && c.To.Username == packet.From) || (c.From.Username == packet.From && c.To.Username == packet.To))) { message = "Contact request already exists."; return(false); } // Create new record to ContactRequests Table db.ContactRequests.InsertOnSubmit(new Models.ContactRequest() { From = db.Clients.Single(c => c.Username == packet.From), To = db.Clients.Single(c => c.Username == packet.To) }); db.SubmitChanges(); } } else { return(false); } message = "Cool!"; return(true); } catch (SqlException) { message = "New contact request error: Database error"; return(false); } }