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.AutoCloseableAbstract base class of an nng Socket, implementing the common nng api for Socket communication.
-
-
Field Summary
Fields Modifier and Type Field Description protected SocketStruct.ByValuesocket
-
Constructor Summary
Constructors Modifier Constructor Description protectedSocket(java.util.function.Function<SocketStruct,java.lang.Integer> socketOpener)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Close the Socket.voiddial(java.lang.String url)Dial the given urlSocketStructgetSocketStruct()voidlisten(java.lang.String url)Listen for connections on the given urllongreceive(java.nio.ByteBuffer buffer)Receive a raw message into the providedByteBuffer, blocking until data is received or a timeout occurs.longreceive(java.nio.ByteBuffer buffer, boolean blockUntilReceived)Receives raw data on the Socket into aByteBufferNote: Currently does not support NNG_FLAG_ALLOC, forcing the caller to own and manage the lifetime of the provided ByteBufferMessagereceiveMessage()Attempt to receive a Message on the Socket.MessagereceiveMessage(int flags)voidsend(java.nio.ByteBuffer data)Send raw data over aSocketvoidsend(java.nio.ByteBuffer data, boolean blockUntilSent)Sends raw data over the Socket.voidsendMessage(Message msg)Send the provide Message on the Socket.voidsendMessage(java.nio.ByteBuffer buffer)Send a new Message using the provided ByteBuffer for the message bodyvoidsendMessage(java.nio.ByteBuffer body, java.nio.ByteBuffer header)voidsetReceiveTimeout(int timeoutMillis)Set the timeout for receive requests on the SocketvoidsetSendTimeout(int timeoutMillis)Set the timeout for send requests on the SocketvoidsetTlsConfig(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 NngExceptionClose 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:
closein interfacejava.lang.AutoCloseable- Throws:
NngException- if an nng error occurs
-
setReceiveTimeout
public void setReceiveTimeout(int timeoutMillis) throws NngExceptionSet 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 NngExceptionSet 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 NngExceptionSend 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 NngExceptionDial 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 NngExceptionListen 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 NngExceptionSends 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 NngExceptionSend raw data over aSocket- Parameters:
data- aByteBuffercontaining 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.IllegalArgumentExceptionReceives raw data on the Socket into aByteBufferNote: 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 NngExceptionReceive 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()
-
-