Example #1
0
 public bool Send(BufferSegment segBuff, bool waiting = true)
 {
     try
     {
         bool isWillEvent = true;
         ArraySegment <byte>[] segItems = _sendBuff.BuffToSegs(segBuff.buffer, segBuff.offset, segBuff.count);
         foreach (var seg in segItems)
         {
             SocketAsyncEventArgs sendArg = _sendArgs.GetFreeArg((retry) => { return(true); }, waiting);
             if (sendArg == null)
             {
                 throw new Exception("发送缓冲池已用完,等待回收...");
             }
             sendArg.RemoteEndPoint = _endPoint;
             if (!_sendBuff.WriteBuffer(sendArg, seg.Array, seg.Offset, seg.Count))
             {
                 _sendArgs.Set(sendArg);
                 throw new Exception(string.Format("发送缓冲区溢出...buffer block max size:{0}", _sendBuff.bufferSize));
             }
             isWillEvent &= _sock.SendToAsync(sendArg);
             if (!isWillEvent)
             {
                 SendCallBack(sendArg);
             }
         }
         return(isWillEvent);
     }
     catch (Exception ex)
     {
         Close();
         throw ex;
     }
 }
 public bool SendAsync(BufferSegment segBuff, bool wait = true)
 {
     try
     {
         if (connected == false || sock == null || sock.Connected == false)
         {
             Close();
             return(false);
         }
         ArraySegment <byte>[] segs = _sendBuffMgr.BuffToSegs(segBuff.buffer, segBuff.offset, segBuff.count);
         bool isWillEvent           = true;
         foreach (var seg in segs)
         {
             SocketAsyncEventArgs senArg = GetFreeSendArg(wait);
             if (senArg == null)
             {
                 return(false);
             }
             if (!_sendBuffMgr.WriteBuffer(senArg, seg.Array, seg.Offset, seg.Count))
             {
                 _sendArgs.Set(senArg);
                 throw new Exception(string.Format("发送缓冲区溢出...buffer block max size:{0}", _sendBuffMgr.bufferSize));
             }
             if (senArg.UserToken == null)
             {
                 ((SocketToken)senArg.UserToken).sock = sock;
             }
             if (connected == false || sock == null || sock.Connected == false)
             {
                 Close();
                 return(false);
             }
             isWillEvent &= sock.SendAsync(senArg);
             if (!isWillEvent)//can't trigger the io complated event to do
             {
                 SendCallback(senArg);
             }
             if (_sendArgs.count < (_sendArgs.capcity >> 2))
             {
                 Thread.Sleep(2);
             }
         }
         return(isWillEvent);
     }
     catch (Exception)
     {
         Close();
         throw;
     }
 }
Example #3
0
 public bool SendAsync(SocketToken token, BufferSegment seg, bool wait = false)
 {
     try
     {
         if (!token.sock.Connected)
         {
             return(false);
         }
         bool isWillEvent           = true;
         ArraySegment <byte>[] segs = _sendBuffMgr.BuffToSegs(seg.buffer, seg.offset, seg.count);
         for (int i = 0; i < segs.Length; i++)
         {
             SocketAsyncEventArgs senArg = GetFreeSendArg(wait, token.sock);
             if (senArg == null)
             {
                 return(false);
             }
             senArg.UserToken = token;
             if (!_sendBuffMgr.WriteBuffer(senArg, segs[i].Array, segs[i].Offset, segs[i].Count))
             {
                 _sendArgs.Set(senArg);
                 throw new Exception(string.Format("发送缓冲区溢出...buffer block max size:{0}", _sendBuffMgr.bufferSize));
             }
             if (!token.sock.Connected)
             {
                 return(false);
             }
             isWillEvent &= token.sock.SendAsync(senArg);
             if (!isWillEvent)
             {
                 SendCallBack(senArg);
             }
             if (_sendArgs.count < (_sendArgs.capcity >> 2))
             {
                 Thread.Sleep(5);
             }
         }
         return(isWillEvent);
     }
     catch (Exception ex)
     {
         Close(token);
         throw ex;
     }
 }
Example #4
0
 public bool Send(BufferSegment segBuff, IPEndPoint remoteEP, bool waiting)
 {
     try
     {
         bool isWillEvent = true;
         ArraySegment <byte>[] segItems = sendBuff.BuffToSegs(segBuff.buffer, segBuff.offset, segBuff.count);
         foreach (var seg in segItems)
         {
             var SendArg = sendArgs.GetFreeArg((retry) => { return(true); }, waiting);
             if (SendArg == null)
             {
                 throw new Exception("发送缓冲池已用完,等待回收超时...");
             }
             SendArg.RemoteEndPoint = remoteEP;
             //Socket s = SocketVersion(remoteEP);
             //SendArg.UserToken = s;
             if (!sendBuff.WriteBuffer(SendArg, seg.Array, seg.Offset, seg.Count))
             {
                 sendArgs.Set(SendArg);
                 throw new Exception(string.Format("发送缓冲区溢出...buffer block max size:{0}", sendBuff.bufferSize));
             }
             if (SendArg.RemoteEndPoint != null)
             {
                 isWillEvent &= _sock.SendToAsync(SendArg);
                 if (!isWillEvent)
                 {
                     SendCallBack(SendArg);
                 }
             }
             Thread.Sleep(5);
         }
         return(isWillEvent);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }