当前位置: 首页>>代码示例>>C#>>正文


C# Message.ReadBytes方法代码示例

本文整理汇总了C#中Message.ReadBytes方法的典型用法代码示例。如果您正苦于以下问题:C# Message.ReadBytes方法的具体用法?C# Message.ReadBytes怎么用?C# Message.ReadBytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Message的用法示例。


在下文中一共展示了Message.ReadBytes方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CompleteRequest

            private Message CompleteRequest(Message req)
            {
                try
                {
                    string name = req.ReadString(1024);
                    Hash contentHash = Hash.FromBytes(req.ReadBytes(32));
                    req.VerifySignature(_clientKey);

                    long totalSize = long.Parse(ReadState(req.TransferId, "total-length"));
                    string location = ReadState(req.TransferId, "location");
                    Check.Assert<InvalidDataException>(location == name);

                    OnCompleteTransfer(req.TransferId, location, totalSize, contentHash);

                    Message response = new Message(TransferState.UploadCompleteResponse, req.TransferId, _clientKey, SessionKey);
                    return response;
                }
                finally
                {
                    Delete(req.TransferId);
                }
            }
开发者ID:hivie7510,项目名称:csharptest-net,代码行数:22,代码来源:SecureTransferServer.cs

示例2: DownloadRequest

            private Message DownloadRequest(Message req)
            {
                Check.Assert<InvalidDataException>(HasState(req.TransferId, "start-time")
                    && HasState(req.TransferId, "nonce")
                    && HasState(req.TransferId, "server-key")
                    && !HasState(req.TransferId, "session-key"));

                byte[] nonceExpected = Convert.FromBase64String(ReadState(req.TransferId, "nonce"));
                DeleteState(req.TransferId, "nonce");

                byte[] serverKeyBits = Convert.FromBase64String(ReadState(req.TransferId, "server-key"));
                DeleteState(req.TransferId, "server-key");

                byte[] hnonce = req.ReadBytes(32);
                string name = req.ReadString(1024);
                byte[] clientKeyBits = req.ReadBytes(32);
                req.VerifySignature(_clientKey);

                Check.Assert<InvalidDataException>(
                    Hash.SHA256(nonceExpected).Equals(Hash.FromBytes(hnonce))
                );

                Salt sessionKey = SessionSecret(clientKeyBits, serverKeyBits);
                long length;
                OnDownloadBytes(req.TransferId, name, out length, 0, 0, Stream.Null);

                Message response = new Message(TransferState.DownloadResponse, req.TransferId, _clientKey, s => sessionKey);
                response.Write(MaxOutboundFileChunk);
                response.Write(length);
                if(length <= MaxOutboundFileChunk)
                {
                    Delete(req.TransferId);
                    using (MemoryStream ms = new MemoryStream())
                    {
                        OnDownloadBytes(req.TransferId, name, out length, 0, (int)length, ms);
                        Check.Assert<InvalidDataException>(ms.Position == length);
                        response.Write(ms.ToArray());
                    }
                }
                else
                {
                    WriteState(req.TransferId, "total-length", length.ToString());
                    WriteState(req.TransferId, "location", name ?? String.Empty);
                    WriteState(req.TransferId, "session-key", sessionKey.ToString());
                    response.Write(new byte[0]);
                }

                return response;
            }
开发者ID:hivie7510,项目名称:csharptest-net,代码行数:49,代码来源:SecureTransferServer.cs

示例3: SendBytesRequest

            private Message SendBytesRequest(Message req)
            {
                long position = req.ReadInt64();
                byte[] bytes = req.ReadBytes(MaxInboundFileChunk);
                req.VerifySignature(_clientKey);

                string location = ReadState(req.TransferId, "location");
                long totalSize = long.Parse(ReadState(req.TransferId, "total-length"));
                Check.InRange(position, 0, totalSize - bytes.Length);

                OnBytesReceived(req.TransferId, location, totalSize, position, bytes);

                Message response = new Message(TransferState.SendBytesResponse, req.TransferId, _clientKey, SessionKey);
                response.Write(position);
                return response;
            }
开发者ID:hivie7510,项目名称:csharptest-net,代码行数:16,代码来源:SecureTransferServer.cs

示例4: TransferRequest

            private Message TransferRequest(Message req)
            {
                Check.Assert<InvalidDataException>(HasState(req.TransferId, "start-time")
                    && HasState(req.TransferId, "nonce")
                    && HasState(req.TransferId, "server-key")
                    && !HasState(req.TransferId, "session-key"));

                byte[] nonceExpected = Convert.FromBase64String(ReadState(req.TransferId, "nonce"));
                DeleteState(req.TransferId, "nonce");

                byte[] serverKeyBits = Convert.FromBase64String(ReadState(req.TransferId, "server-key"));
                DeleteState(req.TransferId, "server-key");

                byte[] hnonce = req.ReadBytes(32);
                long length = req.ReadInt64();
                string name = req.ReadString(1024);
                byte[] clientKeyBits = req.ReadBytes(32);
                req.VerifySignature(_clientKey);

                Check.Assert<InvalidDataException>(
                    Hash.SHA256(nonceExpected).Equals(Hash.FromBytes(hnonce))
                );

                OnBeginTransfer(req.TransferId, name, length);

                WriteState(req.TransferId, "total-length", length.ToString());
                WriteState(req.TransferId, "location", name ?? String.Empty);
                WriteState(req.TransferId, "session-key", SessionSecret(clientKeyBits, serverKeyBits).ToString());

                Message response = new Message(TransferState.UploadResponse, req.TransferId, _clientKey, SessionKey);
                response.Write(MaxInboundFileChunk);
                return response;
            }
