public static void SendFileWithStream(long fileSize, string path, StreamCommunication streamCommunication)
        {
            long fileParts   = ProtocolHelpers.CalculateFileParts(fileSize);
            long offset      = 0;
            long currentPart = 1;

            while (fileSize > offset)
            {
                byte[] data;
                if (currentPart == fileParts)
                {
                    var lastPartSize = (int)(fileSize - offset);
                    data    = FileStreamHandler.Read(path, offset, lastPartSize);
                    offset += lastPartSize;
                }
                else
                {
                    data    = FileStreamHandler.Read(path, offset, ProtocolConstants.MaxPacketSize);
                    offset += ProtocolConstants.MaxPacketSize;
                }

                streamCommunication.Write(data);
                currentPart++;
            }
        }
Ejemplo n.º 2
0
        public static void SendCommentData(StreamCommunication streamCommunication, Comment comment)
        {
            var nameData    = ConversionHandler.ConvertStringToBytes(comment.Commentator.Name, User.UserNameLength);
            var emailData   = ConversionHandler.ConvertStringToBytes(comment.Commentator.Email, User.UserEmailLength);
            var commentData = ConversionHandler.ConvertStringToBytes(comment.Message, Comment.CommentLength);

            streamCommunication.Write(nameData);
            streamCommunication.Write(emailData);
            streamCommunication.Write(commentData);
        }
Ejemplo n.º 3
0
        public static void SendUserData(StreamCommunication streamCommunication, User user)
        {
            var nameData           = ConversionHandler.ConvertStringToBytes(user.Name, User.UserNameLength);
            var emailData          = ConversionHandler.ConvertStringToBytes(user.Email, User.UserEmailLength);
            var lastConnectionData = ConversionHandler.ConvertLongToBytes(user.LastConnection.ToBinary());

            streamCommunication.Write(nameData);
            streamCommunication.Write(emailData);
            streamCommunication.Write(lastConnectionData);
        }
Ejemplo n.º 4
0
        public static async Task <MessageResponse> RecieveMessageCommand(StreamCommunication streamCommunication)
        {
            var response        = ConversionHandler.ConvertBytesToShort(await streamCommunication.ReadAsync(ProtocolConstants.ShortTypeLength));
            var responseCommand = ConversionHandler.ConvertBytesToShort(await streamCommunication.ReadAsync(ProtocolConstants.ShortTypeLength));
            var responseMessage = ConversionHandler.ConvertBytesToString(await streamCommunication.ReadAsync(ProtocolConstants.ResponseMessageLength));

            return(new MessageResponse()
            {
                Message = responseMessage,
                responseCommands = (ProtocolConstants.ResponseCommands)responseCommand
            });
        }
Ejemplo n.º 5
0
        public static void SendPhotoData(StreamCommunication streamCommunication, Photo photo)
        {
            var email    = ConversionHandler.ConvertStringToBytes(photo.User.Email, User.UserEmailLength);
            var photoId  = ConversionHandler.ConvertLongToBytes(photo.Id);
            var nameData = ConversionHandler.ConvertStringToBytes(photo.Name, Photo.PhotoNameLength);
            var extData  = ConversionHandler.ConvertStringToBytes(photo.Extension, Photo.PhotoExtensionLength);
            var sizeData = ConversionHandler.ConvertLongToBytes(photo.FileSize);

            streamCommunication.Write(email);
            streamCommunication.Write(photoId);
            streamCommunication.Write(nameData);
            streamCommunication.Write(extData);
            streamCommunication.Write(sizeData);
        }
        public static async Task ReceiveFileWithStreams(long fileSize, string fileName, StreamCommunication streamCommunication)
        {
            long fileParts   = ProtocolHelpers.CalculateFileParts(fileSize);
            long offset      = 0;
            long currentPart = 1;

            while (fileSize > offset)
            {
                byte[] data;
                if (currentPart == fileParts)
                {
                    var lastPartSize = (int)(fileSize - offset);
                    data = await streamCommunication.ReadAsync(lastPartSize);

                    offset += lastPartSize;
                }
                else
                {
                    data = await streamCommunication.ReadAsync(ProtocolConstants.MaxPacketSize);

                    offset += ProtocolConstants.MaxPacketSize;
                }

                FileStreamHandler.Write(fileName, data);
                currentPart++;
            }
        }
Ejemplo n.º 7
0
        public static void SendRequestCommand(ProtocolConstants.RequestCommands command, StreamCommunication streamCommunication)
        {
            var requestData = ConversionHandler.ConvertCommandToByte(ProtocolConstants.Commands.REQUEST);
            var commandData = ConversionHandler.ConvertRequestCommandToByte(command);

            streamCommunication.Write(requestData);
            streamCommunication.Write(commandData);
        }