Example #1
0
		/// <summary>
		/// Try to register client. Send response about result.
		/// </summary>
		/// <param name="client">client.</param>
		/// <param name="message">client's message.</param>
		private void RegisterClient(MpClient client, RegisterRequestMessage message)
		{
			Message response;

			if (string.IsNullOrEmpty(message.Login) ||
				string.IsNullOrEmpty(message.Password) ||
				message.Login.Length > 20 ||
				message.Password.Length > 30)
			{
				response = new LoginRegisterResponseMessage { Response = LoginRegisterResponse.Error };
			}
			else
			{
				if (DBoperations.Register(message.Login, message.Password))
				{
					log.Info($"Client registered: {message.Login}");

					response = new LoginRegisterResponseMessage { Response = LoginRegisterResponse.Success };
				}
				else
				{
					response = new LoginRegisterResponseMessage { Response = LoginRegisterResponse.Fail };
				}
			}

			try
			{
				// response to client
				client.SendMessage(response);
			}
			catch (ConnectionInterruptedException e)
			{
				log.Error(e);
			}

			// close connection
			log.Info($"Client disconnected. ip {((IPEndPoint)client.tcpClient.Client.RemoteEndPoint).Address.ToString()}");

			try
			{
				client.Close();
			}
			catch (ConnectionInterruptedException e)
			{
				log.Error(e);
			}
		}
Example #2
0
		/// <summary>
		/// Try to login client. Send response about result.
		/// </summary>
		/// <param name="client">client.</param>
		/// <param name="message">client's message.</param>
		private void LoginClient(MpClient client, LoginRequestMessage message)
		{
			bool isLoggedIn = false;
			Message response;

			if (string.IsNullOrEmpty(message.Login) ||
				string.IsNullOrEmpty(message.Password) ||
				message.Login.Length > 20 ||
				message.Password.Length > 30)
			{
				response = new LoginRegisterResponseMessage { Response = LoginRegisterResponse.Error };
			}
			else if (usersHandler.GetOnlineUser(message.Login) != null)
			{
				response = new LoginRegisterResponseMessage { Response = LoginRegisterResponse.AlreadyLogin };
			}
			else
			{
				int id; // client's id in db
				if (DBoperations.Login(message.Login, message.Password, out id))
				{
					log.Info($"Client login: {message.Login}");

					// user is online
					OnlineUser user = new OnlineUser(id, message.Login, client);
					usersHandler.AddUser(user);
					isLoggedIn = true;
					response = new LoginRegisterResponseMessage { Response = LoginRegisterResponse.Success };
				}
				else
				{
					response = new LoginRegisterResponseMessage { Response = LoginRegisterResponse.Fail };
				}
			}

			try
			{
				// response to client
				client.SendMessage(response);
			}
			catch (ConnectionInterruptedException e)
			{
				log.Error(e);
			}

			// close connection with client if client not logged in
			if (!isLoggedIn)
			{
				log.Info($"Client disconnected. ip {((IPEndPoint)client.tcpClient.Client.RemoteEndPoint).Address.ToString()}");

				try
				{
					client.Close();
				}
				catch (ConnectionInterruptedException e)
				{
					log.Error(e);
				}
			}
		}
 /// <summary>
 /// Create user.
 /// </summary>
 /// <param name="id">client's id in db.</param>
 /// <param name="login">client's login.</param>
 /// <param name="client">client.</param>
 public OnlineUser(int id, string login, MpClient client)
 {
     Id = id;
     Login = login;
     Client = client;
 }
Example #4
0
		/// <summary>
		/// Handle client.
		/// </summary>
		/// <param name="client">Connected client.</param>
		private async Task HandleClient(MpClient client)
		{
			log.Info($"Client connected. ip {((IPEndPoint)client.tcpClient.Client.RemoteEndPoint).Address.ToString()}");

			await Task.Run(() =>
			{
				try
				{
					// recieve client's message
					Message message = client.ReceiveMessage();

					// login / register
					if (message != null)
					{
						if (message is LoginRequestMessage)
							LoginClient(client, (LoginRequestMessage)message);
						else if (message is RegisterRequestMessage)
							RegisterClient(client, (RegisterRequestMessage)message);
					}
				}
				catch (ConnectionInterruptedException e)
				{
					log.Error(e);

					log.Info($"Client disconnected. ip {((IPEndPoint)client.tcpClient.Client.RemoteEndPoint).Address.ToString()}");
				}
			});
		}
Example #5
0
		public Client()
		{
			// get data from connection.cfg
			XDocument doc = XDocument.Load("connection.config");
			
			var _ip = doc.Descendants("ip");
			ip = "";
			foreach (var i in _ip) ip = i.Value;

			var _port = doc.Descendants("port");
			port = 0;
			foreach (var i in _port) port = int.Parse(i.Value);

			OnlineFriendsList = null;
			OfflineFriendsList = null;
			OnlineUsersList = null;
			OfflineUsersList = null;
			IncomeRequestsList = null;
			OutcomeRequestsList = null;

			Name = null;
			client = new MpClient();
			Conversations = new Conversations();

			ConnectionBreaks += delegate { isLoggedIn = false; };
		}