Пример #1
0
        public void StartSession()
        {
            string receivedPackage;

            NetworkStream networkStream = null;

            try
            {
                Console.WriteLine("new client");

                networkStream = TcpClient.GetStream();

                while (true)
                {
                    receivedPackage = Package.Read(networkStream);
                    RequestType type;
                    type = RequestConverter.GetRequestType(receivedPackage);
                    Console.WriteLine(type);
                    Type selectedController = GetControllerByRequestType(type);

                    selectedController.GetMethod(nameof(BaseController.Handle))
                    .Invoke(null, new object[] { RequestConverter.GetData(receivedPackage), this });
                }
            }
            catch (IOException ioEx)
            {
                Console.WriteLine(ioEx.Message);
            }
            catch (FormatException foEx)
            {
                Console.WriteLine(foEx.Message);
            }
            catch (ControllerNotFoundExeption contrNotFound)
            {
                Console.WriteLine(contrNotFound.Message);
            }
            finally
            {
                networkStream?.Close();
                TcpClient?.Close();

                Connection connection = this;
                ConnectionController.Connections.TryTake(out connection);
            }
        }
        public async void Start()
        {
            await Task.Run(() =>
            {
                try
                {
                    TcpClient        = new TcpClient(Constants.IP, Constants.PORT);
                    NetworkStream ns = TcpClient.GetStream();
                    string buff;

                    while (true)
                    {
                        while (ns.DataAvailable)
                        {
                            string receivedPackage = Package.Read(ns);

                            RequestType type = RequestConverter.GetRequestType(receivedPackage);
                            string data      = RequestConverter.GetData(receivedPackage);
                            switch (type)
                            {
                            case RequestType.LoginResponse:
                                userHandler.Found = RequestConverter.DecomposeLoginResponse(data);
                                break;

                            case RequestType.RegistrationResponse:
                                userHandler.Found = RequestConverter.DecomposeLoginResponse(data);
                                break;

                            case RequestType.SendMessage:
                                Message message = RequestConverter.DecomposeMessage(data);
                                messageHandler.SaveAndShowMessage(new LocalMessage(message));
                                Package.Write(ns, RequestConverter.ComposeMessageReceived(message.Id));
                                break;

                            case RequestType.CheckUserExistResponse:
                                userHandler.ReceiverFound = RequestConverter.DecomposeUserExistResponse(data);
                                break;

                            case RequestType.MessageSyncInfo:
                                messageHandler.SyncMessage(RequestConverter.DecomposeMessageSyncInfo(data));
                                break;

                            default:
                                break;
                            }
                        }

                        while (!RequestsToSend.IsEmpty)
                        {
                            RequestsToSend.TryTake(out buff);
                            Package.Write(ns, buff);
                        }

                        Thread.Sleep(5);
                    }
                }
                catch (IOException ioEx)
                {
                    Console.WriteLine(ioEx.Message);
                }
                finally
                {
                    TcpClient?.GetStream().Close();
                    TcpClient?.Close();
                }
            });
        }