Esempio n. 1
0
        // 处理格式
        protected unsafe virtual void OnThreadBufferProcess(byte[] recvBuffer, int recvBufSz)
        {
            if (recvBuffer == null || recvBuffer.Length <= 0 || recvBufSz <= 0)
            {
                return;
            }
            if (recvBufSz > recvBuffer.Length)
            {
                recvBufSz = recvBuffer.Length;
            }

            GamePackHeader header     = new GamePackHeader();
            int            headerSize = Marshal.SizeOf(header);

            //    IntPtr headerBuffer = Marshal.AllocHGlobal(headerSize);
            try {
                if (recvBufSz >= headerSize)
                {
                    byte *headerBuffer = (byte *)&header;
                    Marshal.Copy(recvBuffer, 0, (IntPtr)headerBuffer, headerSize);
                    //  header = (GamePackHeader)Marshal.PtrToStructure(headerBuffer, typeof(GamePackHeader));
#if USE_NETORDER
                    // used Net
                    header.headerCrc32 = (uint)IPAddress.NetworkToHostOrder(header.headerCrc32);
                    header.dataCrc32   = (uint)IPAddress.NetworkToHostOrder(header.dataCrc32);
                    header.header      = IPAddress.NetworkToHostOrder(header.header);
                    header.dataSize    = IPAddress.NetworkToHostOrder(header.dataSize);
#endif
                    if (recvBufSz < (header.dataSize + headerSize))
                    {
                        return;
                    }

                    GamePacket packet = GamePacket.CreateFromPool();
                    packet.header = header;
                    if (packet.header.dataSize <= 0)
                    {
                        packet.header.dataSize = 0;
                        packet.data            = null;
                    }
                    else
                    {
                        packet.data = NetByteArrayPool.GetByteBufferNode(packet.header.dataSize);
                        var buf = packet.data.GetBuffer();
                        Buffer.BlockCopy(recvBuffer, headerSize, buf, 0, packet.header.dataSize);
                    }

                    //LinkedListNode<GamePacket> node = new LinkedListNode<GamePacket>(packet);
                    var node = packet.LinkedNode;
                    lock (this)
                    {
                        mPacketList.AddLast(node);
                    }
                }
            } finally
            {
                //   Marshal.FreeHGlobal(headerBuffer);
            }
        }
Esempio n. 2
0
        internal static unsafe void WriteStream(Stream stream, Vector3 v)
        {
            var byteBufferNode = NetByteArrayPool.GetByteBufferNode(Marshal.SizeOf(v));

            try {
                System.IntPtr srcPtr = (System.IntPtr)(&v);
                Marshal.Copy(srcPtr, byteBufferNode.Buffer, 0, byteBufferNode.DataSize);
                stream.Write(byteBufferNode.Buffer, 0, byteBufferNode.DataSize);
            } finally {
                byteBufferNode.Dispose();
            }
        }
Esempio n. 3
0
        internal unsafe static void WriteStream(Stream stream, PVSNodeHeader header)
        {
            var byteBufferNode = NetByteArrayPool.GetByteBufferNode(Marshal.SizeOf(header));

            try {
                System.IntPtr srcPtr = (System.IntPtr)(&header);
                Marshal.Copy(srcPtr, byteBufferNode.Buffer, 0, byteBufferNode.DataSize);
                stream.Write(byteBufferNode.Buffer, 0, byteBufferNode.DataSize);
            } finally {
                byteBufferNode.Dispose();
            }
        }
Esempio n. 4
0
        internal unsafe static void ReadFromStream(Stream stream, out PVSNode ret)
        {
            ByteBufferNode node = NetByteArrayPool.GetByteBufferNode(Marshal.SizeOf(typeof(PVSNode)));

            try {
                stream.Read(node.Buffer, 0, node.DataSize);
                PVSNode       p;
                System.IntPtr dstPtr = (System.IntPtr)(&p);
                Marshal.Copy(node.Buffer, 0, dstPtr, node.DataSize);
                ret = p;
            } finally {
                node.Dispose();
            }
        }
Esempio n. 5
0
        public bool Send(string ip, int port, byte[] buf, int packetHandle, int bufSize = -1)
        {
            if (string.IsNullOrEmpty(ip) || port <= 0)
            {
                return(false);
            }

            if (bufSize < 0 && buf != null)
            {
                bufSize = buf.Length;
            }

            bool hasBufData = (buf != null) && (bufSize > 0);

            GamePackHeader header = new GamePackHeader();

            if (hasBufData)
            {
                header.dataSize = bufSize;
            }
            else
            {
                header.dataSize = 0;
            }

            header.header = packetHandle;

            if (hasBufData)
            {
                mCrc.Crc(buf, bufSize);
                header.dataCrc32 = (uint)mCrc.Value;
            }
            else
            {
                header.dataCrc32 = 0;
            }

            int headerSize = Marshal.SizeOf(header);
            int dstSize    = headerSize;

            if (hasBufData)
            {
                dstSize += bufSize;
            }

            // 下面注释是未优化代码
            //byte[] dstBuffer = new byte[dstSize];
            // 此处已优化
            var dstStream = NetByteArrayPool.GetByteBufferNode(dstSize);

            try
            {
                byte[] dstBuffer = dstStream.GetBuffer();

                // 此处可优化,可以考虑后续使用RINGBUF优化,RINGBUF用完可以自动关闭掉连接
                IntPtr pStruct = Marshal.AllocHGlobal(headerSize);
                try
                {
                    Marshal.StructureToPtr(header, pStruct, false);
                    Marshal.Copy(pStruct, dstBuffer, 0, headerSize);

#if USE_NETORDER
                    // Calc header Crc
                    CalcHeaderCrc(ref header, dstBuffer);

                    // used net
                    header.headerCrc32 = (uint)IPAddress.HostToNetworkOrder(header.headerCrc32);
                    header.dataCrc32   = (uint)IPAddress.HostToNetworkOrder(header.dataCrc32);
                    header.header      = IPAddress.HostToNetworkOrder(header.header);
                    header.dataSize    = IPAddress.HostToNetworkOrder(header.dataSize);

                    Marshal.StructureToPtr(header, pStruct, false);
                    Marshal.Copy(pStruct, dstBuffer, 0, headerSize);
#endif
                }
                finally
                {
                    Marshal.FreeHGlobal(pStruct);
                }

#if USE_NETORDER
#else
                // Calc header Crc
                //  CalcHeaderCrc(ref header, dstBuffer);
#endif
                if (hasBufData)
                {
                    Buffer.BlockCopy(buf, 0, dstBuffer, headerSize, bufSize);
                }
                return(SendBuf(ip, port, dstBuffer, dstSize));
            }
            finally
            {
                if (dstStream != null)
                {
                    dstStream.Dispose();
                    dstStream = null;
                }
            }
        }