public void TransportPacket(Who recipient, FlexBuffer buf) { // Data-ize the packet // assert index is at the start of the header. int dataSize = buf.Avail(); if (dataSize < HEADER_SIZE) { throw new ArgumentException("dataSize < HEADER_SIZE"); } int pktSize = dataSize - HEADER_SIZE; if (maxPktSize > 0 && pktSize > maxPktSize) { throw new ArgumentException("maxPktSize > 0 && pktSize > maxPktSize"); } int index = buf.Index(); buf.PutInt(SIG); buf.PutInt(pktSize); buf.SetIndex(index); transport.TransportData(recipient, buf); }
/// <summary> /// Copies the Available bytes from buf into buffer as if by /// repeated execution of put( buf.Get() ). /// </summary> /// <param name="buf">the source of the bytes to put. All Available /// bytes are copied.</param> /// <returns>flex buffer object.</returns> /// Exception: /// IOException if the buffer overflows its max length. public FlexBuffer Put(FlexBuffer buf) { int n = buf.Avail(); Put(buf.buffer, buf.index, n); buf.Skip(n, false); return(this); }
/// <summary> /// Copies the specified number of bytes from buf into buffer /// as if by repeated execution of Put( buf.Get() ). /// </summary> /// <param name="buf">the source of the bytes to put.</param> /// <param name="len"></param>len the number of bytes to put. Len must be /// less than or equal to buf.Avail(). /// <returns>flex buffer object.</returns> /// Exception: /// IOException if the buffer overflows its max length. public FlexBuffer Put(FlexBuffer buf, int len) { if (len > buf.Avail()) { throw new ArgumentOutOfRangeException("len > buf.Avail()"); } Put(buf.buffer, buf.index, len); buf.Skip(len, false); return(this); }
public void TransportData(Who recipient, FlexBuffer buf) { Send(buf.GetBuf(), buf.Index(), buf.Avail()); }
// General test function private void CheckBuf(int length, int index, int avail) { Assert.AreEqual(length, buf.Length()); Assert.AreEqual(index, buf.Index()); Assert.AreEqual(avail, buf.Avail()); }
public void SessionData(Who sender, FlexBuffer buf) { while (buf.Avail() > 0) { if (wantHeader) { // do we have enough to make a header if ((savedBuf.Length() + buf.Avail()) >= HEADER_SIZE) { int pktSize; if (savedBuf.Length() == 0) { // savedBuf is empty, entire header in buf. pktSize = ProcessHeader(buf, false); } else // header split across savedBuf and buf { // move just enough data from buf to savedBuf to have a header. int needFromBuf = HEADER_SIZE - savedBuf.Length(); savedBuf.Put(buf, needFromBuf); savedBuf.SetIndex(0); pktSize = ProcessHeader(savedBuf, true); } if (pktSize == 0) { continue; } bodyLen = pktSize; wantHeader = false; } else // want header but not enough space to make it { // save buf in savedBuf. savedBuf.SetIndex(savedBuf.Length()); savedBuf.Put(buf); } } else if ((savedBuf.Length() + buf.Avail()) >= bodyLen) { // want body, and there's enough to make it. // three possible cases: the body is entirely in savedBuf, // the body is split, or the body is entirely in buf. assert // that the body cannot entirely be in savedBuf, or else // we'd have processed it last time. Debug.Assert(savedBuf.Length() < bodyLen); if (savedBuf.Length() == 0) { // savedBuf is empty, entire body in buf. int length = buf.Length(); int index = buf.Index(); buf.SetLength(index + bodyLen); // handler.Packet(sender, buf); session.SessionPacket(sender, buf); buf.SetLength(length); buf.SetIndex(index + bodyLen); wantHeader = true; } else // body split across savedBuf and buf { // move just enough data from buf to savedBuf to have a body. int needFromBuf = bodyLen - savedBuf.Length(); savedBuf.Put(buf, needFromBuf); savedBuf.SetIndex(0); // handler.Packet(sender, savedBuf); session.SessionPacket(sender, savedBuf); savedBuf.Reset(); wantHeader = true; } } else // want body, but there's not enough to make it. { // save buf in savedBuf. savedBuf.Put(buf); } } // buf is now empty, and there's nothing else to do. Debug.Assert(buf.Avail() == 0); }