Ejemplo n.º 1
0
 public void Flush(AsyncDatagramSession session)
 {
     if (ScheduleFlush(session))
     {
         Flush();
     }
 }
Ejemplo n.º 2
0
            private void EndSend(AsyncDatagramSession session, Exception ex)
            {
                IWriteRequest req = session.CurrentWriteRequest;

                if (req != null)
                {
                    req.Future.Exception = ex;
                }
                session.FilterChain.FireExceptionCaught(ex);
                BeginSend(session);
            }
Ejemplo n.º 3
0
 private Boolean ScheduleFlush(AsyncDatagramSession session)
 {
     if (session.ScheduledForFlush())
     {
         _flushingSessions.Enqueue(session);
         return(true);
     }
     else
     {
         return(false);
     }
 }
Ejemplo n.º 4
0
            private void BeginSend(AsyncDatagramSession session)
            {
                IWriteRequest req;

                while (true)
                {
                    if (session == null && !_flushingSessions.TryDequeue(out session))
                    {
                        Interlocked.Exchange(ref _writing, 0);
                        return;
                    }

                    req = session.CurrentWriteRequest;
                    if (req == null)
                    {
                        req = session.WriteRequestQueue.Poll(session);

                        if (req == null)
                        {
                            session.UnscheduledForFlush();
                            session = null;
                            continue;
                        }

                        session.CurrentWriteRequest = req;
                    }

                    break;
                }

                IoBuffer buf = req.Message as IoBuffer;

                if (buf == null)
                {
                    EndSend(session, new InvalidOperationException("Don't know how to handle message of type '"
                                                                   + req.Message.GetType().Name + "'.  Are you missing a protocol encoder?"));
                }

                if (buf.HasRemaining)
                {
                    EndPoint destination = req.Destination;
                    if (destination == null)
                    {
                        destination = session.RemoteEndPoint;
                    }
                    BeginSend(session, buf, destination);
                }
                else
                {
                    EndSend(session, 0);
                }
            }
Ejemplo n.º 5
0
            private void BeginSend(AsyncDatagramSession session, IoBuffer buf, EndPoint remoteEP)
            {
                _writeBuffer.Clear();

                SocketAsyncEventArgs       saea;
                SocketAsyncEventArgsBuffer saeaBuf = buf as SocketAsyncEventArgsBuffer;

                if (saeaBuf == null)
                {
                    if (_writeBuffer.Remaining < buf.Remaining)
                    {
                        // TODO allocate a temp buffer
                    }
                    else
                    {
                        _writeBuffer.Put(buf);
                    }
                    _writeBuffer.Flip();
                    saea = _writeBuffer.SocketAsyncEventArgs;
                    saea.SetBuffer(saea.Offset + _writeBuffer.Position, _writeBuffer.Limit);
                }
                else
                {
                    saea            = saeaBuf.SocketAsyncEventArgs;
                    saea.Completed += _completeHandler;
                }

                saea.UserToken      = session;
                saea.RemoteEndPoint = remoteEP;

                Boolean willRaiseEvent;

                try
                {
                    willRaiseEvent = Socket.SendToAsync(saea);
                }
                catch (ObjectDisposedException)
                {
                    // do nothing
                    return;
                }
                catch (Exception ex)
                {
                    EndSend(session, ex);
                    return;
                }
                if (!willRaiseEvent)
                {
                    ProcessSend(saea);
                }
            }
 private void BeginSend(AsyncDatagramSession session, IoBuffer buf, EndPoint remoteEP)
 {
     ArraySegment<Byte> array = buf.GetRemaining();
     try
     {
         Socket.BeginSendTo(array.Array, array.Offset, array.Count, SocketFlags.None, remoteEP, SendCallback, session);
     }
     catch (ObjectDisposedException)
     {
         // ignore
     }
     catch (Exception ex)
     {
         EndSend(session, ex);
     }
 }
Ejemplo n.º 7
0
            private void BeginSend(AsyncDatagramSession session, IoBuffer buf, EndPoint remoteEP)
            {
                ArraySegment <Byte> array = buf.GetRemaining();

                try
                {
                    Socket.BeginSendTo(array.Array, array.Offset, array.Count, SocketFlags.None, remoteEP, SendCallback, session);
                }
                catch (ObjectDisposedException)
                {
                    // ignore
                }
                catch (Exception ex)
                {
                    EndSend(session, ex);
                }
            }
Ejemplo n.º 8
0
            private void SendCallback(IAsyncResult ar)
            {
                AsyncDatagramSession session = (AsyncDatagramSession)ar.AsyncState;
                Int32 written;

                try
                {
                    written = Socket.EndSend(ar);
                }
                catch (ObjectDisposedException)
                {
                    // do nothing
                    return;
                }
                catch (Exception ex)
                {
                    EndSend(session, ex);
                    return;
                }

                EndSend(session, written);
            }
Ejemplo n.º 9
0
            private void EndSend(AsyncDatagramSession session, Int32 bytesTransferred)
            {
                session.IncreaseWrittenBytes(bytesTransferred, DateTime.Now);

                IWriteRequest req = session.CurrentWriteRequest;

                if (req != null)
                {
                    IoBuffer buf = req.Message as IoBuffer;
                    if (buf == null)
                    {
                        // we only send buffers and files so technically it shouldn't happen
                    }
                    else
                    {
                        // Buffer has been sent, clear the current request.
                        Int32 pos = buf.Position;
                        buf.Reset();

                        session.CurrentWriteRequest = null;
                        try
                        {
                            session.FilterChain.FireMessageSent(req);
                        }
                        catch (Exception ex)
                        {
                            session.FilterChain.FireExceptionCaught(ex);
                        }

                        // And set it back to its position
                        buf.Position = pos;
                    }
                }

                BeginSend(session);
            }
            private void BeginSend(AsyncDatagramSession session, IoBuffer buf, EndPoint remoteEP)
            {
                _writeBuffer.Clear();

                SocketAsyncEventArgs saea;
                SocketAsyncEventArgsBuffer saeaBuf = buf as SocketAsyncEventArgsBuffer;
                if (saeaBuf == null)
                {
                    if (_writeBuffer.Remaining < buf.Remaining)
                    {
                        // TODO allocate a temp buffer
                    }
                    else
                    {
                        _writeBuffer.Put(buf);
                    }
                    _writeBuffer.Flip();
                    saea = _writeBuffer.SocketAsyncEventArgs;
                    saea.SetBuffer(saea.Offset + _writeBuffer.Position, _writeBuffer.Limit);
                }
                else
                {
                    saea = saeaBuf.SocketAsyncEventArgs;
                    saea.Completed += _completeHandler;
                }

                saea.UserToken = session;
                saea.RemoteEndPoint = remoteEP;

                Boolean willRaiseEvent;
                try
                {
                    willRaiseEvent = Socket.SendToAsync(saea);
                }
                catch (ObjectDisposedException)
                { 
                    // do nothing
                    return;
                }
                catch (Exception ex)
                {
                    EndSend(session, ex);
                    return;
                }
                if (!willRaiseEvent)
                {
                    ProcessSend(saea);
                }
            }