Beispiel #1
0
        private void TryDequeueAndPostSend(ExSocket socket, SocketAsyncEventArgs ioEventArgs)
        {
            bool isOwner = ioEventArgs == null;

            byte[] data;
            if (socket.TryDequeue(out data))
            {
                if (ioEventArgs == null)
                {
                    ioEventArgs = ioEventArgsPool.Pop();
                    ioEventArgs.AcceptSocket = socket.WorkSocket;
                }
                DataToken dataToken = (DataToken)ioEventArgs.UserToken;
                dataToken.Socket = socket;
                dataToken.byteArrayForMessage = data;
                dataToken.messageLength       = data.Length;
                try
                {
                    PostSend(ioEventArgs);
                }
                catch
                {
                    if (isOwner)
                    {
                        ReleaseIOEventArgs(ioEventArgs);
                    }
                    throw;
                }
            }
            else
            {
                ReleaseIOEventArgs(ioEventArgs);
                socket.ResetSendFlag();
            }
        }
Beispiel #2
0
 private void AddClient(ExSocket socket)
 {
     lock (clientSockets)
     {
         clientSockets.Add(socket);
     }
 }
Beispiel #3
0
 public void CloseSocket(ExSocket socket)
 {
     try
     {
         socket.WorkSocket.Shutdown(SocketShutdown.Both);
     }
     catch { }
     socket.WorkSocket.Close();
 }
Beispiel #4
0
 public void PostSend(ExSocket socket, byte[] data, int offset, int count)
 {
     byte[] buffer = new byte[count + 4];
     Buffer.BlockCopy(BitConverter.GetBytes(count), 0, buffer, 0, 4);
     Buffer.BlockCopy(data, offset, buffer, 4, count);
     socket.Enqueue(buffer);
     if (socket.TrySetSendFlag())
     {
         try
         {
             TryDequeueAndPostSend(socket, null);
         }
         catch
         {
             socket.ResetSendFlag();
             throw;
         }
     }
 }
Beispiel #5
0
        private void ProcessAccept(SocketAsyncEventArgs acceptEventArgs)
        {
            PostAccept();

            if (acceptEventArgs.SocketError != SocketError.Success)
            {
                HandleBadAccept(acceptEventArgs);
                return;
            }

            SocketAsyncEventArgs ioEventArgs = this.ioEventArgsPool.Pop();

            ioEventArgs.AcceptSocket     = acceptEventArgs.AcceptSocket;
            acceptEventArgs.AcceptSocket = null;
            this.acceptEventArgsPool.Push(acceptEventArgs);
            var dataToken = (DataToken)ioEventArgs.UserToken;

            ioEventArgs.SetBuffer(dataToken.bufferOffset, socketSettings.BufferSize);
            var exSocket = new ExSocket(ioEventArgs.AcceptSocket);

            exSocket.LastAccessTime = DateTime.Now;
            dataToken.Socket        = exSocket;

            AddClient(exSocket);

            try
            {
                OnConnected(new ConnectionEventArgs {
                    Socket = exSocket
                });
            }
            catch (Exception ex)
            {
                logger.Error("OnConnected", ex);
            }

            PostReceive(ioEventArgs);
        }
Beispiel #6
0
 private void TryDequeueAndPostSend(ExSocket socket, SocketAsyncEventArgs ioEventArgs)
 {
     bool isOwner = ioEventArgs == null;
     byte[] data;
     if (socket.TryDequeue(out data))
     {
         if (ioEventArgs == null)
         {
             ioEventArgs = ioEventArgsPool.Pop();
             ioEventArgs.AcceptSocket = socket.WorkSocket;
         }
         DataToken dataToken = (DataToken)ioEventArgs.UserToken;
         dataToken.Socket = socket;
         dataToken.byteArrayForMessage = data;
         dataToken.messageLength = data.Length;
         try
         {
             PostSend(ioEventArgs);
         }
         catch
         {
             if (isOwner)
                 ReleaseIOEventArgs(ioEventArgs);
             throw;
         }
     }
     else
     {
         ReleaseIOEventArgs(ioEventArgs);
         socket.ResetSendFlag();
     }
 }
Beispiel #7
0
        private void ProcessAccept(SocketAsyncEventArgs acceptEventArgs)
        {
            PostAccept();

            if (acceptEventArgs.SocketError != SocketError.Success)
            {
                HandleBadAccept(acceptEventArgs);
                return;
            }

            SocketAsyncEventArgs ioEventArgs = this.ioEventArgsPool.Pop();
            ioEventArgs.AcceptSocket = acceptEventArgs.AcceptSocket;
            acceptEventArgs.AcceptSocket = null;
            this.acceptEventArgsPool.Push(acceptEventArgs);
            var dataToken = (DataToken)ioEventArgs.UserToken;
            ioEventArgs.SetBuffer(dataToken.bufferOffset, socketSettings.BufferSize);
            var exSocket = new ExSocket(ioEventArgs.AcceptSocket);
            exSocket.LastAccessTime = DateTime.Now;
            dataToken.Socket = exSocket;

            AddClient(exSocket);

            try
            {
                OnConnected(new ConnectionEventArgs { Socket = exSocket });
            }
            catch (Exception ex)
            {
                logger.Error("OnConnected", ex);
            }

            PostReceive(ioEventArgs);
        }
Beispiel #8
0
 private void AddClient(ExSocket socket)
 {
     lock (clientSockets)
     {
         clientSockets.Add(socket);
     }
 }
Beispiel #9
0
 public void PostSend(ExSocket socket, byte[] data, int offset, int count)
 {
     byte[] buffer = new byte[count + 4];
     Buffer.BlockCopy(BitConverter.GetBytes(count), 0, buffer, 0, 4);
     Buffer.BlockCopy(data, offset, buffer, 4, count);
     socket.Enqueue(buffer);
     if (socket.TrySetSendFlag())
     {
         try
         {
             TryDequeueAndPostSend(socket, null);
         }
         catch
         {
             socket.ResetSendFlag();
             throw;
         }
     }
 }
Beispiel #10
0
 public void CloseSocket(ExSocket socket)
 {
     try
     {
         socket.WorkSocket.Shutdown(SocketShutdown.Both);
     }
     catch { }
     socket.WorkSocket.Close();
 }