public void TestReadWriteTwrite() { var stream = new MemoryStream(); var utf8 = new UTF8Encoding(); var message = new Twrite( 1, 65535L, (uint)utf8.GetByteCount("test"), utf8.GetBytes("test")) { Tag = 1248 }; var p = new Protocol.Protocol(stream); p.Write(message); stream.Position = 0; var data = new byte[message.Length]; var length = stream.Read(data, 0, (int)message.Length); Assert.That(length, Is.EqualTo(message.Length)); stream.Position = 0; var message2 = (Twrite)p.Read(); Assert.That(message, Is.EqualTo(message2)); }
public uint Write(uint fid, ulong offset, uint count, byte[] data) { var request = new Twrite(fid, offset, count, data) { Tag = (ushort)_tagQueue.Dequeue() }; _protocol.Write(request); var r = _protocol.Read(); if (r.Tag != request.Tag) { throw new TagMismatchException(r.Tag, request.Tag); } Rwrite response; try { response = (Rwrite)r; } catch (InvalidCastException) { if (r.Type != (byte)MessageType.Rerror) { throw new UnexpectedMessageException(request.Type, r.Type); } var err = (Rerror)r; throw new ServerErrorException(err.Ename); } _tagQueue.Enqueue(request.Tag); return(response.Count); }
public Message Read() { Message message; var bytes = ReadMessage(); var offset = Constants.Bit32Sz; var type = bytes[offset]; switch (type) { case (byte)MessageType.Tversion: message = new Tversion(bytes); break; case (byte)MessageType.Rversion: message = new Rversion(bytes); break; case (byte)MessageType.Tauth: message = new Tauth(bytes); break; case (byte)MessageType.Rauth: message = new Rauth(bytes); break; case (byte)MessageType.Tattach: message = new Tattach(bytes); break; case (byte)MessageType.Rattach: message = new Rattach(bytes); break; case (byte)MessageType.Rerror: message = new Rerror(bytes); break; case (byte)MessageType.Tflush: message = new Tflush(bytes); break; case (byte)MessageType.Rflush: message = new Rflush(bytes); break; case (byte)MessageType.Twalk: message = new Twalk(bytes); break; case (byte)MessageType.Rwalk: message = new Rwalk(bytes); break; case (byte)MessageType.Topen: message = new Topen(bytes); break; case (byte)MessageType.Ropen: message = new Ropen(bytes); break; case (byte)MessageType.Tcreate: message = new Tcreate(bytes); break; case (byte)MessageType.Rcreate: message = new Rcreate(bytes); break; case (byte)MessageType.Tread: message = new Tread(bytes); break; case (byte)MessageType.Rread: message = new Rread(bytes); break; case (byte)MessageType.Twrite: message = new Twrite(bytes); break; case (byte)MessageType.Rwrite: message = new Rwrite(bytes); break; case (byte)MessageType.Tclunk: message = new Tclunk(bytes); break; case (byte)MessageType.Rclunk: message = new Rclunk(bytes); break; case (byte)MessageType.Tremove: message = new Tremove(bytes); break; case (byte)MessageType.Rremove: message = new Rremove(bytes); break; case (byte)MessageType.Tstat: message = new Tstat(bytes); break; case (byte)MessageType.Rstat: message = new Rstat(bytes); break; case (byte)MessageType.Twstat: message = new Twstat(bytes); break; case (byte)MessageType.Rwstat: message = new Rwstat(bytes); break; default: throw new Exception("Unsupported Message Type"); } return(message); }