Example #1
0
 /// <summary>
 /// 接受线程函数(异步)
 /// </summary>
 protected override void ReceiveCallback(IAsyncResult result)
 {
     try
     {
         Socket ts  = (Socket)result.AsyncState;
         int    len = ts.EndReceive(result);
         if (len > 0)
         {
             result.AsyncWaitHandle.Close();
             RBuffer.Decode(buffer);
             TriggerSocketInvoke();
         }
         else if (len == 0)
         {
             // 如果等于0说明断开连接
             return;
         }
         //清空数据,重新开始异步接收
         Array.Clear(buffer, 0, buffer.Length);
         ts.BeginReceive(buffer, 0, buffer.Length, System.Net.Sockets.SocketFlags.None, new AsyncCallback(ReceiveCallback), ts);
     }
     catch (Exception ex)
     {
         // Log.Exception(ex, "ClientConnection", "ReceiveCallback", "Socket");
         socketInvoke.SocketException(ex);
     }
 }
Example #2
0
        /// <summary>
        /// 触发回调委托
        /// </summary>
        protected override void TriggerSocketInvoke()
        {
            var sb = RBuffer.TakeStreamBuffer();

            while (sb != null)
            {
                if (socketInvoke != null)
                {
                    socketInvoke.OnReceivedCompleted(new SocketMsg(0, sb, this));
                }
                // 提取下一个
                sb = RBuffer.TakeStreamBuffer();
            }
        }
Example #3
0
 /// <summary>
 /// 触发回调委托
 /// </summary>
 internal void TriggerSocketInvoke()
 {
     byte[] sb = null;
     do
     {
         if (RBuffer != null)
         {
             sb = RBuffer.TakeStreamBuffer();
         }
         if (sb == null)
         {
             return;
         }
         if (SocketInvoke != null)
         {
             SocketInvoke.OnReceivedCompleted(new RemoteSocketMsg(0, sb, this));
         }
     } while (true);
 }
Example #4
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);
            }
        }