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); }
public void PutDouble1() { buf = new FlexBuffer(new byte[] { }); buf.PutDouble(Double.MaxValue); CheckBuf(8, 8, 0); buf.SetIndex(0); Assert.AreEqual(Double.MaxValue, buf.GetDouble()); CheckBuf(8, 8, 0); }
public void PutShort1() { buf = new FlexBuffer(new byte[] { }); buf.PutShort(short.MaxValue); CheckBuf(2, 2, 0); buf.SetIndex(0); Assert.AreEqual(short.MaxValue, buf.GetShort()); CheckBuf(2, 2, 0); }
public void PutShort2() { int max = 4 * 1024 * 1024; buf = new FlexBuffer(new byte[max]); buf.SetIndex(max); buf.PutShort(( short )1); CheckBuf(max + 2, max + 2, 0); }
public void PutLong0() { buf = new FlexBuffer(new byte[] { }); buf.PutLong(long.MaxValue); CheckBuf(8, 8, 0); buf.SetIndex(0); Assert.AreEqual(long.MaxValue, buf.GetLong()); CheckBuf(8, 8, 0); }
public void PutLong1() { int max = 4 * 1024 * 1024; buf = new FlexBuffer(new byte[max]); buf.SetIndex(max); buf.PutLong(long.MinValue); CheckBuf(max + 8, max + 8, 0); }
public void PutInt1() { buf = new FlexBuffer(new byte[] { }); buf.PutInt(int.MaxValue); CheckBuf(4, 4, 0); buf.SetIndex(0); Assert.AreEqual(int.MaxValue, buf.GetInt()); CheckBuf(4, 4, 0); }
public void PutInt2() { int max = 4 * 1024 * 1024; buf = new FlexBuffer(new byte[max]); buf.SetIndex(max); buf.PutInt(int.MinValue); CheckBuf(max + 4, max + 4, 0); }
public void PutByte1() { buf = new FlexBuffer(new byte[] { }); buf.PutByte(( byte )0x01); CheckBuf(1, 1, 0); buf.SetIndex(0); Assert.AreEqual(( byte )0x01, buf.GetByte()); CheckBuf(1, 1, 0); }
public void PutDouble2() { int max = 4 * 1024 * 1024; buf = new FlexBuffer(new byte[max]); buf.SetIndex(max); buf.PutDouble(Double.MinValue); CheckBuf(max + 8, max + 8, 0); }
public void PutByte2() { int max = 4 * 1024 * 1024; buf = new FlexBuffer(new byte[max]); buf.SetIndex(max); buf.PutByte(( byte )0x01); CheckBuf(max + 1, max + 1, 0); }
public void PutFlexBuffer3() { FlexBuffer buf0 = new FlexBuffer(new byte[] { 1, 2 }); buf = new FlexBuffer(new byte[] { }); buf.SetIndex(0); buf.Put(buf0, 3); CheckBuf(3, 3, 0); }
public void PutFlexBuffer5() { FlexBuffer buf0 = new FlexBuffer(new byte[] { 1, 2 }); int max = 4 * 1024 * 1024; buf = new FlexBuffer(new byte[max]); buf.SetIndex(max); buf.Put(buf0, 1); CheckBuf(max + 1, max + 1, 0); }
public void PutFlexBuffer2() { FlexBuffer buf0 = new FlexBuffer(new byte[] { 1, 2 }); int max = 4 * 1024 * 1024; buf = new FlexBuffer(new byte[max]); buf.SetIndex(max); buf.Put(buf0); CheckBuf(max + buf0.Length(), max + buf0.Length(), 0); }
protected override void ReadSocket() { Stream ns = checkStream(); FlexBuffer buf = new FlexBuffer(new byte[8192]); try { while (IsStarted()) { int n = ns.Read(buf.GetBuf(), 0, buf.Length()); if (n <= 0) { break; } buf.SetLength(n); buf.SetIndex(0); FireData(buf); } } catch (Exception e) { if (e.Message == null) { throw; } if (e.Message.Contains("connection was aborted")) { return; } if (e.Message.Contains("blocking operation")) { return; } if (e.Message.Contains("socket closed")) { return; } if (e.Message.Contains("read operation failed")) { return; } throw; } }
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); }
public void SetIndex0() { buf.Reset(); buf.SetIndex(0); CheckBuf(0, 0, 0); }
protected override void ReadSocket() { //set the language to English in order to make sure the following //string comparisons work also with an installed .Net Framework Language Pack //save users current CI CultureInfo userCI = Thread.CurrentThread.CurrentCulture; //set CI to English Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US"); Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture; Stream ns = checkStream(); FlexBuffer buf = new FlexBuffer(new byte[8192]); try { while (IsStarted()) { int n = ns.Read(buf.GetBuf(), 0, buf.Length()); if (n <= 0) { break; } buf.SetLength(n); buf.SetIndex(0); FireData(buf); } } catch (Exception e) { string errorMessage = e.Message; if (errorMessage == null) { throw; } if (errorMessage.Contains("connection was aborted")) { return; } if (errorMessage.Contains("blocking operation")) { return; } if (errorMessage.Contains("socket closed")) { return; } if (errorMessage.Contains("read operation failed")) { return; } throw; } finally { //set CI back to the users CI Thread.CurrentThread.CurrentCulture = userCI; Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture; } }