开发者ID:hivie7510,项目名称:csharptest-net,代码行数:33,代码来源:SecureTransferServer.cs

示例5: ReadByteRange

            private void ReadByteRange(Guid transferId, Salt sessionKey, string location, StreamCache streams, long offset, int count)
            {
                using (Message req = new Message(TransferState.DownloadBytesRequest, transferId, _publicKey, s=>sessionKey))
                {
                    req.Write(location);
                    req.Write(offset);
                    req.Write(count);

                    Stream response = SendPayload(req, location, req.ToStream(_privateKey));
                    using (Message rsp = new Message(response, _privateKey, s=>sessionKey))
                    {
                        Check.Assert<InvalidOperationException>(rsp.State == TransferState.DownloadBytesResponse);
                        byte[] bytes = rsp.ReadBytes(100 * 1000 * 1024);
                        Check.Assert<InvalidOperationException>(bytes.Length == count);
                        
                        rsp.VerifySignature(_publicKey);

                        using(Stream io = streams.Open(FileAccess.Write))
                        {
                            io.Seek(offset, SeekOrigin.Begin);
                            io.Write(bytes, 0, count);
                        }
                    }
                }
            }
开发者ID:hivie7510,项目名称:csharptest-net,代码行数:25,代码来源:SecureTransferClient.cs

示例6: Download

            private bool Download(string location, StreamCache output)
            {
                int maxMessageLength;
                long fileLength, bytesReceived = 0;
                Guid transferId = Guid.NewGuid();
                byte[] serverKeyBits;
                byte[] nonce = GetNonce(transferId, location, out serverKeyBits);
                Hash hnonce = Hash.SHA256(nonce);

                //STEP 2: Create and send session key
                Salt clientKeyBits = new Salt(Salt.Size.b256);
                Salt sessionKey = SessionSecret(clientKeyBits, serverKeyBits);

                using (Message req = new Message(TransferState.DownloadRequest, transferId, _publicKey, NoSession))
                {
                    req.Write(hnonce.ToArray());
                    req.Write(location);
                    req.Write(clientKeyBits.ToArray());

                    Stream response = SendPayload(req, location, req.ToStream(_privateKey));
                    using (Message rsp = new Message(response, _privateKey, s=>sessionKey))
                    {
                        Check.Assert<InvalidOperationException>(rsp.State == TransferState.DownloadResponse);
                        maxMessageLength = Check.InRange(rsp.ReadInt32(), 0, int.MaxValue);
                        fileLength = Check.InRange(rsp.ReadInt64(), 0, 0x00FFFFFFFFFFFFFFL);
                        byte[] bytes = rsp.ReadBytes(100 * 1000 * 1024);
                        rsp.VerifySignature(_publicKey);

                        using(Stream io = output.Open(FileAccess.Write))
                        {
                            io.SetLength(fileLength);
                            if (bytes.Length > 0)
                            {
                                io.Seek(0, SeekOrigin.Begin);
                                io.Write(bytes, 0, bytes.Length);
                                bytesReceived += bytes.Length;
                            }
                        }
                    }
                }
                //STEP 3...n: Continue downloading other chunks of the file
                if (bytesReceived < fileLength)
                {
                    bool[] failed = new bool[1];
                    using (WorkQueue queue = new WorkQueue(LimitThreads))
                    {
                        queue.OnError += (o, e) => { failed[0] = true; };
                        while (bytesReceived < fileLength && !failed[0] && !_abort.WaitOne(0, false))
                        {
                            int len = (int) Math.Min(fileLength - bytesReceived, maxMessageLength);
                            BytesToRead task = new BytesToRead(
                                this, LimitThreads, transferId, sessionKey, location, output, bytesReceived, len);
                            queue.Enqueue(task.Send);
                            OnProgressChanged(location, bytesReceived, fileLength);
                            bytesReceived += len;
                        }

                        queue.Complete(true, failed[0] ? 5000 : 7200000);
                    }

                    if (_abort.WaitOne(0, false))
                        return false;
                    Check.Assert<InvalidDataException>(failed[0] == false);

                    // STEP 4: Complete the transfer
                    using (Message req = new Message(TransferState.DownloadCompleteRequest, transferId, _publicKey, s => sessionKey))
                    {
                        SendPayload(req, location, req.ToStream(_privateKey)).Dispose();
                    }
                }
                OnProgressChanged(location, fileLength, fileLength);
                return true;
            }
开发者ID:hivie7510,项目名称:csharptest-net,代码行数:73,代码来源:SecureTransferClient.cs

示例7: GetNonce

 private byte[] GetNonce(Guid transferId, string location, out byte[] serverKeyBits)
 {
     byte[] nonce;
     // STEP 1: Send a NonceRequest
     using (Message req = new Message(TransferState.NonceRequest, transferId, _publicKey, NoSession))
     {
         Stream response = SendPayload(req, location, req.ToStream(_privateKey));
         using (Message rsp = new Message(response, _privateKey, NoSession))
         {
             Check.Assert<InvalidOperationException>(rsp.State == TransferState.NonceResponse);
             nonce = rsp.ReadBytes(1024);
             serverKeyBits = rsp.ReadBytes(1024);
             rsp.VerifySignature(_publicKey);
         }
     }
     return nonce;
 }
开发者ID:hivie7510,项目名称:csharptest-net,代码行数:17,代码来源:SecureTransferClient.cs


注:本文中的Message.ReadBytes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。