示例#1
0
        public async Task SendMessageAsync(Message msg)
        {
            await _networkCommunication.SendBytesAsync(new[] { (byte)msg.Type });

            await _networkCommunication.SendShortAsync((short)msg.Id);

            switch (msg)
            {
            // Error
            case ErrorResponse errorResponse:
                var errorResponseHandler = new ErrorResponseHandler(_networkCommunication);
                await errorResponseHandler.SendMessageAsync(errorResponse);

                break;

            // Login
            case LoginRequest loginRequest:
                var loginRequestHandler = new LoginRequestHandler(_networkCommunication);
                await loginRequestHandler.SendMessageAsync(loginRequest);

                break;

            case LoginResponse loginResponse:
                var loginResponseHandler = new LoginResponseHandler(_networkCommunication);
                await loginResponseHandler.SendMessageAsync(loginResponse);

                break;

            // Create User
            case CreateUserRequest createUserRequest:
                var createUserRequestHandler = new CreateUserRequestHandler(_networkCommunication);
                await createUserRequestHandler.SendMessageAsync(createUserRequest);

                break;

            case CreateUserResponse createUserResponse:
                var createUserResponseHandler = new CreateUserResponseHandler(_networkCommunication);
                await createUserResponseHandler.SendMessageAsync(createUserResponse);

                break;

            // Create photo
            case CreatePhotoRequest createPhotoRequest:
                var createPhotoRequestHandler = new CreatePhotoRequestHandler(
                    _networkCommunication,
                    _fileCommunication
                    );
                await createPhotoRequestHandler.SendMessageAsync(createPhotoRequest);

                break;

            case CreatePhotoResponse createPhotoResponse:
                var createPhotoResponseHandler = new CreatePhotoResponseHandler();
                await createPhotoResponseHandler.SendMessageAsync(createPhotoResponse);

                break;

            // Photo list
            case PhotoListRequest photoListRequest:
                var photoListRequestHandler = new PhotoListRequestHandler(_networkCommunication);
                await photoListRequestHandler.SendMessageAsync(photoListRequest);

                break;

            case PhotoListResponse photoListResponse:
                var photoListResponseHandler = new PhotoListResponseHandler(
                    _networkCommunication,
                    _fileCommunication
                    );
                await photoListResponseHandler.SendMessageAsync(photoListResponse);

                break;

            // User list
            case UserListRequest userListRequest:
                var userListRequestHandler = new UserListRequestHandler();
                await userListRequestHandler.SendMessageAsync(userListRequest);

                break;

            case UserListResponse userListResponse:
                var userListResponseHandler = new UserListResponseHandler(
                    _networkCommunication
                    );
                await userListResponseHandler.SendMessageAsync(userListResponse);

                break;

            // Comment photo
            case CommentPhotoRequest commentPhotoRequest:
                var commentPhotoRequestHandler = new CommentPhotoRequestHandler(_networkCommunication);
                await commentPhotoRequestHandler.SendMessageAsync(commentPhotoRequest);

                break;

            case CommentPhotoResponse commentPhotoResponse:
                var commentPhotoResponseHandler = new CommentPhotoResponseHandler(
                    _networkCommunication
                    );
                await commentPhotoResponseHandler.SendMessageAsync(commentPhotoResponse);

                break;

            // Comment List
            case CommentListRequest commentListRequest:
                var commentListRequestHandler = new CommentListRequestHandler(_networkCommunication);
                await commentListRequestHandler.SendMessageAsync(commentListRequest);

                break;

            case CommentListResponse commentListResponse:
                var commentListResponseHandler = new CommentListResponseHandler(
                    _networkCommunication
                    );
                await commentListResponseHandler.SendMessageAsync(commentListResponse);

                break;

            // Logout
            case LogoutRequest logoutRequest:
                var logoutRequestHandler = new LogoutRequestHandler();
                await logoutRequestHandler.SendMessageAsync(logoutRequest);

                break;

            case LogoutResponse logoutResponse:
                var logoutResponseHandler = new LogoutResponseHandler();
                await logoutResponseHandler.SendMessageAsync(logoutResponse);

                break;

            default:
                // TODO Create a custom exception
                throw new Exception($"Message not recognized ID={msg.Id}, type={msg.Type}");
            }
        }
