Example #1
0
        /// <summary>
        /// Handles socket send completions.
        /// </summary>
        /// <param name="ar">The async result.</param>
        private void OnSend(IAsyncResult ar)
        {
            try
            {
                using (TimedLock.Lock(router.SyncRoot))
                {
                    if (sock == null)
                    {
                        return;
                    }

                    sendPos += sock.EndSend(ar);
                    SetLastAccess();

                    if (sendPos < cbSend)
                    {
                        // Continue sending the current message

                        sock.BeginSend(sendBuf, sendPos, cbSend - sendPos, SocketFlags.None, onSend, null);
                        return;
                    }

                    // Begin sending any queued messages

                    if (sendQueue.Count == 0)
                    {
                        sending = false;
                        sendBuf = null;
                        sendMsg = null;
                        return;
                    }

                    sendMsg = Dequeue();
                    sendBuf = sendMsg._MsgFrame;
                    sending = true;
                    sendPos = 0;
                    cbSend  = sendBuf.Length;

                    sock.BeginSend(sendBuf, sendPos, cbSend, SocketFlags.None, onSend, null);
                }
            }
            catch (Exception e)
            {
                TraceException(e);
                router.OnTcpClose(this);
                Close();
            }
        }
Example #2
0
 private void OnSendBuf(IAsyncResult ar)
 {
     try
     {
         syncCompletion = ar.CompletedSynchronously;
         cbTransfer     = sock.EndSend(ar);
     }
     catch (Exception e)
     {
         asyncException = e;
     }
     finally
     {
         asyncEvent.Set();
     }
 }
Example #3
0
 /// <summary>
 /// Completes the asynchronous transmission of bytes to the remote side of the connection.
 /// </summary>
 /// <param name="ar">The <see cref="IAsyncResult" /> instance returned by the call to <see cref="BeginSend" /> that initiated the transmission.</param>
 /// <exception cref="SocketException">Thrown if the transmission failed.</exception>
 /// <exception cref="SocketClosedException">Thrown if the socket has been closed.</exception>
 /// <remarks>
 /// <note>
 /// All successful calls to <see cref="BeginSend" /> must eventually be followed by a call to <see cref="EndSend" />.
 /// </note>
 /// </remarks>
 public void EndSend(IAsyncResult ar)
 {
     lock (syncLock)
     {
         if (isTcp)
         {
             try
             {
                 sock.EndSend(ar);
             }
             finally
             {
                 tcpSendPending = false;
             }
         }
         else
         {
             sock.EndSendTo(ar);
         }
     }
 }