Exemplo n.º 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("Connection with client brokes.", e);
            }

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

            try
            {
                client.Close();
            }
            catch (ConnectionInterruptedException e)
            {
                log.Error(e);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Start listen to clients and process connected clients.
        /// </summary>
        public async void Start()
        {
#if DEBUG
            DBoperations.Register("13", "13");
            for (int i = 0; i < 10; i++)
            {
                string login = "******" + i.ToString();
                DBoperations.Register(login, login);
            }
            for (int i = 10; i < 20; i++)
            {
                string login = "******" + i.ToString();
                DBoperations.Register(login, login);
                DBoperations.SetFriendship(DBoperations.GetUserId(login), DBoperations.GetUserId("13"), false);
            }
            for (int i = 20; i < 30; i++)
            {
                string login = "******" + i.ToString();
                DBoperations.Register(login, login);
                DBoperations.SetFriendship(DBoperations.GetUserId("13"), DBoperations.GetUserId(login), false);
            }
            for (int i = 30; i < 40; i++)
            {
                string login = "******" + i.ToString();
                DBoperations.Register(login, login);
                DBoperations.SetFriendship(DBoperations.GetUserId("13"), DBoperations.GetUserId(login), true);
            }
#endif

            if (isStarted)
            {
                return;
            }
            if (certificate == null)
            {
                return;
            }

            mpListener = new MpListener(port, certificate);
            mpListener.Start();
            isStarted = true;

            log.Info("Server is now listening.");

            while (true)
            {
                try
                {
                    MpClient client = await mpListener.AcceptMpClientAsync();

                    Task t = HandleClient(client);
                    activeTasks.Add(t);

                    // remove completed tasks
                    activeTasks.RemoveAll(x => x.IsCompleted);
                }
                catch (ConnectionInterruptedException e)
                {
                    // connection with client broke
                    // nothing critical, just continue
                    log.Error("Connection with client broke.", e);
                    continue;
                }
                catch (SocketException e)
                {
                    log.Error("Socket exception. Trying to restart listening.", e);

                    mpListener.Stop();
                    mpListener = null;
                    mpListener = new MpListener(port, certificate);
                    mpListener.Start();

                    continue;
                }
                catch (ObjectDisposedException)
                {
                    // the way we stop server
                    break;
                }
            }

            // wait for finishing process clients
            Task.WaitAll(activeTasks.ToArray());

            log.Info("Server is now stopped listening.");
            isStarted = false;
        }