Package io.sisu.nng
Class Socket
- java.lang.Object
-
- io.sisu.nng.Socket
-
- All Implemented Interfaces:
java.lang.AutoCloseable
- Direct Known Subclasses:
Pair0Socket
,Pair1Socket
,Pub0Socket
,Pull0Socket
,Push0Socket
,RawRep0Socket
,Rep0Socket
,Req0Socket
,Respondent0
,Sub0Socket
,Surveyor0Socket
public abstract class Socket extends java.lang.Object implements java.lang.AutoCloseable
Abstract base class of an nng Socket, implementing the common nng api for Socket communication.
-
-
Field Summary
Fields Modifier and Type Field Description protected SocketStruct.ByValue
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 urlSocketStruct
getSocketStruct()
void
listen(java.lang.String url)
Listen for connections on the given urllong
receive(java.nio.ByteBuffer buffer)
Receive a raw message into the providedByteBuffer
, blocking until data is received or a timeout occurs.long
receive(java.nio.ByteBuffer buffer, boolean blockUntilReceived)
Receives raw data on the Socket into aByteBuffer
Note: Currently does not support NNG_FLAG_ALLOC, forcing the caller to own and manage the lifetime of the provided ByteBufferMessage
receiveMessage()
Attempt to receive a Message on the Socket.Message
receiveMessage(int flags)
void
send(java.nio.ByteBuffer data)
Send raw data over aSocket
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 bodyvoid
sendMessage(java.nio.ByteBuffer body, java.nio.ByteBuffer header)
void
setReceiveTimeout(int timeoutMillis)
Set the timeout for receive requests on the Socketvoid
setSendTimeout(int timeoutMillis)
Set the timeout for send requests on the Socketvoid
setTlsConfig(TlsConfig config)
-
-
-
Field Detail
-
socket
protected final SocketStruct.ByValue socket
-
-
Constructor Detail
-
Socket
protected Socket(java.util.function.Function<SocketStruct,java.lang.Integer> socketOpener) throws NngException
- Throws:
NngException
-
-
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 interfacejava.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 sendingjava.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
-
receiveMessage
public Message receiveMessage(int flags) throws NngException
- Throws:
NngException
-
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 sendblockUntilSent
- 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 aSocket
- Parameters:
data
- aByteBuffer
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 aByteBuffer
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 intoblockUntilReceived
- 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 occursjava.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 providedByteBuffer
, 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
-
setTlsConfig
public void setTlsConfig(TlsConfig config) throws NngException
- Throws:
NngException
-
getSocketStruct
public SocketStruct getSocketStruct()
-
-