예제 #1
0
        public void Send(OutgoingPacket packet)
        {
            var headerLength = packet.HeaderLength;
            var length       = packet.Length;
            var totalLength  = headerLength + length;

            // Insert the header.
            var bytes = new byte[totalLength];

            _propagator.WriteHeader(packet, bytes);

            // Insert the payload.
            packet.Position = 0;
            packet.BaseStream.Read(bytes, headerLength, length);

            // Encrypt if some sort of encryption scheme has been set.
            if (Crypt != null)
            {
                Crypt.Encrypt(bytes, 0, headerLength);
            }

            var args = SocketAsyncEventArgsPool.Acquire();

            args.SetBuffer(bytes, 0, totalLength);
            args.Completed += OnSend;

            try
            {
                if (!_socket.SendAsync(args))
                {
                    OnSend(this, args);
                }
            }
            catch (Exception ex)
            {
                args.Completed -= OnSend;
                SocketAsyncEventArgsPool.Release(args);
                Disconnect();

                if (ex is ObjectDisposedException)
                {
                    return;
                }

                if (ex is SocketException)
                {
                    ExceptionManager.RegisterException(ex);
                    return;
                }

                throw;
            }
        }
예제 #2
0
        public void Disconnect()
        {
            try
            {
                _socket.Shutdown(SocketShutdown.Both);
                _socket.Disconnect(false);
                _socket.Close();
            }
            catch (Exception)
            {
                // Suck it up. If an exception occurs, it's already been disposed.
            }

            SocketAsyncEventArgsPool.Release(_eventArgs);
            Server.RemoveClient(this);
        }
예제 #3
0
        /// <summary>
        /// Starts accepting incoming clients.
        /// </summary>
        /// <param name="args">The async event args to use, if any (passing null
        /// will create a new object).</param>
        private void Accept(SocketAsyncEventArgs args)
        {
            if (args == null)
            {
                args            = SocketAsyncEventArgsPool.Acquire();
                args.Completed += OnAccept;
            }
            else
            {
                args.AcceptSocket = null;
            }

            try
            {
                if (!_socket.AcceptAsync(args))
                {
                    OnAccept(this, args);
                }
            }
            catch (Exception ex)
            {
                args.Completed -= OnAccept;
                SocketAsyncEventArgsPool.Release(args);

                if (ex is ObjectDisposedException)
                {
                    return;
                }

                if (ex is SocketException)
                {
                    ExceptionManager.RegisterException(ex);
                    return;
                }

                throw;
            }
        }
예제 #4
0
 private void OnSend(object sender, SocketAsyncEventArgs args)
 {
     args.Completed -= OnSend;
     SocketAsyncEventArgsPool.Release(args);
 }