Ejemplo n.º 1
0
        public override void write(ConnectionHandlerContext ctx, object msg)
        {
            if (msg is ByteBuf)
            {
                tempBuf.Clear();
                ByteBuf buf = (ByteBuf)msg;
                if (LengthBytes == 2)
                {
                    ushort len = (ushort)buf.remainBytes();
                    tempBuf.writeUInt16(len);
                }
                else if (LengthBytes == 4)
                {
                    uint len = (uint)buf.remainBytes();
                    tempBuf.writeUInt32(len);
                }
                else if (LengthBytes == 8)
                {
                    ulong len = (ulong)buf.remainBytes();
                    tempBuf.writeUInt64(len);
                }
                tempBuf.writeBytes(buf);

                ctx.fireWrite(tempBuf);
            }
        }
 public void setPreHandler(ConnectionHandlerContext ctx)
 {
     ctx.next = this;
     ctx.pre  = this.pre;
     if (this.pre != null)
     {
         this.pre.next = ctx;
     }
     this.pre = ctx;
 }
 public void setNextHandler(ConnectionHandlerContext ctx)
 {
     ctx.next = this.next;
     ctx.pre  = this;
     if (this.next != null)
     {
         this.next.pre = ctx;
     }
     this.next = ctx;
 }
        public void fireWrite(Object message)
        {
            ConnectionHandlerContext ctx = nextOutboundContext();

            if (ctx == null)
            {
                return;
            }
            ctx.invokeWrite(message);
        }
Ejemplo n.º 5
0
        public void fireDeactive()
        {
            ConnectionHandlerContext ctx = tail;

            while (ctx != null)
            {
                ctx.Deactive();
                ctx = ctx.getPreHandler();
            }
        }
Ejemplo n.º 6
0
        public void fireActive()
        {
            ConnectionHandlerContext ctx = header;

            while (ctx != null)
            {
                ctx.Active();
                ctx = ctx.getNextHandler();
            }
        }
        public ConnectionHandlerContext nextInboundContext()
        {
            ConnectionHandlerContext ctx = this;

            do
            {
                ctx = ctx.next;
            } while (ctx != null && !ctx.isInbound);
            return(ctx);
        }
Ejemplo n.º 8
0
        public override void  write(ConnectionHandlerContext ctx, object msg)
        {
            tempBuf.MakeBufferEmpty();
            memStream.Position = 0;
            ProtoBuf.Serializer.Serialize(memStream, msg);
            int writeIndex = (int)memStream.Position;

            tempBuf.setWriteIndex(writeIndex);

            ctx.fireWrite(tempBuf);
        }
        public void fireRead(Object message)
        {
            ConnectionHandlerContext ctx = nextInboundContext();

            if (ctx == null)
            {
                return;
            }
//			System.Console.WriteLine ("receive bytes fire read");
            ctx.invokeRead(message);
        }
Ejemplo n.º 10
0
        public override void read(ConnectionHandlerContext ctx, object msg)
        {
            ByteBuf buf = (ByteBuf)msg;
            //System.Console.WriteLine("receive bytes " + buf.remainBytes());
            MemoryStream ms  = new MemoryStream(buf.getBuffer(), buf.getReaderIndex(), buf.remainBytes());
            T            obj = ProtoBuf.Serializer.Deserialize <T>(ms);

            buf.skipReaderIndex((int)ms.Position);
            if (receiveObjFunc != null)
            {
                receiveObjFunc(obj);
            }
        }
Ejemplo n.º 11
0
 public override void deactive(ConnectionHandlerContext ctx)
 {
 }
Ejemplo n.º 12
0
 public override void deactive(ConnectionHandlerContext ctx)
 {
     System.Console.WriteLine("Connection is Closed!");
 }
Ejemplo n.º 13
0
        public override void read(ConnectionHandlerContext ctx, object msg)
        {
            ByteBuf inputBuf = (ByteBuf)msg;

            System.Console.WriteLine(inputBuf.ToString());
        }
Ejemplo n.º 14
0
        public void addLast(string name, ConnectionHandler handler)
        {
            ConnectionHandlerContext ctx = new ConnectionHandlerContext(name, handler);

            tail.setPreHandler(ctx);
        }
Ejemplo n.º 15
0
 public virtual void read(ConnectionHandlerContext ctx, object msg)
 {
     throw new NotImplementedException();
 }
        public void readComplete()
        {
            ConnectionHandlerContext ctx = nextInboundContext();

            ctx.invokeReadComplete();
        }
Ejemplo n.º 17
0
 public virtual void deactive(ConnectionHandlerContext ctx)
 {
 }
Ejemplo n.º 18
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;
                }
            }
        }
Ejemplo n.º 19
0
 public virtual void readComplete(ConnectionHandlerContext ctx)
 {
 }
Ejemplo n.º 20
0
        public void addFirst(string name, ConnectionHandler handler)
        {
            ConnectionHandlerContext ctx = new ConnectionHandlerContext(name, handler);

            header.setNextHandler(ctx);
        }