`
Donald_Draper
  • 浏览: 999266 次
社区版块
存档分类
最新评论

Mina Nio会话(Socket,DataGram)

    博客分类:
  • Mina
阅读更多
Mina Socket会话配置:http://donald-draper.iteye.com/blog/2375529
Mina 抽象Io会话:http://donald-draper.iteye.com/blog/2377880
引言:
    上一篇我们看了一下Io会话的抽象实现,先来回顾一下:
    抽象会话AbstractIoSession内部有一个关联的IoService和一个IoHandler;一个写请求队列用于存发会话写请求;一个会话属性Map存放会话属性,还有一些读写字节数,消息数,相关吞吐量和上次读写或空闲操作时间计数器。,会话初始化主要为初始化关联service,及关联的IoHandler,实际为Service的IoHandler;初始化所有的会话事件计数器为当前时间。关闭方法默认关闭时,清空写请求队列,并将写请求的结果置为已写,触发过滤过滤链fireFilterClose事件,即不flush会话写请求队列,closeOnFlush方法为,在关闭会话前,flush会话写请求队列。会话读操作,首先获取会话读请求结果队列,从队列poll一个读结果,如果读结果不为空且已关闭,则重新入队列,否则新建一个默认读请求结果,添加到会话等待读请求结果队列。会话写请求,首先保证消息不为null,会话建立连接,并且远端socket地址不为null;如果消息为IoBuffer,确保buffer不为空,如果消息为文件通道/文件类型,则包装消息为DefaultFileRegion/FilenameFileRegion;然后创建写请求DefaultWriteRequest,触发会话过滤链fireFilterWrite事件,如果消息为文件通道,则注册写结果监听器,在消息发送完后,关闭文件通道,返回写结果DefaultWriteFuture。
今天来看Io会话的具体实现传输层管理的会话NioSession
/**
 * An {@link IoSession} which is managed by the NIO transport.
 *  
 * @author [url=http://mina.apache.org]Apache MINA Project[/url]
 */
public abstract class NioSession extends AbstractIoSession {
    /** The NioSession processor */
    protected final IoProcessor<NioSession> processor;//会话关联Io处理器
    /** The communication channel */
    protected final Channel channel;//会话管理通道
    /** The SelectionKey used for this session */
    private SelectionKey key;//会话通道选择key
    /** The FilterChain created for this session */
    private final IoFilterChain filterChain;//会话过滤链
    /**
     * 
     * Creates a new instance of NioSession, with its associated IoProcessor.
     * <br>
     * This method is only called by the inherited class.
     *根据IO处理器,IoService,通道Channel构建nio会话
     * @param processor The associated {@link IoProcessor}
     * @param service The associated {@link IoService}
     * @param channel The associated {@link Channel}
     */
    protected NioSession(IoProcessor<NioSession> processor, IoService service, Channel channel) {
        super(service);
        this.channel = channel;
        this.processor = processor;
        filterChain = new DefaultIoFilterChain(this);
    }

    /**
     * @return The ByteChannel associated with this {@link IoSession} 
     获取会话通道
     */
    abstract ByteChannel getChannel();

    /**
     * {@inheritDoc}
     获取会话过滤链
     */
    @Override
    public IoFilterChain getFilterChain() {
        return filterChain;
    }

    /**
     * @return The {@link SelectionKey} associated with this {@link IoSession}
     获取会话选择key
     */
    /* No qualifier*/SelectionKey getSelectionKey() {
        return key;
    }

    /**
     * Sets the {@link SelectionKey} for this {@link IoSession}
     *设置会话选择key
     * @param key The new {@link SelectionKey}
     */
    /* No qualifier*/void setSelectionKey(SelectionKey key) {
        this.key = key;
    }

    /**
     * {@inheritDoc}
     获取会话关联Io处理器
     */
    @Override
    public IoProcessor<NioSession> getProcessor() {
        return processor;
    }

    /**
     * {@inheritDoc}
     会话是否处于激活状态
     */
    @Override
    public final boolean isActive() {
        return key.isValid();
    }
}

从上面可以看出,NioSession会话主要管理会话关联Io处理器processor,通道channel,
选择键SelectionKey和会话过滤链IoFilterChain;IoService交由AbstractIoSession管理。
下面来看socket会话NioSocketSession
/**
 * An {@link IoSession} for socket transport (TCP/IP).
 *
 * @author [url=http://mina.apache.org]Apache MINA Project[/url]
 */
class NioSocketSession extends NioSession {
   //Transport元数据
    static final TransportMetadata METADATA = new DefaultTransportMetadata("nio", "socket", false, true,
            InetSocketAddress.class, SocketSessionConfig.class, IoBuffer.class, FileRegion.class);
}
//默认Transport元数据
public class DefaultTransportMetadata
    implements TransportMetadata
{
    private final String providerName;//提供者名nio/aio/bio
    private final String name;//Transport元数据名
    private final boolean connectionless;//连接状态,(有连接的通信tcp,还是无连接udp)
    private final boolean fragmentation;//是否分片
    private final Class addressType;//地址类型
    private final Class sessionConfigType;//会话配置类型
    private final Set envelopeTypes;//涉及包装类型
    public transient DefaultTransportMetadata(String providerName, String name, boolean connectionless, 
                  boolean fragmentation, Class addressType, Class sessionConfigType, Class envelopeTypes[])
    {
    ...
    }
}

再来看socket会话的构造:
/**
 * 
 * Creates a new instance of NioSocketSession.
 *创建socket会话
 * @param service the associated IoService 
 * @param processor the associated IoProcessor
 * @param channel the used channel
 */
public NioSocketSession(IoService service, IoProcessor<NioSession> processor, SocketChannel channel) {
    super(processor, service, channel);
    config = new SessionConfigImpl();
    config.setAll(service.getSessionConfig());
}

从构造来看,socket会话配置继承于关联IoService的会话配置 ;
//获取会话socket
private Socket getSocket() {
    return ((SocketChannel) channel).socket();
}
/**
 * {@inheritDoc}
 获取传输元数据
 */
@Override
public TransportMetadata getTransportMetadata() {
    return METADATA;
}
/**
 * {@inheritDoc}
 获取socket会话配置
 */
@Override
public SocketSessionConfig getConfig() {
    return (SocketSessionConfig) config;
}
/**
 * {@inheritDoc}
 获取会话通道
 */
@Override
SocketChannel getChannel() {
    return (SocketChannel) channel;
}
/**
 * {@inheritDoc}
 获取会话远端地址
 */
@Override
public InetSocketAddress getRemoteAddress() {
    if (channel == null) {
        return null;
    }
    Socket socket = getSocket();
    if (socket == null) {
        return null;
    }
    return (InetSocketAddress) socket.getRemoteSocketAddress();
}
/**
 * {@inheritDoc}
 获取会话本地地址
 */
@Override
public InetSocketAddress getLocalAddress() {
    if (channel == null) {
        return null;
    }
    Socket socket = getSocket();
    if (socket == null) {
        return null;
    }
    return (InetSocketAddress) socket.getLocalSocketAddress();
}
//关闭会话,即取消会话关联的选择key和关闭关联通道。
protected void destroy(NioSession session) throws IOException {
    ByteChannel ch = session.getChannel();
    SelectionKey key = session.getSelectionKey();
    if (key != null) {
        key.cancel();
    }
    ch.close();
}
@Override
//获取会话关联Ioservice地址
public InetSocketAddress getServiceAddress() {
    return (InetSocketAddress) super.getServiceAddress();
}

回到构造函数中的会话配置:
public NioSocketSession(IoService service, IoProcessor<NioSession> processor, SocketChannel channel) {
    super(processor, service, channel);
    config = new SessionConfigImpl();
    config.setAll(service.getSessionConfig());
}

我们来看看一下SessionConfigImpl:
/**
     * A private class storing a copy of the IoService configuration when the IoSession
     * is created. That allows the session to have its own configuration setting, over
     * the IoService default one.
     SessionConfigImpl为socket会话私有类。当Io会话创建时,从关联IoService拷贝一份会话相关配置到
     当前会话。运行会话可以拥有自己的配置,覆盖从IoService继承的
     */
    private class SessionConfigImpl extends AbstractSocketSessionConfig {
       //下面这些配置,我们在前面已讲,这里不再重复,与socket配置基本相同
        /**
         * {@inheritDoc}
         */
        @Override
        public boolean isKeepAlive() {
            try {
                return getSocket().getKeepAlive();
            } catch (SocketException e) {
                throw new RuntimeIoException(e);
            }
        }
        /**
         * {@inheritDoc}
         */
        @Override
        public void setKeepAlive(boolean on) {
            try {
                getSocket().setKeepAlive(on);
            } catch (SocketException e) {
                throw new RuntimeIoException(e);
            }
        }
        /**
         * {@inheritDoc}
         */
        @Override
        public boolean isOobInline() {
            try {
                return getSocket().getOOBInline();
            } catch (SocketException e) {
                throw new RuntimeIoException(e);
            }
        }
        /**
         * {@inheritDoc}
         */
        @Override
        public void setOobInline(boolean on) {
            try {
                getSocket().setOOBInline(on);
            } catch (SocketException e) {
                throw new RuntimeIoException(e);
            }
        }
        /**
         * {@inheritDoc}
         */
        @Override
        public boolean isReuseAddress() {
            try {
                return getSocket().getReuseAddress();
            } catch (SocketException e) {
                throw new RuntimeIoException(e);
            }
        }
        /**
         * {@inheritDoc}
         */
        @Override
        public void setReuseAddress(boolean on) {
            try {
                getSocket().setReuseAddress(on);
            } catch (SocketException e) {
                throw new RuntimeIoException(e);
            }
        }
        /**
         * {@inheritDoc}
         */
        @Override
        public int getSoLinger() {
            try {
                return getSocket().getSoLinger();
            } catch (SocketException e) {
                throw new RuntimeIoException(e);
            }
        }
        /**
         * {@inheritDoc}
         */
        @Override
        public void setSoLinger(int linger) {
            try {
                if (linger < 0) {
                    getSocket().setSoLinger(false, 0);
                } else {
                    getSocket().setSoLinger(true, linger);
                }
            } catch (SocketException e) {
                throw new RuntimeIoException(e);
            }
        }
        /**
         * {@inheritDoc}
         */
        @Override
        public boolean isTcpNoDelay() {
            if (!isConnected()) {
                return false;
            }
            try {
                return getSocket().getTcpNoDelay();
            } catch (SocketException e) {
                throw new RuntimeIoException(e);
            }
        }
        /**
         * {@inheritDoc}
         */
        @Override
        public void setTcpNoDelay(boolean on) {
            try {
                getSocket().setTcpNoDelay(on);
            } catch (SocketException e) {
                throw new RuntimeIoException(e);
            }
        }
        /**
         * {@inheritDoc}
         */
        @Override
        public int getTrafficClass() {
            try {
                return getSocket().getTrafficClass();
            } catch (SocketException e) {
                throw new RuntimeIoException(e);
            }
        }
        /**
         * {@inheritDoc}
         */
        @Override
        public void setTrafficClass(int tc) {
            try {
                getSocket().setTrafficClass(tc);
            } catch (SocketException e) {
                throw new RuntimeIoException(e);
            }
        }
        /**
         * {@inheritDoc}
         */
        @Override
        public int getSendBufferSize() {
            try {
                return getSocket().getSendBufferSize();
            } catch (SocketException e) {
                throw new RuntimeIoException(e);
            }
        }
        /**
         * {@inheritDoc}
         */
        @Override
        public void setSendBufferSize(int size) {
            try {
                getSocket().setSendBufferSize(size);
            } catch (SocketException e) {
                throw new RuntimeIoException(e);
            }
        }
        /**
         * {@inheritDoc}
         */
        @Override
        public int getReceiveBufferSize() {
            try {
                return getSocket().getReceiveBufferSize();
            } catch (SocketException e) {
                throw new RuntimeIoException(e);
            }
        }
        /**
         * {@inheritDoc}
         */
        @Override
        public void setReceiveBufferSize(int size) {
            try {
                getSocket().setReceiveBufferSize(size);
            } catch (SocketException e) {
                throw new RuntimeIoException(e);
            }
        }
    }
    /**
     * {@inheritDoc}
     */
    @Override
    public final boolean isSecured() {
        // If the session does not have a SslFilter, we can return false
        IoFilterChain chain = getFilterChain();
	//根据会话过滤链中是否添加SslFilter过滤器,来判断会话是否安全
        IoFilter sslFilter = chain.get(SslFilter.class);
        if (sslFilter != null) {
        // Get the SslHandler from the SslFilter
            return ((SslFilter)sslFilter).isSslStarted(this);
        } else {
            return false;
        }
    }
}

我们再来看一下报文会话:
class NioDatagramSession extends NioSession {
   //传输元数据
    static final TransportMetadata METADATA = new DefaultTransportMetadata("nio", "datagram", true, false,
            InetSocketAddress.class, DatagramSessionConfig.class, IoBuffer.class);
    private final InetSocketAddress localAddress;//本地地址
    private final InetSocketAddress remoteAddress;//远端地址
     /**
     * Creates a new connector-side session instance.
     创建一个单向连接的报文会话实例
     */
    NioDatagramSession(IoService service, DatagramChannel channel, IoProcessor<NioSession> processor) {
        this(service, channel, processor, channel.socket().getRemoteSocketAddress());
    }
    /**
     * Creates a new acceptor-side session instance.
     创建一个单向连接的报文会话实例
     */
    NioDatagramSession(IoService service, DatagramChannel channel, IoProcessor<NioSession> processor,
            SocketAddress remoteAddress) {
        super(processor, service, channel);
        config = new NioDatagramSessionConfig(channel);
        config.setAll(service.getSessionConfig());
        this.remoteAddress = (InetSocketAddress) remoteAddress;
        this.localAddress = (InetSocketAddress) channel.socket().getLocalSocketAddress();
    }
    /**
     * {@inheritDoc}
     */
    @Override
    public DatagramSessionConfig getConfig() {
        return (DatagramSessionConfig) config;
    }
    /**
     * {@inheritDoc}
     */
    @Override
    DatagramChannel getChannel() {
        return (DatagramChannel) channel;
    }
    /**
     * {@inheritDoc}
     */
    @Override
    public TransportMetadata getTransportMetadata() {
        return METADATA;
    }
    /**
     * {@inheritDoc}
     */
    @Override
    public InetSocketAddress getRemoteAddress() {
        return remoteAddress;
    }
    /**
     * {@inheritDoc}
     */
    @Override
    public InetSocketAddress getLocalAddress() {
        return localAddress;
    }
    /**
     * {@inheritDoc}
     */
    @Override
    public InetSocketAddress getServiceAddress() {
        return (InetSocketAddress) super.getServiceAddress();
    }
}

总结:
   NioSession会话主要管理会话关联Io处理器processor,通道channel,选择键SelectionKey和会话过滤链IoFilterChain;IoService交由AbstractIoSession管理。
socket会话NioSocketSession配置继承于关联IoService的会话配置 ;关闭会话,即取消会话关联的选择key和关闭关联通道。

附:
public class DefaultTransportMetadata
    implements TransportMetadata
{
    private final String providerName;//提供者名nio/aio/bio
    private final String name;//Transport元数据名
    private final boolean connectionless;//连接状态,(有连接的通信tcp,还是无连接udp)
    private final boolean fragmentation;//是否分片
    private final Class addressType;//地址类型
    private final Class sessionConfigType;//会话配置类型
    private final Set envelopeTypes;//涉及包装类型
    public transient DefaultTransportMetadata(String providerName, String name, boolean connectionless, 
                  boolean fragmentation, Class addressType, Class sessionConfigType, Class envelopeTypes[])
    {
        
        if(providerName == null)
            throw new IllegalArgumentException("providerName");
        if(name == null)
            throw new IllegalArgumentException("name");
        providerName = providerName.trim().toLowerCase();
        if(providerName.length() == 0)
            throw new IllegalArgumentException("providerName is empty.");
        name = name.trim().toLowerCase();
        if(name.length() == 0)
            throw new IllegalArgumentException("name is empty.");
        if(addressType == null)
            throw new IllegalArgumentException("addressType");
        if(envelopeTypes == null)
            throw new IllegalArgumentException("envelopeTypes");
        if(envelopeTypes.length == 0)
            throw new IllegalArgumentException("envelopeTypes is empty.");
        if(sessionConfigType == null)
            throw new IllegalArgumentException("sessionConfigType");
        this.providerName = providerName;
        this.name = name;
        this.connectionless = connectionless;
        this.fragmentation = fragmentation;
        this.addressType = addressType;
        this.sessionConfigType = sessionConfigType;
        Set newEnvelopeTypes = new IdentityHashSet();
        Class aclass[] = envelopeTypes;
        int i = aclass.length;
        for(int j = 0; j < i; j++)
        {
            Class c = aclass[j];
            newEnvelopeTypes.add(c);
        }
        this.envelopeTypes = Collections.unmodifiableSet(newEnvelopeTypes);
    } 
}

下面这部分是报文会话配置,不做太多介绍:
//DatagramSessionConfig
/**
 * An {@link IoSessionConfig} for datagram transport type.
 *
 * @author The Apache Directory Project (mina-dev@directory.apache.org)
 * @version $Rev$, $Date$
 */
public interface DatagramSessionConfig extends IoSessionConfig {
    /**
     * @see DatagramSocket#getBroadcast()
     */
    boolean isBroadcast();

    /**
     * @see DatagramSocket#setBroadcast(boolean)
     */
    void setBroadcast(boolean broadcast);

    /**
     * @see DatagramSocket#getReuseAddress()
     */
    boolean isReuseAddress();

    /**
     * @see DatagramSocket#setReuseAddress(boolean)
     */
    void setReuseAddress(boolean reuseAddress);

    /**
     * @see DatagramSocket#getReceiveBufferSize()
     */
    int getReceiveBufferSize();

    /**
     * @see DatagramSocket#setReceiveBufferSize(int)
     */
    void setReceiveBufferSize(int receiveBufferSize);

    /**
     * @see DatagramSocket#getSendBufferSize()
     */
    int getSendBufferSize();

    /**
     * @see DatagramSocket#setSendBufferSize(int)
     */
    void setSendBufferSize(int sendBufferSize);

    /**
     * @see DatagramSocket#getTrafficClass()
     */
    int getTrafficClass();

    /**
     * @see DatagramSocket#setTrafficClass(int)
     */
    void setTrafficClass(int trafficClass);
}

//AbstractDatagramSessionConfig
/**
 * The Datagram transport session configuration.
 * 
 * @author [url=http://mina.apache.org]Apache MINA Project[/url]
 */
public abstract class AbstractDatagramSessionConfig extends AbstractIoSessionConfig implements DatagramSessionConfig {
    /** Tells if we should close the session if the port is unreachable. Default to true */
    private boolean closeOnPortUnreachable = true;

    /**
     * {@inheritDoc}
     */
    @Override
    public void setAll(IoSessionConfig config) {
        super.setAll(config);
        
        if (!(config instanceof DatagramSessionConfig)) {
            return;
        }

        if (config instanceof AbstractDatagramSessionConfig) {
            // Minimize unnecessary system calls by checking all 'propertyChanged' properties.
            AbstractDatagramSessionConfig cfg = (AbstractDatagramSessionConfig) config;
            
            if (cfg.isBroadcastChanged()) {
                setBroadcast(cfg.isBroadcast());
            }
            
            if (cfg.isReceiveBufferSizeChanged()) {
                setReceiveBufferSize(cfg.getReceiveBufferSize());
            }
            
            if (cfg.isReuseAddressChanged()) {
                setReuseAddress(cfg.isReuseAddress());
            }
            
            if (cfg.isSendBufferSizeChanged()) {
                setSendBufferSize(cfg.getSendBufferSize());
            }
            
            if (cfg.isTrafficClassChanged() && getTrafficClass() != cfg.getTrafficClass()) {
                setTrafficClass(cfg.getTrafficClass());
            }
        } else {
            DatagramSessionConfig cfg = (DatagramSessionConfig) config;
            setBroadcast(cfg.isBroadcast());
            setReceiveBufferSize(cfg.getReceiveBufferSize());
            setReuseAddress(cfg.isReuseAddress());
            setSendBufferSize(cfg.getSendBufferSize());
            
            if (getTrafficClass() != cfg.getTrafficClass()) {
                setTrafficClass(cfg.getTrafficClass());
            }
        }
    }

    /**
     * @return <tt>true</tt> if and only if the <tt>broadcast</tt> property
     * has been changed by its setter method.  The system call related with
     * the property is made only when this method returns <tt>true</tt>.  By
     * default, this method always returns <tt>true</tt> to simplify implementation
     * of subclasses, but overriding the default behavior is always encouraged.
     */
    protected boolean isBroadcastChanged() {
        return true;
    }

    /**
     * @return <tt>true</tt> if and only if the <tt>receiveBufferSize</tt> property
     * has been changed by its setter method.  The system call related with
     * the property is made only when this method returns <tt>true</tt>.  By
     * default, this method always returns <tt>true</tt> to simplify implementation
     * of subclasses, but overriding the default behavior is always encouraged.
     */
    protected boolean isReceiveBufferSizeChanged() {
        return true;
    }

    /**
     * @return <tt>true</tt> if and only if the <tt>reuseAddress</tt> property
     * has been changed by its setter method.  The system call related with
     * the property is made only when this method returns <tt>true</tt>.  By
     * default, this method always returns <tt>true</tt> to simplify implementation
     * of subclasses, but overriding the default behavior is always encouraged.
     */
    protected boolean isReuseAddressChanged() {
        return true;
    }

    /**
     * @return <tt>true</tt> if and only if the <tt>sendBufferSize</tt> property
     * has been changed by its setter method.  The system call related with
     * the property is made only when this method returns <tt>true</tt>.  By
     * default, this method always returns <tt>true</tt> to simplify implementation
     * of subclasses, but overriding the default behavior is always encouraged.
     */
    protected boolean isSendBufferSizeChanged() {
        return true;
    }

    /**
     * @return <tt>true</tt> if and only if the <tt>trafficClass</tt> property
     * has been changed by its setter method.  The system call related with
     * the property is made only when this method returns <tt>true</tt>.  By
     * default, this method always returns <tt>true</tt> to simplify implementation
     * of subclasses, but overriding the default behavior is always encouraged.
     */
    protected boolean isTrafficClassChanged() {
        return true;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean isCloseOnPortUnreachable() {
        return closeOnPortUnreachable;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setCloseOnPortUnreachable(boolean closeOnPortUnreachable) {
        this.closeOnPortUnreachable = closeOnPortUnreachable;
    }
}

//NioDatagramSessionConfig
/**
 * Define the configuration for a Datagram based session. 
 * 
 * @author [url=http://mina.apache.org]Apache MINA Project[/url]
 */
class NioDatagramSessionConfig extends AbstractDatagramSessionConfig {
    /** The associated channel 关联报文通道*/
    private final DatagramChannel channel;

    /**
     * Creates a new instance of NioDatagramSessionConfig, associated
     * with the given DatagramChannel.
     *
     * @param channel The associated DatagramChannel
     */
    NioDatagramSessionConfig(DatagramChannel channel) {
        this.channel = channel;
    }

    /**
     * Get the Socket receive buffer size for this DatagramChannel.
     * 
     * @return the DatagramChannel receive buffer size.
     * @throws RuntimeIoException if the socket is closed or if we 
     * had a SocketException
     * 
     * @see DatagramSocket#getReceiveBufferSize()
     */
    @Override
    public int getReceiveBufferSize() {
        try {
            return channel.socket().getReceiveBufferSize();
        } catch (SocketException e) {
            throw new RuntimeIoException(e);
        }
    }

    /**
     * Set the Socket receive buffer size for this DatagramChannel. <br>
     * <br>
     * Note : The underlying Socket may not accept the new buffer's size.
     * The user has to check that the new value has been set. 
     * 
     * @param receiveBufferSize the DatagramChannel receive buffer size.
     * @throws RuntimeIoException if the socket is closed or if we 
     * had a SocketException
     * 
     * @see DatagramSocket#setReceiveBufferSize(int)
     */
    @Override
    public void setReceiveBufferSize(int receiveBufferSize) {
        try {
            channel.socket().setReceiveBufferSize(receiveBufferSize);
        } catch (SocketException e) {
            throw new RuntimeIoException(e);
        }
    }

    /**
     * Tells if SO_BROADCAST is enabled.
     * 
     * @return <tt>true</tt> if SO_BROADCAST is enabled
     * @throws RuntimeIoException If the socket is closed or if we get an
     * {@link SocketException} 
     */
    @Override
    public boolean isBroadcast() {
        try {
            return channel.socket().getBroadcast();
        } catch (SocketException e) {
            throw new RuntimeIoException(e);
        }
    }

    @Override
    public void setBroadcast(boolean broadcast) {
        try {
            channel.socket().setBroadcast(broadcast);
        } catch (SocketException e) {
            throw new RuntimeIoException(e);
        }
    }

    /**
     * 
     * @throws RuntimeIoException If the socket is closed or if we get an
     * {@link SocketException} 
     */
    @Override
    public int getSendBufferSize() {
        try {
            return channel.socket().getSendBufferSize();
        } catch (SocketException e) {
            throw new RuntimeIoException(e);
        }
    }

    /**
     * 
     * @throws RuntimeIoException If the socket is closed or if we get an
     * {@link SocketException} 
     */
    @Override
    public void setSendBufferSize(int sendBufferSize) {
        try {
            channel.socket().setSendBufferSize(sendBufferSize);
        } catch (SocketException e) {
            throw new RuntimeIoException(e);
        }
    }

    /**
     * Tells if SO_REUSEADDR is enabled.
     * 
     * @return <tt>true</tt> if SO_REUSEADDR is enabled
     * @throws RuntimeIoException If the socket is closed or if we get an
     * {@link SocketException} 
     */
    @Override
    public boolean isReuseAddress() {
        try {
            return channel.socket().getReuseAddress();
        } catch (SocketException e) {
            throw new RuntimeIoException(e);
        }
    }

    /**
     * 
     * @throws RuntimeIoException If the socket is closed or if we get an
     * {@link SocketException} 
     */
    @Override
    public void setReuseAddress(boolean reuseAddress) {
        try {
            channel.socket().setReuseAddress(reuseAddress);
        } catch (SocketException e) {
            throw new RuntimeIoException(e);
        }
    }

    /**
     * Get the current Traffic Class for this Socket, if any. As this is
     * not a mandatory feature, the returned value should be considered as 
     * a hint. 
     * 
     * @return The Traffic Class supported by this Socket
     * @throws RuntimeIoException If the socket is closed or if we get an
     * {@link SocketException} 
     */
    @Override
    public int getTrafficClass() {
        try {
            return channel.socket().getTrafficClass();
        } catch (SocketException e) {
            throw new RuntimeIoException(e);
        }
    }

    /**
     * {@inheritDoc}
     * @throws RuntimeIoException If the socket is closed or if we get an
     * {@link SocketException} 
     */
    @Override
    public void setTrafficClass(int trafficClass) {
        try {
            channel.socket().setTrafficClass(trafficClass);
        } catch (SocketException e) {
            throw new RuntimeIoException(e);
        }
    }
}


0
1
分享到:
评论

相关推荐

    AMESim仿真平台在电动汽车热泵空调系统设计与优化的应用解析

    内容概要:本文深入探讨了AMESim仿真平台在电动汽车(EV)热泵空调系统设计与优化中的应用。首先介绍了AMESim的基础建模方法,如构建制冷循环模型中的压缩机、蒸发器和冷凝器等组件,并详细解释了各部件的工作原理及其参数设定。接着重点阐述了EV热泵空调系统的特殊之处,即不仅能够制冷还可以在冬季提供高效的制热功能,这对于提高电动汽车在寒冷条件下的续航里程和乘坐舒适性非常重要。文中给出了几个具体的案例,包括通过改变压缩机运行频率来进行性能优化,以及针对低温环境下热泵系统的控制策略,如四通阀切换逻辑、电子膨胀阀开度调节等。此外,还讨论了热泵系统与其他子系统(如电池温控)之间的协同工作方式,强调了系统集成的重要性。最后分享了一些实用的经验技巧,例如如何避免仿真过程中可能出现的问题,怎样评估系统的整体性能等。 适合人群:从事汽车工程、暖通空调(HVAC)领域的研究人员和技术人员,特别是关注新能源汽车热管理系统的专业人士。 使用场景及目标:适用于希望深入了解电动汽车热泵空调系统特性的工程师们,旨在帮助他们掌握基于AMESim进行系统建模、仿真分析的方法论,以便更好地指导实际产品研发。 阅读建议:由于涉及到较多的专业术语和技术细节,建议读者具备一定的机械工程背景知识,同时配合官方文档或其他参考资料一起研读,以加深理解。

    dtcwt 双树复小波的matlab工具箱

    dtcwt 双树复小波的matlab工具箱。内容来源于网络分享,如有侵权请联系我删除。

    基于Hadoop的朴素贝叶斯分类(MapReduce实现).zip

    基于hadoop的系统

    永磁同步电机中电流预测控制与广义预测控制(速度环)的技术解析及应用

    内容概要:本文探讨了永磁同步电机(PMSM)控制系统中两种先进的控制策略:电流预测控制和广义预测控制(GPC),特别是在速度环中结合扩展状态观测器(ESO)的应用。文章首先介绍了广义预测控制的基本原理及其在速度环中的实现方式,强调了其通过滚动优化对未来系统输出进行预测的能力。接着讨论了电流环中采用的双矢量改进预测控制算法,该算法通过优化两个电压矢量的选择提高了电流控制的精度和动态响应速度。此外,文章还提供了具体的代码示例和技术细节,帮助读者更好地理解和实现这些控制策略。最后,推荐了几篇相关文献供进一步学习。 适合人群:从事电机控制领域的研究人员、工程师以及对预测控制感兴趣的高校师生。 使用场景及目标:适用于需要提高永磁同步电机控制系统性能的研究项目或工业应用场景,旨在实现更精确、高效的电机控制,增强系统的鲁棒性和稳定性。 其他说明:文中提到的方法已在实验室环境中进行了验证,并取得了显著的效果,如减小了突加负载时的速度跌落幅度,降低了电流谐波失真度等。同时,作者分享了一些实用的调试技巧和注意事项,有助于加速实际项目的开发进程。

    期末作业Python实现基于图神经网络的信任评估项目源代码+使用说明(高分项目)

    期末作业Python实现基于图神经网络的信任评估项目源代码+使用说明(高分项目),个人经导师指导并认可通过的高分设计项目,评审分99分,代码完整确保可以运行,小白也可以亲自搞定,主要针对计算机相关专业的正在做大作业的学生和需要项目实战练习的学习者,可作为毕业设计、课程设计、期末大作业,代码资料完整,下载可用。 期末作业Python实现基于图神经网络的信任评估项目源代码+使用说明(高分项目)期末作业Python实现基于图神经网络的信任评估项目源代码+使用说明(高分项目)期末作业Python实现基于图神经网络的信任评估项目源代码+使用说明(高分项目)期末作业Python实现基于图神经网络的信任评估项目源代码+使用说明(高分项目)期末作业Python实现基于图神经网络的信任评估项目源代码+使用说明(高分项目)期末作业Python实现基于图神经网络的信任评估项目源代码+使用说明(高分项目)期末作业Python实现基于图神经网络的信任评估项目源代码+使用说明(高分项目)期末作业Python实现基于图神经网络的信任评估项目源代码+使用说明(高分项目)期末作业Python实现基于图神经网络的信任评估项目源代码+使用说明(高分项目)期末作业Python实现基于图神经网络的信任评估项目源代码+使用说明(高分项目)期末作业Python实现基于图神经网络的信任评估项目源代码+使用说明(高分项目)期末作业Python实现基于图神经网络的信任评估项目源代码+使用说明(高分项目)期末作业Python实现基于图神经网络的信任评估项目源代码+使用说明(高分项目)期末作业Python实现基于图神经网络的信任评估项目源代码+使用说明(高分项目)期末作业Python实现基于图神经网络的信任评估项目源代码+使用说明(高分项目)期末作业Python实现基于图神经网络的信任评估项目源代码+使用说明(高分项目)期末作

    开源6轴机械臂控制器SmallRobotArm AR3:离线控制与运动学算法详解

    内容概要:本文详细介绍了开源6轴机械臂控制器SmallRobotArm AR3的功能特点和技术细节。首先,文章展示了AR3可以直接通过板载按钮进行关节和坐标运动控制,无需连接电脑,提供了便捷的操作体验。其次,深入探讨了其核心算法,包括正逆解算法、运动模式切换逻辑以及平滑处理机制。此外,还介绍了自动标定程序、G代码支持和扩展功能如手势控制等特性。最后,强调了AR3的高扩展性和易用性,使其成为机器人爱好者和专业开发者的理想选择。 适合人群:对机器人技术和机械臂感兴趣的初学者、中级开发者及研究人员。 使用场景及目标:适用于教育、科研、DIY项目等领域,旨在帮助用户快速掌握机械臂控制原理并应用于实际任务中。 其他说明:文中提供的代码片段有助于理解和实现相关功能,而详细的硬件介绍则为动手能力强的用户提供改装灵感。

    编程竞赛蓝桥杯Python模拟题集锦与解析:涵盖字符串处理、数字运算及算法设计

    内容概要:本文档提供了10道蓝桥杯Python模拟题及其详细解答过程,涵盖字符串处理、数字运算、算法设计等多个方面。题目包括数字反转、字母转换、数字分段和、质数判断、字符串统计、数独验证、最大子数组和、括号匹配、斐波那契数列和文件统计。每道题目不仅给出了详细的解题思路和分析,还附有完整的Python源代码。通过这些题目,读者可以系统地提升编程能力和算法思维,掌握常见的编程技巧和方法。 适合人群:适合有一定编程基础的Python学习者,特别是准备参加蓝桥杯或其他编程竞赛的学生和爱好者。 使用场景及目标:①作为蓝桥杯赛前训练资料,帮助参赛者熟悉比赛题型和解题思路;②作为编程练习题集,巩固Python语言的基础知识和常用算法;③通过实际编程练习,提高解决实际问题的能力和编程水平。 阅读建议:建议读者先尝试独立完成每道题目,然后再参考提供的解题思路和代码,对比自己的解法,找出差距并加以改进。同时,注意理解每道题目背后的算法思想和编程技巧,以便举一反三,灵活应用。

    web_6_login.rar

    web_6_login.rar

    AI插件实用脚本illustrator-scripts-master

    脚本插件 1.Ai转PS矢量图层 2.分割线段 3.图形变换 4.圆形填充 5.文字块排版 6.多页PDF文档置入 将jsx文件复制到\Abobe Illustrator XX\Presets(在部分AI软件中可能显示为“预设”)\zh_CN\脚本 文件夹下,重新启动ai,就可以在"文件"-"脚本"下看见ai脚本菜单,运行即可。

    嵌入式系统中滑模观测器与PLL的C代码实现及其在STM32F1平台的应用

    内容概要:本文详细介绍了滑模观测器和PLL在STM32F1平台上的C代码实现。滑模观测器用于估计系统内部状态,尤其适用于电机控制领域;PLL则用于确保输出信号相位与输入信号相位保持一致。文中展示了两种滑模观测器的实现方法:一种采用符号函数进行硬切换,另一种采用饱和函数进行软化处理。此外,文章还强调了使用TI的IQmath库进行定点计算加速,以提高运算效率并减少资源占用。通过具体的代码示例和调试技巧,作者分享了如何在STM32F1平台上实现高效稳定的滑模观测器和PLL系统。 适合人群:嵌入式系统开发者、电机控制系统工程师、熟悉C语言编程的技术人员。 使用场景及目标:① 实现高效的滑模观测器和PLL系统,应用于电机控制和其他实时性要求较高的场景;② 学习如何使用IQmath库进行定点计算加速,优化嵌入式系统的性能;③ 掌握调试技巧,确保系统稳定运行。 其他说明:文章提供了详细的代码示例和调试经验,帮助读者更好地理解和实现滑模观测器和PLL系统。同时,文中提到的一些注意事项和常见问题解决方案也非常实用。

    基于纳什谈判与ADMM的共享储能电站优化运行解决方案

    内容概要:本文探讨了利用纳什谈判理论和交替方向乘子法(ADMM)解决共享储能电站中多用户之间的利益分配问题。文中详细介绍了将复杂的利益分配问题转化为数学模型的方法,以及如何通过ADMM算法实现分布式优化,确保各参与方都能接受的最优解。同时,文章还展示了实际应用案例,证明了该方法的有效性和优越性。 适合人群:从事电力系统优化、储能技术研究的专业人士,以及对分布式优化算法感兴趣的科研人员。 使用场景及目标:适用于多个用户共同使用储能设施的场景,旨在通过科学合理的算法设计,实现储能系统的高效运行和利益的最大化分配。 其他说明:文章不仅提供了详细的算法实现步骤,还包括了一些实用的代码片段和实验结果分析,帮助读者更好地理解和应用相关技术。此外,文中提到的一些反直觉现象也为进一步的研究提供了思路。

    08011车TC1车ER(A、B网)通讯故障分析报告 .docx

    08011车TC1车ER(A、B网)通讯故障分析报告 .docx

    2025年机器人+人工智能工业应用研究报告.pdf

    2025年机器人+人工智能工业应用研究报告.pdf

    智能优化领域的麻雀搜索算法(DEH_SSA)改进及其Python实现

    内容概要:本文深入探讨了麻雀搜索算法(SSA)的一种改进版本——融合差分进化和多策略的麻雀搜索算法(DEH_SSA)。文章首先介绍了反向学习初始化种群、非线性因子改进发现者策略、改进警觉性策略、融合差分算法以及精英扰动策略五大创新点。接着详细展示了这些策略的具体实现方法,如反向学习初始化通过计算反向解使初始种群分布更合理,非线性因子改进发现者策略通过动态调整搜索步长提高搜索效率等。此外,文中提供了23个基准测试函数的实验结果,证明了DEH_SSA相比传统SSA在收敛速度和最优解质量上的显著优势。最后给出了详细的调参指南,帮助初学者更好地理解和应用该算法。 适合人群:对智能优化算法感兴趣的研究人员和技术开发者,尤其是有一定编程基础并对群体智能算法有初步了解的人士。 使用场景及目标:适用于需要解决复杂优化问题的场景,如工程优化、机器学习超参数调优等领域。目标是提供一种高效的优化工具,帮助用户更快更精确地找到全局最优解。 其他说明:文章不仅提供了理论解释,还有丰富的代码实例,便于读者动手实践。同时,附带的调参指南有助于读者根据具体问题调整算法参数,获得更好的优化效果。

    西门子WinCC报表系统多版本通用模板及优化技术详解

    内容概要:本文详细介绍了西门子WinCC报表系统的多版本通用模板及其优化技术。涵盖了日报表、月报表、年报表、自由报表及班次报表等多种类型的报表模板,提供了详细的代码示例,包括VBS脚本、SQL查询语句、C脚本等。文中强调了性能优化措施,如索引建立、参数化查询、存储过程的应用等,并附有配套的视频教程帮助理解和实践。此外,还讨论了班次报表的灵活性设计、数据分页技术、错误处理机制等方面的内容。 适合人群:已经具备WinCC脚本基础和SQL语言基础的技术人员,尤其是从事工业自动化领域的工程师。 使用场景及目标:适用于需要频繁生成各类生产报表的企业,旨在提高数据查询效率,满足不同客户的定制化需求,同时确保系统的稳定性和兼容性。通过学习本文提供的模板和技术,用户能够更好地应对复杂的生产数据管理和报表生成任务。 其他说明:文中提到的所有技术手段均经过实际测试验证,可在多个版本的WinCC环境中稳定运行。配套的视频教程进一步降低了学习门槛,使用户能够更快地上手操作。

    ANPC并网逆变器的SPWM控制与电流闭环仿真实现

    内容概要:本文详细介绍了ANPC并网逆变器的闭环仿真建模过程,涵盖了直流电压源的稳定接入、SPWM调制、电流闭环控制以及锁相环的应用。作者通过具体的代码示例和技术细节,展示了如何利用SPWM生成稳定的正弦波,通过锁相环确保电流和电压的相位一致,并采用前馈解耦提高电流控制的精度。同时,文中还讨论了一些常见的仿真问题及其解决方案,如死区效应、调制比限制和电流环PI参数的优化。 适合人群:电力电子工程师、控制系统设计师、从事逆变器研究的技术人员。 使用场景及目标:适用于希望深入了解ANPC并网逆变器控制策略的研究人员和工程师,旨在帮助他们掌握SPWM调制、电流闭环控制和锁相环的设计与实现。 其他说明:文章提供了丰富的代码片段和实践经验,有助于读者快速理解和应用相关技术。此外,文中提到的一些常见问题和解决方案也为实际项目提供了宝贵的参考。

    groovy-3.0.7.jar中文文档.zip

    # 压缩文件中包含: 中文文档 jar包下载地址 Maven依赖 Gradle依赖 源代码下载地址 # 本文件关键字: jar中文文档.zip,java,jar包,Maven,第三方jar包,组件,开源组件,第三方组件,Gradle,中文API文档,手册,开发手册,使用手册,参考手册 # 使用方法: 解压最外层zip,再解压其中的zip包,双击 【index.html】 文件,即可用浏览器打开、进行查看。 # 特殊说明: ·本文档为人性化翻译,精心制作,请放心使用。 ·只翻译了该翻译的内容,如:注释、说明、描述、用法讲解 等; ·不该翻译的内容保持原样,如:类名、方法名、包名、类型、关键字、代码 等。 # 温馨提示: (1)为了防止解压后路径太长导致浏览器无法打开,推荐在解压时选择“解压到当前文件夹”(放心,自带文件夹,文件不会散落一地); (2)有时,一套Java组件会有多个jar,所以在下载前,请仔细阅读本篇描述,以确保这就是你需要的文件;

    groovy-2.4.9.jar中文文档.zip

    # 压缩文件中包含: 中文文档 jar包下载地址 Maven依赖 Gradle依赖 源代码下载地址 # 本文件关键字: jar中文文档.zip,java,jar包,Maven,第三方jar包,组件,开源组件,第三方组件,Gradle,中文API文档,手册,开发手册,使用手册,参考手册 # 使用方法: 解压最外层zip,再解压其中的zip包,双击 【index.html】 文件,即可用浏览器打开、进行查看。 # 特殊说明: ·本文档为人性化翻译,精心制作,请放心使用。 ·只翻译了该翻译的内容,如:注释、说明、描述、用法讲解 等; ·不该翻译的内容保持原样,如:类名、方法名、包名、类型、关键字、代码 等。 # 温馨提示: (1)为了防止解压后路径太长导致浏览器无法打开,推荐在解压时选择“解压到当前文件夹”(放心,自带文件夹,文件不会散落一地); (2)有时,一套Java组件会有多个jar,所以在下载前,请仔细阅读本篇描述,以确保这就是你需要的文件;

    thumbnailator-0.4.16.jar中文-英文对照文档.zip

    # 压缩文件中包含: 中文-英文对照文档 jar包下载地址 Maven依赖 Gradle依赖 源代码下载地址 # 本文件关键字: jar中文-英文对照文档.zip,java,jar包,Maven,第三方jar包,组件,开源组件,第三方组件,Gradle,中文API文档,手册,开发手册,使用手册,参考手册 # 使用方法: 解压最外层zip,再解压其中的zip包,双击 【index.html】 文件,即可用浏览器打开、进行查看。 # 特殊说明: ·本文档为人性化翻译,精心制作,请放心使用。 ·只翻译了该翻译的内容,如:注释、说明、描述、用法讲解 等; ·不该翻译的内容保持原样,如:类名、方法名、包名、类型、关键字、代码 等。 # 温馨提示: (1)为了防止解压后路径太长导致浏览器无法打开,推荐在解压时选择“解压到当前文件夹”(放心,自带文件夹,文件不会散落一地); (2)有时,一套Java组件会有多个jar,所以在下载前,请仔细阅读本篇描述,以确保这就是你需要的文件;

    基于折射反向学习和自适应惯性权重的MATLAB蝴蝶优化算法改进及其应用

    内容概要:本文深入探讨了基于折射反向学习策略和自适应惯性权重机制改进的蝴蝶优化算法(BOA)。首先介绍了折射对立学习策略用于构建精英种群的方法,通过折射对立操作提高种群质量和多样性。接着阐述了自适应惯性权重机制的作用,即通过动态调整权重来平衡全局搜索和局部开发的能力。文中详细展示了这两种改进策略的具体MATLAB代码实现,并通过23种测试函数进行了性能对比,证明改进后的BOA在复杂多峰函数上表现出显著优势。此外,文章还讨论了改进算法在工程优化问题中的应用实例,如光伏阵列参数优化,展示了其实用价值。 适合人群:对优化算法感兴趣的科研人员、研究生以及从事相关领域的工程师。 使用场景及目标:适用于解决复杂的多峰优化问题,特别是在需要高效求解全局最优解的情况下。目标是提供一种改进的BOA算法,能够更好地应对复杂优化任务,提高求解效率和准确性。 其他说明:文章提供了详细的代码注释和测试数据,便于读者理解和复现实验结果。同时,文中还分享了一些实用的小技巧,如如何调整参数以应对不同类型的优化问题。

Global site tag (gtag.js) - Google Analytics