Beispiel #1
0
        /// <summary>
        /// 发送数据
        /// <para>返回 0 为成功 -1 异常</para>
        /// </summary>
        /// <param name="buffer">数据</param>
        /// <param name="offset">偏移</param>
        /// <param name="count">数量</param>
        /// <returns>0 为成功 -1 异常</returns>
        protected bool SendBuffer(/*ushort sessionId,*/ byte[] buffer, int offset, int count)
        {
            // 数据打包
            byte[] data = null;
            if (offset == 0 && buffer.Length == count)
            {
                data = RBuffer.Encode(buffer);
            }
            else if (offset > 0)
            {
                byte[] buffer2 = new byte[count];
                Array.Copy(buffer, offset, buffer2, 0, count);
                data = RBuffer.Encode(buffer);
            }

            try
            {
                var args = sendEventArgs.Pop();
                args.SetBuffer(data, 0, data.Length);
                var willRaiseEvent = clientSocket.SendAsync(args);
                if (!willRaiseEvent)
                {
                    return(ProcessSend(args));
                }
                else
                {
                    return(true);
                }
            }
            catch (Exception ex)
            {
                // Log.Exception(ex, "BaseClientConnection", "SendBuffer", "Socket");
                socketInvoke.SocketException(ex);
                return(false);
            }
        }