Esempio n. 1
0
 public void GetFully1()
 {
     buf = new FlexBuffer(new byte[] { 1, 2, 3, 4, 5, 6 });
     byte[] b = new byte[3];
     buf.GetFully(b);
     Assert.AreEqual(( byte )1, b[0]);
     Assert.AreEqual(( byte )2, b[1]);
     Assert.AreEqual(( byte )3, b[2]);
     buf.GetFully(b);
     Assert.AreEqual(( byte )4, b[0]);
     Assert.AreEqual(( byte )5, b[1]);
     Assert.AreEqual(( byte )6, b[2]);
     CheckBuf(6, 6, 0);
 }
Esempio n. 2
0
 public void GetShort1()
 {
     buf = new FlexBuffer(new byte[] { 2, 1, unchecked (( byte )-2), unchecked (( byte )-1) });
     if (FlexBuffer.littleEndian)
     {
         Assert.AreEqual(( short )0x0102, buf.GetShort());
         Assert.AreEqual(unchecked (( short )0xfffe), buf.GetShort());
     }
     else
     {
         Assert.AreEqual(( short )0x0201, buf.GetShort());
         Assert.AreEqual(unchecked (( short )0xfeff), buf.GetShort());
     }
     CheckBuf(4, 4, 0);
 }
Esempio n. 3
0
 public void GetInt1()
 {
     buf = new FlexBuffer(new byte[] { 4, 3, 2, 1, unchecked (( byte )-4), unchecked (( byte )-3),
                                       unchecked (( byte )-2), unchecked (( byte )-1) });
     if (FlexBuffer.littleEndian)
     {
         Assert.AreEqual(0x01020304, buf.GetInt());
         Assert.AreEqual(unchecked (( int )0xfffefdfc), buf.GetInt());
     }
     else
     {
         Assert.AreEqual(0x04030201, buf.GetInt());
         Assert.AreEqual(unchecked (( int )0xfcfdfeff), buf.GetInt());
     }
     CheckBuf(8, 8, 0);
 }
Esempio n. 4
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;
            }
        }
Esempio n. 5
0
        public void GetLong1()
        {
            buf = new FlexBuffer(new byte[] { 8, 7, 6, 5, 4, 3, 2, 1, unchecked (( byte )-8), unchecked (( byte )-7),
                                              unchecked (( byte )-6), unchecked (( byte )-5), unchecked (( byte )-4), unchecked (( byte )-3),
                                              unchecked (( byte )-2), unchecked (( byte )-1) });
            if (FlexBuffer.littleEndian)
            {
                Assert.AreEqual(0x0102030405060708L, buf.GetLong());
                Assert.AreEqual(unchecked (( long )0xfffefdfcfbfaf9f8L), buf.GetLong());
            }
            else
            {
                Assert.AreEqual(0x0807060504030201L, buf.GetLong());
                Assert.AreEqual(unchecked (( long )0xf8f9fafbfcfdfeffL), buf.GetLong());
            }

            CheckBuf(16, 16, 0);
        }
Esempio n. 6
0
        protected override void ReadSocket()
        {
            try
            {
                while (IsStarted())
                {
                    IPEndPoint senderEndPoint = remoteIpEndPoint;
                    byte[]     receiveBytes   = null;

                    if (readQueue != null)
                    {
                        UdpPacket packet = readQueue.Take();

                        senderEndPoint = packet.IPEndPoint;
                        receiveBytes   = packet.Bytes;
                    }
                    else
                    {
                        receiveBytes = udpClient.Receive(ref senderEndPoint);
                    }

                    WhoSender  sender     = new WhoSender(senderEndPoint);
                    FlexBuffer receiveBuf = new FlexBuffer(receiveBytes);

                    session.SessionPacket(sender, receiveBuf);
                }
            }
            catch (ArgumentNullException)
            {
                // ignore
            }
            catch (SocketException ex)
            {
                if (ex.SocketErrorCode != SocketError.Interrupted)
                {
                    throw ex;
                }
            }
        }
Esempio n. 7
0
        private int ProcessHeader(FlexBuffer buf, bool reset)
        {
            int sig = buf.GetInt();

            if (sig != SIG)
            {
                throw new Exception("bad SIG");
            }

            int pktSize = buf.GetInt();

            if (reset)
            {
                buf.Reset();
            }

            if (pktSize < 0 || (maxPktSize > 0 && pktSize > maxPktSize))
            {
                throw new Exception("pktSize < 0 || (maxPktSize > 0 && pktSize > maxPktSize)");
            }

            return(pktSize);
        }
Esempio n. 8
0
 public void Skip4()
 {
     buf = new FlexBuffer(new byte[] { 1, 2 });
     buf.Skip(2, false);
     CheckBuf(2, 2, 0);
 }
Esempio n. 9
0
 public void Initial4()
 {
     buf = new FlexBuffer(new byte[5], 1, 3);
     CheckBuf(4, 1, 3);
 }
Esempio n. 10
0
 public void Initial3()
 {
     buf = new FlexBuffer(new byte[5], 2);
     CheckBuf(2, 0, 2);
 }
Esempio n. 11
0
 public void Initial2()
 {
     buf = new FlexBuffer(new byte[5]);
     CheckBuf(5, 0, 5);
 }
Esempio n. 12
0
 public void Initial1()
 {
     buf = new FlexBuffer();
     CheckBuf(0, 0, 0);
 }
Esempio n. 13
0
 public void TransportData(Who recipient, FlexBuffer buf)
 {
     Send(buf.GetBuf(), buf.Index(), buf.Avail());
 }
Esempio n. 14
0
 public void Skip3()
 {
     buf = new FlexBuffer(new byte[] { });
     buf.Skip(0, false);
     CheckBuf(0, 0, 0);
 }
Esempio n. 15
0
 public void Skip2()
 {
     buf = new FlexBuffer(new byte[] { 1, 2 });
     buf.Skip(3, false);
 }
Esempio n. 16
0
 public void Skip1()
 {
     buf = new FlexBuffer(new byte[] { });
     buf.Skip(-1, false);
 }
Esempio n. 17
0
 public void GetFully2()
 {
     buf = new FlexBuffer(new byte[] { 1, 2 });
     byte[] b = new byte[3];
     buf.GetFully(b);
 }
Esempio n. 18
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;
            }
        }
Esempio n. 19
0
 private void FireData(FlexBuffer buf)
 {
     session.SessionData(null, buf);
 }
Esempio n. 20
0
 public void Skip6()
 {
     buf = new FlexBuffer(new byte[] { 1, 2 });
     buf.Skip(1, true);
     CheckBuf(2, 1, 1);
 }
Esempio n. 21
0
 public void Skip7()
 {
     buf = new FlexBuffer(new byte[] { 1, 2 });
     buf.Skip(5, true);
     CheckBuf(5, 5, 0);
 }
Esempio n. 22
0
        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);
        }