本文整理汇总了C#中Message.ToStream方法的典型用法代码示例。如果您正苦于以下问题:C# Message.ToStream方法的具体用法?C# Message.ToStream怎么用?C# Message.ToStream使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Message
的用法示例。
在下文中一共展示了Message.ToStream方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CompleteUpload
private void CompleteUpload(Guid transferId, Salt sessionKey, string location, Hash fullHash)
{
// STEP 4: Finalize the transfer
using (Message req = new Message(TransferState.UploadCompleteRequest, transferId, _publicKey, s => sessionKey))
{
req.Write(location);
req.Write(fullHash.ToArray());
Stream response = SendPayload(req, location, req.ToStream(_privateKey));
using (Message rsp = new Message(response, _privateKey, s => sessionKey))
{
Check.Assert<InvalidOperationException>(rsp.State == TransferState.UploadCompleteResponse);
rsp.VerifySignature(_publicKey);
}
}
}
示例2: 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);
}
}
}
}
示例3: TransferBytes
private void TransferBytes(Guid transferId, Salt sessionKey, string location, long offset, byte[] bytes)
{
// STEP 3...n: Send a block of bytes
using (Message req = new Message(TransferState.SendBytesRequest, transferId, _publicKey, s => sessionKey))
{
req.Write(offset);
req.Write(bytes);
Stream response = SendPayload(req, location, req.ToStream(_privateKey));
using (Message rsp = new Message(response, _privateKey, s => sessionKey))
{
Check.Assert<InvalidOperationException>(rsp.State == TransferState.SendBytesResponse);
Check.Assert<InvalidOperationException>(rsp.ReadInt64() == offset);
rsp.VerifySignature(_publicKey);
}
}
}
示例4: BeginUpload
private Salt BeginUpload(Guid transferId, string location, long length, out int maxMessageLength)
{
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 sessionSecret = SessionSecret(clientKeyBits, serverKeyBits);
using (Message req = new Message(TransferState.UploadRequest, transferId, _publicKey, NoSession))
{
req.Write(hnonce.ToArray());
req.Write(length);
req.Write(location);
req.Write(clientKeyBits.ToArray());
Stream response = SendPayload(req, location, req.ToStream(_privateKey));
using (Message rsp = new Message(response, _privateKey, s=>sessionSecret))
{
Check.Assert<InvalidOperationException>(rsp.State == TransferState.UploadResponse);
maxMessageLength = Check.InRange(rsp.ReadInt32(), 0, int.MaxValue);
rsp.VerifySignature(_publicKey);
}
}
return sessionSecret;
}
示例5: 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;
}
示例6: 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;
}