Beispiel #1
0
        private static SaeaAwaitable OperateAsync(Socket socket, SaeaAwaitable awaitable, Func <Socket, SaeaAwaitable, bool> operation)
        {
            if (socket == null)
            {
                throw new ArgumentNullException("socket");
            }

            if (awaitable == null)
            {
                throw new ArgumentNullException("awaitable");
            }

            var awaiter = awaitable.GetAwaiter();

            lock (awaiter.SyncRoot)
            {
                if (!awaiter.IsCompleted)
                {
                    throw new InvalidOperationException(
                              "A socket operation is already in progress using the same awaitable SAEA.");
                }

                awaiter.Reset();
                if (awaitable.ShouldCaptureContext)
                {
                    awaiter.SyncContext = SynchronizationContext.Current;
                }
            }

            try
            {
                if (!operation.Invoke(socket, awaitable))
                {
                    awaiter.Complete();
                }
            }
            catch (SocketException ex)
            {
                awaiter.Complete();
                awaitable.Saea.SocketError =
                    ex.SocketErrorCode != SocketError.Success ? ex.SocketErrorCode : SocketError.SocketError;
            }
            catch (Exception)
            {
                awaiter.Complete();
                awaitable.Saea.SocketError = SocketError.Success;
                throw;
            }

            return(awaitable);
        }
 private void Initialize()
 {
     _saeaPool = new SaeaPool(
         () =>
     {
         var saea = new SaeaAwaitable();
         return(saea);
     },
         (saea) =>
     {
         try
         {
             saea.Saea.AcceptSocket = null;
             saea.Saea.SetBuffer(EmptyArray, 0, 0);
             saea.Saea.RemoteEndPoint = null;
             saea.Saea.SocketFlags    = SocketFlags.None;
         }
         catch (Exception ex)     // initialize SAEA error occurred
         {
             _log.Error(ex.Message, ex);
         }
     })
                 .Initialize(256);
 }
Beispiel #3
0
 public static SaeaAwaitable SendAsync(this Socket socket, SaeaAwaitable awaitable)
 {
     return(OperateAsync(socket, awaitable, SEND));
 }
Beispiel #4
0
 public static SaeaAwaitable ReceiveAsync(this Socket socket, SaeaAwaitable awaitable)
 {
     return(OperateAsync(socket, awaitable, RECEIVE));
 }
Beispiel #5
0
 public static SaeaAwaitable DisonnectAsync(this Socket socket, SaeaAwaitable awaitable)
 {
     return(OperateAsync(socket, awaitable, DISCONNECT));
 }
Beispiel #6
0
 public static SaeaAwaitable AcceptAsync(this Socket socket, SaeaAwaitable awaitable)
 {
     return(OperateAsync(socket, awaitable, ACCEPT));
 }
Beispiel #7
0
 public SaeaAwaiter(SaeaAwaitable awaitable)
 {
     _awaitable = awaitable;
     _awaitable.Saea.Completed += OnSaeaCompleted;
 }
 private void Initialize()
 {
     _acceptSaeaPool = new SaeaPool(
         () =>
     {
         var saea = new SaeaAwaitable();
         return(saea);
     },
         (saea) =>
     {
         try
         {
             saea.Saea.AcceptSocket = null;
             saea.Saea.SetBuffer(0, 0);
             saea.Saea.RemoteEndPoint = null;
             saea.Saea.SocketFlags    = SocketFlags.None;
         }
         catch (Exception ex)
         {
             _log.Error(ex.Message, ex);
         }
     })
                       .Initialize(16);
     _handleSaeaPool = new SaeaPool(
         () =>
     {
         var saea = new SaeaAwaitable();
         return(saea);
     },
         (saea) =>
     {
         try
         {
             saea.Saea.AcceptSocket = null;
             saea.Saea.SetBuffer(EmptyArray, 0, 0);
             saea.Saea.RemoteEndPoint = null;
             saea.Saea.SocketFlags    = SocketFlags.None;
         }
         catch (Exception ex)
         {
             _log.Error(ex.Message, ex);
         }
     })
                       .Initialize(1024);
     _sessionPool = new SessionPool(
         () =>
     {
         var session = new TcpSocketSaeaSession(_configuration, _configuration.BufferManager, _handleSaeaPool, _dispatcher, this);
         return(session);
     },
         (session) =>
     {
         try
         {
             session.Detach();
         }
         catch (Exception ex)
         {
             _log.Error(ex.Message, ex);
         }
     })
                    .Initialize(512);
 }