/// <summary> /// Handle a LoginRequest package /// </summary> /// <param name="loginRequest">LoginRequest to handle</param> /// <param name="clientAddress">address of the sender of the package</param> /// <returns>the feedback transmitted</returns> private static void HandlePackage(LoginRequest loginRequest, string clientAddress) { Feedback feedback = null; if (!SDBController.TryVerifyUser(loginRequest.Email, loginRequest.PasswordHash, out bool result)) { feedback = new Feedback(FeedbackCode.InternalServerError); } else { if (result) { if (SDBController.TryUpdateClientLastIPAddress(loginRequest.Email, clientAddress)) { feedback = new Feedback(FeedbackCode.LoginAccepted); } else { feedback = new Feedback(FeedbackCode.InternalServerError); } } else { feedback = new Feedback(FeedbackCode.LoginDenied); } } if (feedback.FeedbackCode == FeedbackCode.LoginAccepted) { if (!SDBController.TryGetClientList(out List <User> userList)) { feedback = new Feedback(FeedbackCode.InternalServerError); } else { if (!SDBController.TryGetMessageList(loginRequest.Email, out List <MessagePackage> list)) { feedback = new Feedback(FeedbackCode.InternalServerError); } else { UserList userListPackage = new UserList(userList); SConnectionController.SendPackage(userListPackage, clientAddress); MessageList messageList = new MessageList(list); SConnectionController.SendPackage(messageList, clientAddress); SDBController.TryUpdateSendTime(loginRequest.Email); return; } } } SConnectionController.SendPackage(feedback, clientAddress); }
private static void HandlePackage(SendTextMessage sendTextMessage, string clientAddress) { DateTime?sendTime; if (!SDBController.ConnectionAvailable()) { SConnectionController.SendPackage(new Feedback(FeedbackCode.MessageSendFailed), clientAddress); return; } if (!SDBController.TryGetUser(clientAddress, out User user)) { SConnectionController.SendPackage(new Feedback(FeedbackCode.InvalidSession), clientAddress); return; } if (!SDBController.TryGetIPAddressByEmail(sendTextMessage.EMail, out string ipAddress)) { sendTime = null; } else { if (string.IsNullOrEmpty(ipAddress)) { sendTime = null; } else { if (SConnectionController.SendPackage(new ReceiveTextMessage(sendTextMessage.EMail, sendTextMessage.Text), ipAddress)) { sendTime = DateTime.Now; } else { sendTime = null; } } } if (SDBController.TryAddMessage(user.Email, sendTextMessage.EMail, sendTextMessage.Text, sendTime)) { SConnectionController.SendPackage(new Feedback(FeedbackCode.MessageSendSuccess), clientAddress); } else { SConnectionController.SendPackage(new Feedback(FeedbackCode.MessageSendFailed), clientAddress); } }
static void Main(string[] args) { WriteLine("Willkommen bei REEChat Server!"); WriteLine("Überprüfung der Datenbankverbindung..."); //checks whether a database connection can be created while (!SDBController.ConnectionAvailable()) { WriteLine("Datenbankverbindung fehlgeschlagen.\nErneuter Versuch in 10 Sekunden"); Thread.Sleep(10 * 1000); } WriteLine("Datenbankverbindung erfolgreich!"); WriteLine("Server wird initialisiert..."); SConnectionController.Init(); WriteLine("Server wird gestartet..."); //Trying to start the listener try { SConnectionController.Listener.Start(10); } catch (SocketException) { WriteLine("Server kann nicht gestartet werden. Der Port [" + ConnectionConfig.serverPort + "] wird bereits verwendet."); WriteLine("Mögliche Ursache dafür ist, dass bereits ein REEChat Server auf diesem Gerät läuft!"); WriteLine("Beliebige Taste drücken zum Schließen..."); Console.ReadKey(); return; } SConnectionController.ThreadListener.Start(); WriteLine("Server erfolgreich gestartet!\n"); while (true) { } }
/// <summary> /// Handle a RegistrationRequest /// </summary> /// <param name="request">RegistrationRequest to handle</param> /// <param name="clientAddress">address of the sender of the package</param> /// <returns>the feedback transmitted</returns> private static void HandlePackage(RegistrationRequest request, string clientAddress) { Feedback feedback = null; if (!CheckRegistrationRequest(request)) { feedback = new Feedback(FeedbackCode.RegistrationDenied); } else { switch (SDBController.TryAddClient(request)) { case 0: feedback = new Feedback(FeedbackCode.RegistrationAccepted); SDBController.TryGetAllIpAddresses(out List <string> addressList); SDBController.TryGetClientList(out List <User> userList); foreach (string address in addressList) { if (address != clientAddress) { SConnectionController.SendPackage(new UserList(userList), address); } } break; case 1: feedback = new Feedback(FeedbackCode.InternalServerError); break; case 2: feedback = new Feedback(FeedbackCode.RegistrationDeniedEmailAlreadyUsed); break; } } SConnectionController.SendPackage(feedback, clientAddress); }