Package io.sisu.nng

Class Socket

    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected Socket​(java.util.function.Function<SocketStruct,​java.lang.Integer> socketOpener)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Close the Socket.
      void dial​(java.lang.String url)
      Dial the given url
      SocketStruct getSocketStruct()  
      void listen​(java.lang.String url)
      Listen for connections on the given url
      long receive​(java.nio.ByteBuffer buffer)
      Receive a raw message into the provided ByteBuffer, blocking until data is received or a timeout occurs.
      long receive​(java.nio.ByteBuffer buffer, boolean blockUntilReceived)
      Receives raw data on the Socket into a ByteBuffer Note: Currently does not support NNG_FLAG_ALLOC, forcing the caller to own and manage the lifetime of the provided ByteBuffer
      Message receiveMessage()
      Attempt to receive a Message on the Socket.
      Message receiveMessage​(int flags)  
      void send​(java.nio.ByteBuffer data)
      Send raw data over a Socket
      void send​(java.nio.ByteBuffer data, boolean blockUntilSent)
      Sends raw data over the Socket.
      void sendMessage​(Message msg)
      Send the provide Message on the Socket.
      void sendMessage​(java.nio.ByteBuffer buffer)
      Send a new Message using the provided ByteBuffer for the message body
      void sendMessage​(java.nio.ByteBuffer body, java.nio.ByteBuffer header)  
      void setReceiveTimeout​(int timeoutMillis)
      Set the timeout for receive requests on the Socket
      void setSendTimeout​(int timeoutMillis)
      Set the timeout for send requests on the Socket
      void setTlsConfig​(TlsConfig config)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • close

        public void close()
                   throws NngException
        Close the Socket. Outstanding messages or data may or may not be flushed, depending on the protocol. Note: Data loss may occur if the Socket is closed if there are outstanding messages in the underlying send queue.
        Specified by:
        close in interface java.lang.AutoCloseable
        Throws:
        NngException - if an nng error occurs
      • setReceiveTimeout

        public void setReceiveTimeout​(int timeoutMillis)
                               throws NngException
        Set the timeout for receive requests on the Socket
        Parameters:
        timeoutMillis - timeout duration in milliseconds
        Throws:
        NngException - if an nng error occurs setting the timeout
      • setSendTimeout

        public void setSendTimeout​(int timeoutMillis)
                            throws NngException
        Set the timeout for send requests on the Socket
        Parameters:
        timeoutMillis - timeout duration in milliseconds
        Throws:
        NngException - if an nng error occurs setting the timeout
      • sendMessage

        public void sendMessage​(java.nio.ByteBuffer buffer)
                         throws NngException
        Send a new Message using the provided ByteBuffer for the message body
        Parameters:
        buffer - a ByteBuffer containing the message body
        Throws:
        NngException - if an nng error occurs during sending
      • sendMessage

        public void sendMessage​(java.nio.ByteBuffer body,
                                java.nio.ByteBuffer header)
                         throws NngException
        Throws:
        NngException
      • sendMessage

        public void sendMessage​(Message msg)
                         throws NngException
        Send the provide Message on the Socket. If the underlying nng call reports it's successfully taken and enqueued the Message, invalidate it.
        Parameters:
        msg - the Message to send
        Throws:
        NngException - if the socket raised an error when accepting the messaging for sending
        java.lang.IllegalStateException - if the Message state is invalid
      • receiveMessage

        public Message receiveMessage()
                               throws NngException
        Attempt to receive a Message on the Socket.
        Returns:
        the received Message, owned now by the JVM
        Throws:
        NngException - if the Socket raises an error on receiving
      • dial

        public void dial​(java.lang.String url)
                  throws NngException
        Dial the given url
        Parameters:
        url - a valid nng url as a String
        Throws:
        NngException - on an invalid url or failure to connect
      • listen

        public void listen​(java.lang.String url)
                    throws NngException
        Listen for connections on the given url
        Parameters:
        url - a valid nng url as a String
        Throws:
        NngException - on an invalid url or failure to listen
      • send

        public void send​(java.nio.ByteBuffer data,
                         boolean blockUntilSent)
                  throws NngException
        Sends raw data over the Socket. Note: explicitly does NOT support NNG_FLAG_ALLOC option as it's assumed the ByteBuffer is allocated and managed by the JVM and will not be freed by the call to nng_send.
        Parameters:
        data - a directly allocated ByteBuffer of data to send
        blockUntilSent - boolean flag determining if the caller waits until the message is accepted for sending by the Socket
        Throws:
        NngException - if the underlying nng_send call returns non-zero
      • send

        public void send​(java.nio.ByteBuffer data)
                  throws NngException
        Send raw data over a Socket
        Parameters:
        data - a ByteBuffer containing the data to send
        Throws:
        NngException - on error sending the data
      • receive

        public long receive​(java.nio.ByteBuffer buffer,
                            boolean blockUntilReceived)
                     throws NngException,
                            java.lang.IllegalArgumentException
        Receives raw data on the Socket into a ByteBuffer Note: Currently does not support NNG_FLAG_ALLOC, forcing the caller to own and manage the lifetime of the provided ByteBuffer
        Parameters:
        buffer - the ByteBuffer to receive data into
        blockUntilReceived - boolean flag determining if the call waits until the message is received or returns immediately
        Returns:
        long number of bytes received
        Throws:
        NngException - if an underlying nng error occurs
        java.lang.IllegalArgumentException - if the provided ByteBuffer is not a direct ByteBuffer
      • receive

        public long receive​(java.nio.ByteBuffer buffer)
                     throws NngException
        Receive a raw message into the provided ByteBuffer, blocking until data is received or a timeout occurs.
        Parameters:
        buffer - a direct ByteBuffer
        Returns:
        long number of bytes received
        Throws:
        NngException - if an nng error occurs