public void TestReadWrite() { var buffer = new byte[1024 * 1024]; for (int i = 0; i < buffer.Length; i++) { buffer[i] = (byte)(i % byte.MaxValue); } using (var stream = new FrameStream(512)) using (var mem = new MemoryStream(new byte[1024 * 1024], 0, 1024 * 1024, true, true)) { foreach (var b in buffer) { stream.WriteByte(b); } stream.Seek(0, SeekOrigin.Begin); var read = new byte[1024 * 1024]; stream.Read(read, 0, read.Length); Assert.AreEqual(buffer, read); stream.Seek(0, SeekOrigin.Begin); stream.CopyTo(mem); Assert.AreEqual(buffer, mem.GetBuffer()); } }
public async Task <int> RespondAsync(GenericContext rpcContext, CancellationToken token) { using (var responseData = new FrameStream()) using (var encoder = new BinaryEncoder(responseData)) { if (DoHandshake()) { HANDSHAKE_RESPONSE_WRITER.Write(encoder, rpcContext.HandshakeResponse); _handshakePending = rpcContext.HandshakeResponse.match == HandshakeMatch.BOTH; } META_WRITER.Write(encoder, EMPTY_META); encoder.WriteBoolean(rpcContext.IsError); if (rpcContext.IsError) { _protocol.WriteError(encoder, rpcContext.MessageName, rpcContext.Error); } else { _protocol.WriteReponse(encoder, rpcContext.MessageName, rpcContext.Response); } encoder.WriteBytes(END_OF_FRAME); responseData.Seek(0, SeekOrigin.Begin); return(await _tranceiver.SendAsync(responseData, token)); } }
public Stream ReadFile(String name) { var entry = _fileDict[name]; var stream = new FrameStream(_stream, entry.Offset, entry.Size); stream.Seek(0, SeekOrigin.Begin); return(stream); }
private async Task <GenericContext> Request(GenericContext rpcContext, CancellationToken token) { using (var requestData = new FrameStream()) using (var encoder = new BinaryEncoder(requestData)) { if (DoHandshake()) { if (rpcContext.HandshakeRequest == null) { rpcContext.HandshakeRequest = NewHandshakeRequest((MD5)Protocol.MD5, (MD5)RemoteProtocol.MD5); } HANDSHAKE_REQUEST_WRITER.Write(encoder, rpcContext.HandshakeRequest); } META_WRITER.Write(encoder, EMPTY_META); encoder.WriteString(rpcContext.MessageName); _protocol.WriteRequest(encoder, rpcContext.MessageName, rpcContext.RequestParameters); encoder.WriteBytes(END_OF_FRAME); requestData.Seek(0, SeekOrigin.Begin); using (var responseData = await _tranceiver.RequestAsync(rpcContext.MessageName, requestData, token)) using (var decode = new BinaryDecoder(responseData)) { responseData.Seek(0, SeekOrigin.Begin); if (DoHandshake()) { rpcContext.HandshakeResponse = HANDSHAKE_RESPONSE_READER.Read(decode); _handshakePending = rpcContext.HandshakeResponse.match == HandshakeMatch.NONE; var remoteProtocol = default(AvroProtocol); if (rpcContext.HandshakeResponse.match == HandshakeMatch.CLIENT || rpcContext.HandshakeResponse.match == HandshakeMatch.NONE) { remoteProtocol = AvroParser.ReadProtocol(rpcContext.HandshakeResponse.serverProtocol); _protocol = new GenericRequestor(Protocol, remoteProtocol); } if (rpcContext.HandshakeResponse.match == HandshakeMatch.NONE) { rpcContext.HandshakeRequest.serverHash = (MD5)remoteProtocol.MD5; rpcContext.HandshakeRequest.clientProtocol = Protocol.ToAvroCanonical(); _protocol = new GenericRequestor(Protocol, remoteProtocol); rpcContext = await Request(rpcContext, token); } } rpcContext.Metadata = META_READER.Read(decode); rpcContext.IsError = decode.ReadBoolean(); if (rpcContext.IsError) { rpcContext.Error = _protocol.ReadError <object>(decode, rpcContext.MessageName); } else { rpcContext.Response = _protocol.ReadResponse <object>(decode, rpcContext.MessageName); } return(rpcContext); } } }