Ejemplo n.º 1
0
        public bool DoSendResult(byte[] buffer, int offset, int count)
        {
            string commandText = outgoingDataAssembler.GetProtocolText();

            byte[] bufferUTF8  = Encoding.UTF8.GetBytes(commandText);
            int    totalLength = sizeof(int) + bufferUTF8.Length + count; //获取总大小
            AsyncSendBufferManager asyncSendBufferManager = asyncSocketUserToken.SendBuffer;

            asyncSendBufferManager.StartPacket();
            asyncSendBufferManager.DynamicBufferManager.WriteInt(totalLength, false);       //写入总大小
            asyncSendBufferManager.DynamicBufferManager.WriteInt(bufferUTF8.Length, false); //写入命令大小
            asyncSendBufferManager.DynamicBufferManager.WriteBuffer(bufferUTF8);            //写入命令内容
            asyncSendBufferManager.DynamicBufferManager.WriteBuffer(buffer, offset, count); //写入二进制数据
            asyncSendBufferManager.EndPacket();

            bool result = true;

            if (!sendAsync)
            {
                int packetOffset = 0;
                int packetCount  = 0;
                if (asyncSendBufferManager.GetFirstPacket(ref packetOffset, ref packetCount))
                {
                    sendAsync = true;
                    result    = asyncSocketServer.SendAsyncEvent(asyncSocketUserToken.Socket, asyncSocketUserToken.SendEventArgs,
                                                                 asyncSendBufferManager.DynamicBufferManager.Buffer, packetOffset, packetCount);
                }
            }
            return(result);
        }
Ejemplo n.º 2
0
        public AsyncSocketUserToken(int asyncReceviceBufferSize)
        {
            asyncReceviceBuffer          = new byte[asyncReceviceBufferSize];
            RecevicveEventArgs           = new SocketAsyncEventArgs();
            RecevicveEventArgs.UserToken = this;
            RecevicveEventArgs.SetBuffer(asyncReceviceBuffer, 0, asyncReceviceBuffer.Length);
            SendEventArgs           = new SocketAsyncEventArgs();
            SendEventArgs.UserToken = this;

            ReceiveBuffer = new DynamicBufferManager(ProtocolConst.InitBufferSize);
            SendBuffer    = new AsyncSendBufferManager(ProtocolConst.InitBufferSize);
        }
Ejemplo n.º 3
0
        public virtual bool SendCompleted()
        {
            activeDT  = DateTime.UtcNow;
            sendAsync = false;
            AsyncSendBufferManager asyncSendBufferManager = asyncSocketUserToken.SendBuffer;

            asyncSendBufferManager.ClearFirstPacket(); //清除已发送的包
            int offset = 0;
            int count  = 0;

            if (asyncSendBufferManager.GetFirstPacket(ref offset, ref count))
            {
                sendAsync = true;
                return(asyncSocketServer.SendAsyncEvent(asyncSocketUserToken.Socket, asyncSocketUserToken.SendEventArgs,
                                                        asyncSendBufferManager.DynamicBufferManager.Buffer, offset, count));
            }
            else
            {
                return(SendCallback());
            }
        }
Ejemplo n.º 4
0
        public bool DoSendBuffer(byte[] buffer, int offset, int count) //不是按包格式下发一个内存块,用于日志这类下发协议
        {
            AsyncSendBufferManager asyncSendBufferManager = asyncSocketUserToken.SendBuffer;

            asyncSendBufferManager.StartPacket();
            asyncSendBufferManager.DynamicBufferManager.WriteBuffer(buffer, offset, count);
            asyncSendBufferManager.EndPacket();

            bool result = true;

            if (!sendAsync)
            {
                int packetOffset = 0;
                int packetCount  = 0;
                if (asyncSendBufferManager.GetFirstPacket(ref packetOffset, ref packetCount))
                {
                    sendAsync = true;
                    result    = asyncSocketServer.SendAsyncEvent(asyncSocketUserToken.Socket, asyncSocketUserToken.SendEventArgs,
                                                                 asyncSendBufferManager.DynamicBufferManager.Buffer, packetOffset, packetCount);
                }
            }
            return(result);
        }