Exemple #1
0
        private void FileUploadResponseHandler(FileUploadResponse response)
        {
            FileUploadRequest request = new FileUploadRequest(response);

            if (!response.HasError)
            {
                if (request.CurrentPosition == 0)
                {
                    var encryptionkey = clientPublicEncryptionkeys[request.ReceiverClient];

                    //MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(request.SourceFilePath);
                    //MemoryMappedViewStream mms = mmf.CreateViewStream();
                    using (FileStream fs = File.Open(request.SourceFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                        using (BufferedStream bs = new BufferedStream(fs))
                            using (BinaryReader fileStream = new BinaryReader(bs))
                            {
                                var bytes = new byte[request.BufferSize];
                                // Read and verify the data
                                int j = 0;
                                for (long i = 0; i < request.TotalBytes; i++, j++)
                                {
                                    bytes[j] = fileStream.ReadByte();
                                    if (j == request.BufferSize - 1 || i == request.TotalBytes - 1)
                                    {
                                        request = new FileUploadRequest(request);

                                        var tmpbyte = new byte[j + 1];
                                        Array.Copy(bytes, 0, tmpbyte, 0, j + 1);

                                        request.BytesToWrite    = UtilityFunction.EncryptByte(tmpbyte, encryptionkey.FirstX, encryptionkey.U, encryptionkey.SelectChoas);
                                        request.DataLength      = j + 1;
                                        request.CurrentPosition = i + 1;
                                        SendMessage(request);

                                        j = -1;
                                    }
                                }
                                fileStream.Close();
                                fileStream.Dispose();
                            }
                    //mms.Dispose();
                    //mmf.Dispose();
                }
            }
            else
            {
                CleanMessageQueueFromFile(response);
            }
        }
Exemple #2
0
        private void SessionResponseHandler(SessionResponse response)
        {
            if (response.IsConfirmed)
            {
                if (clientPublicKey.ContainsKey(response.ClientName))
                {
                    clientPublicKey[response.ClientName] = response.PublicKey;
                }
                else
                {
                    clientPublicKey.Add(response.ClientName, response.PublicKey);
                }
                HandShakeRequest    handShakeRequest    = new HandShakeRequest();
                EncryptionkeyObject encryptionkeyObject = new EncryptionkeyObject();
                Random rnd = new Random();

                encryptionkeyObject.FirstX      = rnd.NextDouble();
                encryptionkeyObject.U           = rnd.NextDouble();
                encryptionkeyObject.SelectChoas = rnd.Next(0, 2) == 0 ? ChoasEnum.First : ChoasEnum.Secend;

                if (clientPublicEncryptionkeys.ContainsKey(response.ClientName))
                {
                    clientPublicEncryptionkeys[response.ClientName] = encryptionkeyObject;
                }
                else
                {
                    clientPublicEncryptionkeys.Add(response.ClientName, encryptionkeyObject);
                }

                handShakeRequest.FirstX         = UtilityFunction.EncryptByte(response.PublicKey, BitConverter.GetBytes(encryptionkeyObject.FirstX));
                handShakeRequest.U              = UtilityFunction.EncryptByte(response.PublicKey, BitConverter.GetBytes(encryptionkeyObject.U));
                handShakeRequest.SelectChoas    = UtilityFunction.EncryptByte(response.PublicKey, BitConverter.GetBytes((short)encryptionkeyObject.SelectChoas));
                handShakeRequest.ReceiverClient = response.ClientName;

                SendMessage(handShakeRequest);
            }
        }
Exemple #3
0
        public static void AppendAllBytes(Client client, List <FileUploadRequest> fileUploadRequests, Dictionary <string, EncryptionkeyObject> clientPublicEncryptionkeys)
        {
            if (fileUploadRequests.Count > 0)
            {
                var item0 = fileUploadRequests[0];

                var encryptionkey = clientPublicEncryptionkeys[item0.SenderClient];
                var dycriptdata   = UtilityFunction.EncryptByte(item0.BytesToWrite, encryptionkey.FirstX, encryptionkey.U, encryptionkey.SelectChoas);

                FileStream fs = new FileStream(item0.DestinationFilePath, FileMode.Append, FileAccess.Write);

                FileUploadResponse response = new FileUploadResponse(item0);
                if (fs.Position != item0.CurrentPosition - item0.DataLength)
                {
                    response.HasError  = true;
                    response.Exception = new Exception("The file sequence not correct!");
                    client.SendMessage(response);
                    fileUploadRequests.RemoveAll(x => x.DestinationFilePath == item0.DestinationFilePath);
                    //fileUploadRequests.RemoveAt(0);
                    fs.Dispose();
                    File.Delete(item0.DestinationFilePath);
                    return;
                }
                else
                {
                    client.SendMessage(response);
                    client.OnUploadFileProgress(new EventArguments.FileUploadProgressEventArguments()
                    {
                        CurrentPosition = item0.CurrentPosition, FileName = item0.FileName, TotalBytes = item0.TotalBytes, DestinationPath = item0.DestinationFilePath
                    });
                }
                fs.Write(dycriptdata, 0, item0.DataLength);
                fileUploadRequests.RemoveAt(0);
                while (fileUploadRequests.Count > 0 && fileUploadRequests[0].DestinationFilePath == item0.DestinationFilePath)
                {
                    item0       = fileUploadRequests[0];
                    dycriptdata = UtilityFunction.EncryptByte(item0.BytesToWrite, encryptionkey.FirstX, encryptionkey.U, encryptionkey.SelectChoas);

                    response = new FileUploadResponse(item0);
                    if (fs.Position != item0.CurrentPosition - item0.DataLength)
                    {
                        response.HasError  = true;
                        response.Exception = new Exception("The file sequence not correct!");
                        client.SendMessage(response);
                        fileUploadRequests.RemoveAll(x => x.DestinationFilePath == item0.DestinationFilePath);
                        //fileUploadRequests.RemoveAt(0);
                        fs.Dispose();
                        File.Delete(item0.DestinationFilePath);
                        return;
                    }
                    else
                    {
                        client.SendMessage(response);
                        client.OnUploadFileProgress(new EventArguments.FileUploadProgressEventArguments()
                        {
                            CurrentPosition = item0.CurrentPosition, FileName = item0.FileName, TotalBytes = item0.TotalBytes, DestinationPath = item0.DestinationFilePath
                        });
                    }

                    fs.Write(dycriptdata, 0, item0.DataLength);
                    fileUploadRequests.RemoveAt(0);
                }
                fs.Dispose();
            }
        }