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);
            }
        }
예제 #2
0
 public void writeBytes(ByteBuf buf, int maxSize)
 {
     if (buf.remainBytes() > maxSize)
     {
         writeBytes(buf.getBuffer(), buf.getReaderIndex(), maxSize);
         buf.skipReaderIndex(maxSize);
     }
     else
     {
         writeBytes(buf);
     }
 }
예제 #3
0
        public void writeBytes(ByteBuf buf)
        {
            int maxSize     = getCapacity() - writerIndex;
            int remainBytes = buf.remainBytes();

            if (remainBytes < maxSize)
            {
                maxSize = remainBytes;
            }
            writeBytes(buf.getBuffer(), buf.getReaderIndex(), maxSize);
            buf.skipReaderIndex(maxSize);
        }
        public override void invokeWrite(object message)
        {
            ByteBuf buf = (ByteBuf)message;

            /*
             * var output = new System.IO.FileStream(Application.dataPath + "\\buf.txt", System.IO.FileMode.Append);
             * var writer = new System.IO.StreamWriter(output, System.Text.Encoding.UTF8);
             * writer.WriteLine("buf.remainBytes() = " + buf.remainBytes().ToString());
             * writer.Close();
             * output.Close();
             */



            Socket clientSocket = this.connection.getClientSocket();

            clientSocket.Send(buf.getBuffer(), buf.getReaderIndex(), buf.remainBytes(), SocketFlags.None);
        }
예제 #5
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);
            }
        }
예제 #6
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;
                }
            }
        }