示例#2
0
        public static IRequestHandler InitRequestHandler(
            Request request,
            ClientConnection clientConnection,
            ClientRequestService clientRequestService,
            IAppServiceProvider appServiceProvider)
        {
            IRequestHandler requestHandler;

            switch (request.RequestType)
            {
            case RequestType.VerificationUser:
            {
                requestHandler = new VerificationUserRequestHandler(
                    request,
                    clientConnection,
                    appServiceProvider.LoadUsersService,
                    appServiceProvider.UpdateUsersService,
                    appServiceProvider.VerificationCodesService,
                    appServiceProvider.SmsService);
            }
            break;

            case RequestType.NewUser:
            {
                requestHandler = new NewUserRequestHandler(
                    request,
                    clientConnection,
                    appServiceProvider.NodeNoticeService,
                    appServiceProvider.CreateUsersService,
                    appServiceProvider.TokensService,
                    appServiceProvider.VerificationCodesService,
                    appServiceProvider.LoadUsersService);
            }
            break;

            case RequestType.Login:
            {
                requestHandler = new LoginRequestHandler(
                    request,
                    clientConnection,
                    appServiceProvider.ConnectionsService,
                    appServiceProvider.NoticeService,
                    appServiceProvider.TokensService,
                    appServiceProvider.LoadUsersService,
                    appServiceProvider.PendingMessagesService,
                    appServiceProvider.NodeRequestSender);
            }
            break;

            case RequestType.GetMessages:
            {
                requestHandler = new GetMessagesRequestHandler(
                    request,
                    clientConnection,
                    appServiceProvider.ConnectionsService,
                    appServiceProvider.LoadMessagesService,
                    appServiceProvider.CreateMessagesService,
                    appServiceProvider.AttachmentsService,
                    appServiceProvider.LoadChatsService,
                    appServiceProvider.ConversationsService,
                    appServiceProvider.NodeRequestSender);
            }
            break;

            case RequestType.Logout:
            {
                requestHandler = new LogoutRequestHandler(request, clientConnection, appServiceProvider.ConnectionsService, appServiceProvider.TokensService);
            }
            break;

            case RequestType.SendMessages:
            {
                requestHandler = new SendMessagesRequestHandler(
                    request,
                    clientConnection,
                    appServiceProvider.NodeNoticeService,
                    appServiceProvider.ConversationsNoticeService,
                    appServiceProvider.AttachmentsService,
                    appServiceProvider.CreateMessagesService,
                    appServiceProvider.LoadDialogsService);
            }
            break;

            case RequestType.NewChats:
            {
                requestHandler = new NewChatRequestHandler(
                    request,
                    clientConnection,
                    appServiceProvider.NodeNoticeService,
                    appServiceProvider.ConversationsNoticeService,
                    appServiceProvider.CreateChatsService);
            }
            break;

            case RequestType.EditUser:
            {
                requestHandler = new EditUserRequestHandler(request, clientConnection, appServiceProvider.NodeNoticeService, appServiceProvider.UpdateUsersService);
            }
            break;

            case RequestType.DeleteConversation:
            {
                requestHandler = new DeleteConversationsRequestHandler(
                    request,
                    clientConnection,
                    appServiceProvider.NodeNoticeService,
                    appServiceProvider.LoadChatsService,
                    appServiceProvider.DeleteChatsService,
                    appServiceProvider.LoadDialogsService,
                    appServiceProvider.DeleteDialogsService,
                    appServiceProvider.LoadChannelsService,
                    appServiceProvider.DeleteChannelsService);
            }
            break;

            case RequestType.RefreshTokens:
            {
                requestHandler = new RefreshTokensRequestHandler(request, clientConnection, appServiceProvider.TokensService);
            }
            break;

            case RequestType.DownloadFile:
            {
                throw new WebSocketNotSupportException();
            }

            case RequestType.GetUserFilesInformation:
            {
                requestHandler = new GetUserFilesRequestHandler(request, clientConnection, appServiceProvider.FilesService);
            }
            break;

            case RequestType.GetInformationNode:
            {
                requestHandler = new GetInformationNodeRequestHandler(request, appServiceProvider.NodesService);
            }
            break;

            case RequestType.EditChats:
            {
                requestHandler = new EditChatsRequestHandler(
                    request,
                    clientConnection,
                    appServiceProvider.NodeNoticeService,
                    appServiceProvider.ConversationsNoticeService,
                    appServiceProvider.UpdateChatsService,
                    appServiceProvider.LoadChatsService,
                    appServiceProvider.SystemMessagesService);
            }
            break;

            case RequestType.UploadFile:
            {
                throw new WebSocketNotSupportException();
            }

            case RequestType.AddUsersChats:
            {
                requestHandler = new AddUserChatsRequestHandler(
                    request,
                    clientConnection,
                    appServiceProvider.NodeNoticeService,
                    appServiceProvider.ConversationsNoticeService,
                    appServiceProvider.UpdateChatsService,
                    appServiceProvider.LoadChatsService,
                    appServiceProvider.NodeRequestSender,
                    appServiceProvider.ConnectionsService,
                    appServiceProvider.SystemMessagesService);
            }
            break;

            case RequestType.MessagesRead:
            {
                requestHandler = new MessagesReadRequestHandler(
                    request,
                    clientConnection,
                    appServiceProvider.NodeNoticeService,
                    appServiceProvider.ConversationsNoticeService,
                    appServiceProvider.UpdateMessagesService,
                    appServiceProvider.LoadDialogsService);
            }
            break;

            case RequestType.GetUsersInformationNode:
            {
                requestHandler = new GetUsersInfoNodeRequestHandler(request, appServiceProvider.LoadUsersService, appServiceProvider.PrivacyService);
            }
            break;

            case RequestType.GetChatsInformationNode:
            {
                requestHandler = new GetChatsInfoNodeRequestHandler(request, appServiceProvider.LoadChatsService);
            }
            break;

            case RequestType.GetInformationWeb:
            {
                requestHandler = new GetInformationWebRequestHandler(request, appServiceProvider.NodesService);
            }
            break;

            case RequestType.DeleteUser:
            {
                requestHandler = new DeleteUserRequestHandler(
                    request,
                    clientConnection,
                    appServiceProvider.NodeNoticeService,
                    appServiceProvider.DeleteUsersService,
                    appServiceProvider.VerificationCodesService,
                    appServiceProvider.ConnectionsService);
            }
            break;

            case RequestType.BlockUsers:
            {
                requestHandler = new BlockUsersRequestHandler(request, clientConnection, appServiceProvider.NodeNoticeService, appServiceProvider.UpdateUsersService);
            }
            break;

            case RequestType.GetSelf:
            {
                requestHandler = new GetSelfRequestHandler(request, clientConnection, appServiceProvider.LoadUsersService);
            }
            break;

            case RequestType.GetChatUsers:
            {
                requestHandler = new GetChatUsersRequestHandler(
                    request,
                    clientConnection,
                    appServiceProvider.LoadChatsService,
                    appServiceProvider.PrivacyService,
                    appServiceProvider.ConnectionsService,
                    appServiceProvider.NodeRequestSender,
                    appServiceProvider.CrossNodeService);
            }
            break;

            case RequestType.EditChatUsers:
            {
                requestHandler = new EditChatUsersRequestHandler(
                    request,
                    clientConnection,
                    appServiceProvider.NodeNoticeService,
                    appServiceProvider.ConversationsNoticeService,
                    appServiceProvider.LoadChatsService,
                    appServiceProvider.UpdateChatsService,
                    appServiceProvider.SystemMessagesService);
            }
            break;

            case RequestType.UnblockUsers:
            {
                requestHandler = new UnblockUsersRequestHandler(request, clientConnection, appServiceProvider.NodeNoticeService, appServiceProvider.UpdateUsersService);
            }
            break;

            case RequestType.GetFilesInformation:
            {
                requestHandler = new GetFilesInfoRequestHandler(request, clientConnection, appServiceProvider.FilesService);
            }
            break;

            case RequestType.GetAllUserConversationsNode:
            {
                requestHandler = new GetAllUserConversationsNodeRequestHandler(request, clientConnection, appServiceProvider.ConversationsService);
            }
            break;

            case RequestType.DeleteFiles:
            {
                requestHandler = new DeleteFilesRequestHandler(request, clientConnection, appServiceProvider.NodeNoticeService, appServiceProvider.FilesService);
            }
            break;

            case RequestType.GetUsers:
            {
                requestHandler = new GetUsersRequestHandler(request, clientConnection, appServiceProvider.ConnectionsService, appServiceProvider.LoadUsersService, appServiceProvider.PrivacyService, appServiceProvider.NodeRequestSender);
            }
            break;

            case RequestType.DeleteMessages:
            {
                requestHandler = new DeleteMessagesRequestHandler(
                    request,
                    clientConnection,
                    appServiceProvider.NodeNoticeService,
                    appServiceProvider.ConversationsNoticeService,
                    appServiceProvider.DeleteMessagesService,
                    appServiceProvider.LoadChatsService,
                    appServiceProvider.PendingMessagesService,
                    appServiceProvider.LoadChannelsService,
                    appServiceProvider.LoadDialogsService);
            }
            break;

            case RequestType.GetMessagesUpdates:
            {
                requestHandler = new GetMessagesUpdatesRequestHandler(request, clientConnection, appServiceProvider.LoadMessagesService);
            }
            break;

            case RequestType.GetChats:
            {
                requestHandler = new GetChatsRequestHandler(request, appServiceProvider.LoadChatsService, clientConnection);
            }
            break;

            case RequestType.GetDialogsInformation:
            {
                requestHandler = new GetDialogsInformationRequestHandler(request, clientConnection, appServiceProvider.LoadDialogsService);
            }
            break;

            case RequestType.Search:
            {
                requestHandler = new SearchRequestHandler(
                    request,
                    clientConnection,
                    appServiceProvider.ConnectionsService,
                    appServiceProvider.LoadChatsService,
                    appServiceProvider.NodesService,
                    appServiceProvider.LoadUsersService,
                    appServiceProvider.LoadChannelsService,
                    appServiceProvider.PrivacyService,
                    appServiceProvider.NodeRequestSender);
            }
            break;

            case RequestType.SetNewKeys:
            {
                requestHandler = new SetNewKeysRequestHandler(request, clientConnection, appServiceProvider.NodeNoticeService, appServiceProvider.KeysService);
            }
            break;

            case RequestType.DeleteKeys:
            {
                requestHandler = new DeleteKeysRequestHandler(request, clientConnection, appServiceProvider.NodeNoticeService, appServiceProvider.KeysService);
            }
            break;

            case RequestType.GetRandomSequence:
            {
                requestHandler = new GetRandomSequenceRequestHandler(request, clientConnection);
            }
            break;

            case RequestType.SetNewKeyForChat:
            {
                requestHandler = new SetNewKeysRequestHandler(request, clientConnection, appServiceProvider.NodeNoticeService, appServiceProvider.KeysService);
            }
            break;

            case RequestType.GetUserPublicKeys:
            {
                requestHandler = new GetUserPublicKeysRequestHandler(request, clientConnection, appServiceProvider.KeysService);
            }
            break;

            case RequestType.CreateChannel:
            {
                requestHandler = new CreateChannelRequestHandler(
                    request,
                    clientConnection,
                    appServiceProvider.NodeNoticeService,
                    appServiceProvider.ConversationsNoticeService,
                    appServiceProvider.LoadChannelsService,
                    appServiceProvider.CreateChannelsService);
            }
            break;

            case RequestType.AddUsersToChannels:
            {
                requestHandler = new AddUsersToChannelsRequestHandler(
                    request,
                    clientConnection,
                    appServiceProvider.NodeNoticeService,
                    appServiceProvider.ConversationsNoticeService,
                    appServiceProvider.UpdateChannelsService,
                    appServiceProvider.LoadChannelsService,
                    appServiceProvider.ConnectionsService,
                    appServiceProvider.NodeRequestSender);
            }
            break;

            case RequestType.EditChannel:
            {
                requestHandler = new EditChannelRequestHandler(
                    request,
                    clientConnection,
                    appServiceProvider.NodeNoticeService,
                    appServiceProvider.ConversationsNoticeService,
                    appServiceProvider.LoadChannelsService,
                    appServiceProvider.UpdateChannelsService,
                    appServiceProvider.SystemMessagesService);
            }
            break;

            case RequestType.EditChannelUsers:
            {
                requestHandler = new EditChannelUsersRequestHandler(
                    request,
                    clientConnection,
                    appServiceProvider.NodeNoticeService,
                    appServiceProvider.LoadChannelsService,
                    appServiceProvider.UpdateChannelsService);
            }
            break;

            case RequestType.GetChannelUsers:
            {
                requestHandler = new GetChannelUsersRequestHandler(request, clientConnection, appServiceProvider.LoadChannelsService);
            }
            break;

            case RequestType.GetChannels:
            {
                requestHandler = new GetChannelsRequestHandler(request, clientConnection, appServiceProvider.LoadChannelsService);
            }
            break;

            case RequestType.SetConnectionEncrypted:
            {
                requestHandler = new SetConnectionEncryptedRequestHandler(request, clientConnection,
                                                                          appServiceProvider.ConnectionsService,
                                                                          appServiceProvider.KeysService,
                                                                          appServiceProvider.NodeRequestSender);
            }
            break;

            case RequestType.ChangeNode:
            {
                requestHandler = new ChangeNodeRequestHandler(request, clientConnection, appServiceProvider.ChangeNodeOperationsService);
            }
            break;

            case RequestType.Polling:
            {
                requestHandler = new PollingRequestHandler(
                    request,
                    clientConnection,
                    appServiceProvider.NodeNoticeService,
                    appServiceProvider.LoadChatsService,
                    appServiceProvider.PollsService,
                    appServiceProvider.LoadChannelsService);
            }
            break;

            case RequestType.GetPollVotedUsers:
            {
                requestHandler = new GetPollVotedUsersRequestHandler(request, clientConnection,
                                                                     appServiceProvider.ConnectionsService,
                                                                     appServiceProvider.PollsService,
                                                                     appServiceProvider.NodeRequestSender);
            }
            break;

            case RequestType.EditMessage:
            {
                requestHandler = new EditMessageRequestHandler(
                    request,
                    clientConnection,
                    appServiceProvider.NodeNoticeService,
                    appServiceProvider.ConversationsNoticeService,
                    appServiceProvider.UpdateMessagesService,
                    appServiceProvider.AttachmentsService);
            }
            break;

            case RequestType.CreateOrEditContact:
            {
                requestHandler = new CreateOrEditContactRequestHandler(request, clientConnection, appServiceProvider.ContactsService);
            }
            break;

            case RequestType.DeleteContacts:
            {
                requestHandler = new DeleteContactsRequestHandler(request, clientConnection, appServiceProvider.ContactsService);
            }
            break;

            case RequestType.GetUserContacts:
            {
                requestHandler = new GetUserContactsRequestHandler(request, clientConnection,
                                                                   appServiceProvider.ConnectionsService,
                                                                   appServiceProvider.ContactsService,
                                                                   appServiceProvider.NodeRequestSender);
            }
            break;

            case RequestType.CreateOrEditGroup:
            {
                requestHandler = new CreateOrEditGroupRequestHandler(request, clientConnection, appServiceProvider.GroupsService);
            }
            break;

            case RequestType.DeleteGroup:
            {
                requestHandler = new DeleteGroupRequestHandler(request, clientConnection, appServiceProvider.GroupsService);
            }
            break;

            case RequestType.AddUsersToGroup:
            {
                requestHandler = new AddUsersToGroupRequestHandler(request, clientConnection, appServiceProvider.GroupsService);
            }
            break;

            case RequestType.RemoveUsersFromGroup:
            {
                requestHandler = new RemoveUsersFromGroupRequestHandler(request, clientConnection, appServiceProvider.GroupsService);
            }
            break;

            case RequestType.GetGroupContacts:
            {
                requestHandler = new GetGroupContactsRequestHandler(request, clientConnection,
                                                                    appServiceProvider.ConnectionsService,
                                                                    appServiceProvider.GroupsService,
                                                                    appServiceProvider.NodeRequestSender);
            }
            break;

            case RequestType.GetUserGroups:
            {
                requestHandler = new GetUserGroupsRequestHandler(request, clientConnection, appServiceProvider.GroupsService);
            }
            break;

            case RequestType.GetMessageEditHistory:
            {
                requestHandler = new GetMessageEditHistoryRequestHandler(request, clientConnection, appServiceProvider.LoadMessagesService);
            }
            break;

            case RequestType.GetFavorites:
            {
                requestHandler = new GetFavoritesRequestHandler(request, clientConnection, appServiceProvider.FavoritesService);
            }
            break;

            case RequestType.AddFavorites:
            {
                requestHandler = new AddFavoritesRequestHandler(request, clientConnection, appServiceProvider.FavoritesService);
            }
            break;

            case RequestType.EditFavorites:
            {
                requestHandler = new EditFavoritesRequestHandler(request, clientConnection, appServiceProvider.FavoritesService);
            }
            break;

            case RequestType.VerifyNode:
            {
                requestHandler = new VerifyNodeRequestHandler(request);
            }
            break;

            case RequestType.GetDevicesPrivateKeys:
            {
                requestHandler = new GetDevicesPrivateKeysRequestHandler(request, clientConnection, clientRequestService, appServiceProvider.ConnectionsService);
            }
            break;

            case RequestType.GetSessions:
            {
                requestHandler = new GetSessionsRequestHandler(request, clientConnection, appServiceProvider.LoadUsersService);
            }
            break;

            case RequestType.SearchMessages:
            {
                requestHandler = new SearchMessagesRequestHandler(request, clientConnection, appServiceProvider.LoadMessagesService);
            }
            break;

            case RequestType.MuteConversation:
            {
                requestHandler = new MuteConversationRequestHandler(request, clientConnection, appServiceProvider.ConversationsService);
            }
            break;

            case RequestType.GetQRCode:
            {
                requestHandler = new GetQRCodeRequestHandler(request, clientConnection, appServiceProvider.QRCodesService);
            }
            break;

            case RequestType.CheckQRCode:
            {
                requestHandler = new CheckQRCodeRequestHandler(
                    request,
                    clientConnection,
                    appServiceProvider.NoticeService,
                    appServiceProvider.QRCodesService,
                    appServiceProvider.LoadUsersService);
            }
            break;

            case RequestType.BatchPhonesSearch:
            {
                requestHandler = new BatchPhonesSearchRequestHandler(request, clientConnection,
                                                                     appServiceProvider.LoadUsersService,
                                                                     appServiceProvider.ConnectionsService,
                                                                     appServiceProvider.PrivacyService,
                                                                     appServiceProvider.NodeRequestSender);
            }
            break;

            case RequestType.ChangeEmailOrPhone:
            {
                requestHandler = new ChangeEmailOrPhoneRequestHandler(request, clientConnection,
                                                                      appServiceProvider.UpdateUsersService,
                                                                      appServiceProvider.VerificationCodesService,
                                                                      appServiceProvider.LoadUsersService);
            }
            break;

            case RequestType.DeleteAllMessages:
            {
                requestHandler = new DeleteAllMessagesRequestHandler(request, clientConnection, appServiceProvider.DeleteMessagesService, appServiceProvider.NodeNoticeService);
            }
            break;

            case RequestType.ConversationAction:
            {
                requestHandler = new ConversationActionRequestHandler(request,
                                                                      clientConnection,
                                                                      appServiceProvider.ConversationsService,
                                                                      appServiceProvider.ConversationsNoticeService,
                                                                      appServiceProvider.NodeNoticeService,
                                                                      appServiceProvider.LoadDialogsService,
                                                                      appServiceProvider.SystemMessagesService);
            }
            break;

            default:
                throw new UnknownRequestTypeException();
            }
            return(requestHandler);
        }