Thread: NIO Networking and Netty's Channel Buffer, Please help *Smashes head on wall*

Results 1 to 2 of 2
  1. #1 NIO Networking and Netty's Channel Buffer, Please help *Smashes head on wall* 
    Registered Member
    CTucker's Avatar
    Join Date
    Oct 2008
    Posts
    2,422
    Thanks given
    263
    Thanks received
    281
    Rep Power
    343
    I've been wanting to look into standard NIO networking and walk away from Netty for awhile, I don't know why, I just feel like the knowledge would be great, however one thing that I do enjoy is being able to use an Input/Output "Stream". In NIO you can't directly use a stream from my understanding, which is why Netty uses the ChannelBuffer class, and the 14,000 other subclasses that come with it. I've managed to put the ChannelBuffer class into my NIO project, but I'm having a hard time figuring out where the data is added to the ChannelBuffer, and where the class that implements the ChannelBuffer is being initialized/passed off to the pipeline.

    I've been digging for quite awhile, here's what I've found so far:

    ServerBootstrap.java
    Code:
     @Override
            public void channelOpen(
    ...
    if (!"pipelineFactory".equals(e.getKey())) {
                            parentOptions.put(e.getKey(), e.getValue());
                        }
    ...
    evt.getChannel().getConfig().setOptions(parentOptions);
    ChannelConfig.java
    Code:
        /**
         * Returns the default {@link ChannelBufferFactory} used to create a new
         * {@link ChannelBuffer}.  The default is {@link HeapChannelBufferFactory}.
         * You can specify a different factory to change the default
         * {@link ByteOrder} for example.
         */
        ChannelBufferFactory getBufferFactory();
    
        /**
         * Sets the default {@link ChannelBufferFactory} used to create a new
         * {@link ChannelBuffer}.  The default is {@link HeapChannelBufferFactory}.
         * You can specify a different factory to change the default
         * {@link ByteOrder} for example.
         */
        void setBufferFactory(ChannelBufferFactory bufferFactory);
    DefaultChannelConfig.java
    Code:
    public boolean setOption(String key, Object value) {
            if (key == null) {
                throw new NullPointerException("key");
            }
    
            if ("pipelineFactory".equals(key)) {
                setPipelineFactory((ChannelPipelineFactory) value);
            } else if ("connectTimeoutMillis".equals(key)) {
                setConnectTimeoutMillis(ConversionUtil.toInt(value));
            } else if ("bufferFactory".equals(key)) {
                setBufferFactory((ChannelBufferFactory) value);
            } else {
                return false;
            }
            return true;
        }
    DefaultNioServerSocketConfig.java

    Code:
     @Override
        public void setOptions(Map<String, Object> options) {
            super.setOptions(options);
            if (getWriteBufferHighWaterMark() < getWriteBufferLowWaterMark()) {
                // Recover the integrity of the configuration with a sensible value.
                setWriteBufferLowWaterMark0(getWriteBufferHighWaterMark() >>> 1);
                if (logger.isWarnEnabled()) {
                    // Notify the user about misconfiguration.
                    logger.warn(
                            "writeBufferLowWaterMark cannot be greater than " +
                            "writeBufferHighWaterMark; setting to the half of the " +
                            "writeBufferHighWaterMark.");
                }
            }
        }
    
        @Override
        public boolean setOption(String key, Object value) {
            if (super.setOption(key, value)) {
                return true;
            }
    
            if ("writeBufferHighWaterMark".equals(key)) {
                setWriteBufferHighWaterMark0(ConversionUtil.toInt(value));
            } else if ("writeBufferLowWaterMark".equals(key)) {
                setWriteBufferLowWaterMark0(ConversionUtil.toInt(value));
            } else if ("writeSpinCount".equals(key)) {
                setWriteSpinCount(ConversionUtil.toInt(value));
            } else if ("receiveBufferSizePredictorFactory".equals(key)) {
                setReceiveBufferSizePredictorFactory((ReceiveBufferSizePredictorFactory) value);
            } else if ("receiveBufferSizePredictor".equals(key)) {
                setReceiveBufferSizePredictor((ReceiveBufferSizePredictor) value);
            } else {
                return false;
            }
            return true;
        }
    NioWorker.java
    Code:
     @Override
        protected boolean read(SelectionKey k) {
    ...
    final ChannelBufferFactory bufferFactory = channel.getConfig().getBufferFactory();
    ...
    You can see that it's passing the Key/Value by String/Object where the object is a class, but I can't find at all where these are even being constructed from.

    I've been tearing through these, class after class, but I've yet to figure out the implementation, basically I'm trying to implement the Netty ChannelBuffer in my own NIO application, for simplicity purposes.
    Reply With Quote  
     

  2. #2  
    ???

    funkE's Avatar
    Join Date
    Feb 2008
    Posts
    2,612
    Thanks given
    255
    Thanks received
    989
    Rep Power
    1366
    so you want to walk away from netty but recreate it

    what is the point exactly


    you read a block of bytes, then handle it. it works that way internally. i'd do something like reading until you can't read anymore and then push a bytebuffer downstream (or similar operation)

    https://docs.oracle.com/javase/7/doc...yteBuffer.html

    you don't need a bunch of classes to make use of basic nio. java has plenty of things in their api to get what you need done, honestly. if you're doing it for an rsps, i'd just write my own class that reads the data types to ensure byte order is correct and to handle custom types
    .
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Replies: 1
    Last Post: 01-17-2015, 09:31 PM
  2. Replies: 67
    Last Post: 06-27-2012, 03:57 PM
  3. Replies: 5
    Last Post: 07-29-2010, 11:49 AM
  4. Replies: 3
    Last Post: 02-28-2010, 02:08 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •