public LengthFieldPrepender()
 {
     tempBuf = ByteBuf.allocOne(MAX_SIZE);
 }
Example #2
0
        public bool UpdateLogic()
        {
            if (!connected)
            {
                return(false);
            }
            // Check Receive
            ByteBuf buf = recvBuf;

            buf.Clear();
            int bytes = 0;

            try
            {
                if (clientSocket.Poll(0, SelectMode.SelectRead))
                {
                    bytes = clientSocket.Available;
                    if (bytes == 0)
                    {
                        connected = false;
                        pipeLine.fireDeactive();
                    }
                    else
                    {
                        bytes = clientSocket.Receive(buf.getBuffer());
                    }
                }
            }
            catch (SocketException exp)
            {
                {
                    UnityEngine.Debug.LogException(exp);
                }
            }

            if (bytes > 0)
            {
                buf.setWriteIndex(bytes);
                pipeLine.fireRead(buf);
                pipeLine.readComplete();
            }
            if (!connected)
            {
                return(false);
            }
            // 写 线程!!
            TimeSpan timeSpan = new TimeSpan(DateTime.Now.Ticks - lastWritePingTicks);

            if (timeSpan.TotalSeconds > PingTime)
            {
                lastWritePingTicks = DateTime.Now.Ticks;
                if (PingProc != null)
                {
                    PingProc();
                }
            }
            object data = null;

            lock (this)
            {
                if (dataToWriteList.Count > 0)
                {
                    data = dataToWriteList.Dequeue();
                }
            }

            if (data != null)
            {
                pipeLine.write(data);
            }
            return(connected);
        }
Example #3
0
        public override void read(ConnectionHandlerContext ctx, object msg)
        {
            ByteBuf inputBuf = (ByteBuf)msg;

            //UnityEngine.Debug.LogError("Read the Raw Message!!!! size = " + inputBuf.remainBytes());
            while (true)
            {
                //   UnityEngine.Debug.LogError("Loop Read Raw Message!!!! size = " + inputBuf.remainBytes());
                if (targetSize <= 0)
                {
                    targetSize = 0;
                    // The target size is not read complete
                    int nowsize = receivedBuf.remainBytes();
                    if (nowsize < LengthBytes)
                    {
                        int needSizeForLength = LengthBytes - nowsize;
                        for (int i = 0; i < needSizeForLength; ++i)
                        {
                            if (inputBuf.remainBytes() > 0)
                            {
                                receivedBuf.writeByte(inputBuf.readByte());
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    nowsize = receivedBuf.remainBytes();
                    //                  System.Console.WriteLine("nowSize " + nowsize);
                    if (nowsize >= LengthBytes)
                    {
                        if (LengthBytes == 2)
                        {
                            targetSize = receivedBuf.readUInt16();
                        }
                        else if (LengthBytes == 4)
                        {
                            targetSize = (int)receivedBuf.readUInt32();
                        }
                        else if (LengthBytes == 8)
                        {
                            targetSize = (int)receivedBuf.readUInt64();
                        }
                        else
                        {
                            throw new Exception("Big Errror!!!!!!! , not engouh size bytes!!!");
                        }
                    }
                    else
                    {
                        // note read the length complete ,wait for more bytes
                        if (inputBuf.remainBytes() > 0)
                        {
                            throw new Exception("Big Errror!!!!!!!");
                        }
                        return;
                    }
                }
                // the targetSize must be ready
                if (targetSize <= 0)
                {
                    throw new Exception("the target size ERROR,that's must be a bug!");
                }



                //receivedBuf.writeBytes(inputBuf);
                for (int i = 0; i < targetSize && inputBuf.remainBytes() > 0; ++i)
                {
                    receivedBuf.writeByte(inputBuf.readByte());
                }
                // UnityEngine.Debug.LogError("write the Receive Message!!!! now size " + receivedBuf.remainBytes() + "target Size " + targetSize);

                if (receivedBuf.remainBytes() >= targetSize)
                {
                    //    UnityEngine.Debug.LogError("write the Receive Message!!!!    Begin");

                    ctx.fireRead(receivedBuf);
                    //  UnityEngine.Debug.LogError("write the Receive Message!!!!    End");
                    receivedBuf.DiscardReadedBytes();
                    // UnityEngine.Debug.LogError("Deal after  Receive Message!!!! now size " + receivedBuf.remainBytes());
                    targetSize = 0;
                }


                //   System.Console.WriteLine("TargetSize " + targetSize);
                if (inputBuf.remainBytes() == 0)
                {
                    break;
                }
            }
        }
Example #4
0
        public override void read(ConnectionHandlerContext ctx, object msg)
        {
            ByteBuf inputBuf = (ByteBuf)msg;

            System.Console.WriteLine(inputBuf.ToString());
        }
Example #5
0
 public LengthFieldBasedFrameDecoder()
 {
     receivedBuf = ByteBuf.allocOne(MAX_SIZE);
 }