#include <JavaSocket.hpp>
Inheritance diagram for CarobNS::JavaSocket:
Public Member Functions | |
bool | isValid () const |
Gives socket validity status. | |
bool | isConnected () const |
Gives connection status. | |
void | create (int domain) throw (ConnectionException, UnexpectedException) |
Creates the socket. | |
bool | connectTo (const SocketAddress &addr) throw (ConnectionException, UnexpectedException) |
Connects to the given socket address. | |
virtual bool | closeSystemSocket () throw (SocketIOException, UnexpectedException) |
Closes TCP socket. | |
size_t | writeJavaUTF (const std::wstring &str) const throw (SocketIOException, CodecException, UnexpectedException) |
Writes a UTF-8 encoded wstring to the socket. | |
size_t | readJavaUTF (std::wstring &str) const throw (SocketIOException, CodecException, UnexpectedException) |
Reads UTF-8 encoded string from socket. | |
void | writeJavaInt (int32_t i) const throw (SocketIOException, UnexpectedException) |
Writes 32bits-integer to socket. | |
void | readJavaInt (int32_t &i) const throw (SocketIOException, UnexpectedException) |
Reads 32bits-integer from socket. | |
void | writeJavaLong (int64_t l) const throw (SocketIOException, UnexpectedException) |
Writes 64bits-long to socket. | |
void | readJavaLong (int64_t &l) const throw (SocketIOException, UnexpectedException) |
Reads 64bits-long from socket. | |
void | writeJavaBool (bool b) const throw (SocketIOException, UnexpectedException) |
Writes a boolean (as a byte) to the socket. | |
void | readJavaBool (bool &b) const throw (SocketIOException, UnexpectedException) |
Reads a boolean (byte) from the socket. | |
void | readJavaBytes (int32_t length, java_byte *data) const throw (SocketIOException, UnexpectedException) |
Reads an array of bytes from the socket. | |
void | writeJavaBytes (int32_t length, java_byte *data) const throw (SocketIOException, UnexpectedException) |
Writes an array of bytes to the socket. | |
void | shutdown () |
Interrupts read operations on this socket, mark it as disconnected and makes read operations throw a SocketIOException. | |
Static Public Member Functions | |
static int | pollOnSingleFd (int socketFd, int pollTimeoutInMs, bool pollOnWrites=false) |
Execute poll (or select depending on compilation flag CAROB_USE_SELECT) on the given file descriptor and return after either data is ready to be read or given timeout is elapsed. | |
Protected Member Functions | |
JavaSocket () throw (CodecException) | |
Default constructor - No connection here, just creates and empty socket. | |
virtual | ~JavaSocket () |
Destructor for disconnection - closes the socket. | |
virtual int | recvFully (void *buf, const int len, const int flags) const throw (SocketIOException, UnexpectedException) |
Substitute for recv. | |
virtual void | sendToSocket (const wchar_t fctName[], const void *buf, int len, int flags) const throw (SocketIOException, UnexpectedException) |
Wrapper over send(. | |
void | receiveFromSocket (const wchar_t fctName[], void *buf, int len, int flags) const throw (SocketIOException, UnexpectedException) |
Wrapper around recv(. | |
Protected Attributes | |
int | socket_fd |
Socket file descriptor. | |
bool | connected |
true if the socket is connected to a host | |
bool | canceled |
true cancels all input and connections attempts |
This class can read and write basic Java types over a socket. It does *not* follow any protocol but java pure serialization.
virtual CarobNS::JavaSocket::~JavaSocket | ( | ) | [protected, virtual] |
virtual bool CarobNS::JavaSocket::closeSystemSocket | ( | ) | throw (SocketIOException, UnexpectedException) [virtual] |
Closes TCP socket.
Why public?
SocketIOException |
bool CarobNS::JavaSocket::connectTo | ( | const SocketAddress & | addr | ) | throw (ConnectionException, UnexpectedException) |
Connects to the given socket address.
addr | socket address of the host to connect to |
ConnectionException |
void CarobNS::JavaSocket::create | ( | int | domain | ) | throw (ConnectionException, UnexpectedException) |
Creates the socket.
domain | communication domain (supported = PF_INET or PF_INET6) |
ConnectionException | if the socket could not be create or its options could not be set |
bool CarobNS::JavaSocket::isConnected | ( | ) | const [inline] |
Gives connection status.
bool CarobNS::JavaSocket::isValid | ( | ) | const [inline] |
Gives socket validity status.
static int CarobNS::JavaSocket::pollOnSingleFd | ( | int | socketFd, | |
int | pollTimeoutInMs, | |||
bool | pollOnWrites = false | |||
) | [static] |
Execute poll (or select depending on compilation flag CAROB_USE_SELECT) on the given file descriptor and return after either data is ready to be read or given timeout is elapsed.
socketFd | socket file descriptor on which to wait for incomming data | |
pollTimeoutInMs | timeout after which to return if no data is ready to be read on socket | |
pollOnWrites | true makes poll on write (false by default = on read) |
void CarobNS::JavaSocket::readJavaBool | ( | bool & | b | ) | const throw (SocketIOException, UnexpectedException) |
Reads a boolean (byte) from the socket.
b | resulting boolean to read from stream |
SocketIOException |
void CarobNS::JavaSocket::readJavaBytes | ( | int32_t | length, | |
java_byte * | data | |||
) | const throw (SocketIOException, UnexpectedException) |
Reads an array of bytes from the socket.
The array must be allocated !
length | number of bytes to read | |
data | allocated array of (at least) length bytes |
SocketIOException |
void CarobNS::JavaSocket::readJavaInt | ( | int32_t & | i | ) | const throw (SocketIOException, UnexpectedException) |
Reads 32bits-integer from socket.
i | resulting integer read from stream |
SocketIOException |
void CarobNS::JavaSocket::readJavaLong | ( | int64_t & | l | ) | const throw (SocketIOException, UnexpectedException) |
Reads 64bits-long from socket.
l | resulting long read from stream |
SocketIOException |
size_t CarobNS::JavaSocket::readJavaUTF | ( | std::wstring & | str | ) | const throw (SocketIOException, CodecException, UnexpectedException) |
Reads UTF-8 encoded string from socket.
This is NOT Java's modified UTF-8, see SEQUOIA-133.
str | string to read |
SocketIOException |
void CarobNS::JavaSocket::receiveFromSocket | ( | const wchar_t | fctName[], | |
void * | buf, | |||
int | len, | |||
int | flags | |||
) | const throw (SocketIOException, UnexpectedException) [protected] |
Wrapper around recv(.
..) function to handle errors and throw exceptions
fctName | name of the calling function (for logging purposes) | |
buf | buffer in which to put the received data in | |
len | length of buf | |
flags | send option, see send man page |
SocketIOException | in case of error |
virtual int CarobNS::JavaSocket::recvFully | ( | void * | buf, | |
const int | len, | |||
const int | flags | |||
) | const throw (SocketIOException, UnexpectedException) [protected, virtual] |
Substitute for recv.
Waits for incomming data by calling pollOnSingleFd and loops until full length has been received or an erro occured. If shutdown() is called during the loop, throws a SocketIOException to inform callers that the socket is not longer readable
buf | data to send | |
len | full buffer length | |
flags | send options, see recv man page |
SocketIOException | if interrupted by shutdown() function |
Reimplemented in CarobNS::BufferedSocket.
virtual void CarobNS::JavaSocket::sendToSocket | ( | const wchar_t | fctName[], | |
const void * | buf, | |||
int | len, | |||
int | flags | |||
) | const throw (SocketIOException, UnexpectedException) [protected, virtual] |
Wrapper over send(.
..) function to handle errors and throw exceptions
fctName | name of the calling function (for logging purposes) | |
buf | data to be send | |
len | length of buf | |
flags | send option, see recv man page |
SocketIOException |
Reimplemented in CarobNS::BufferedSocket.
void CarobNS::JavaSocket::shutdown | ( | ) | [inline] |
Interrupts read operations on this socket, mark it as disconnected and makes read operations throw a SocketIOException.
void CarobNS::JavaSocket::writeJavaBool | ( | bool | b | ) | const throw (SocketIOException, UnexpectedException) |
Writes a boolean (as a byte) to the socket.
b | the boolean to send |
SocketIOException |
void CarobNS::JavaSocket::writeJavaBytes | ( | int32_t | length, | |
java_byte * | data | |||
) | const throw (SocketIOException, UnexpectedException) |
Writes an array of bytes to the socket.
length | number of bytes to write | |
data | array of (at least) length bytes |
SocketIOException |
void CarobNS::JavaSocket::writeJavaInt | ( | int32_t | i | ) | const throw (SocketIOException, UnexpectedException) |
void CarobNS::JavaSocket::writeJavaLong | ( | int64_t | l | ) | const throw (SocketIOException, UnexpectedException) |
size_t CarobNS::JavaSocket::writeJavaUTF | ( | const std::wstring & | str | ) | const throw (SocketIOException, CodecException, UnexpectedException) |
Writes a UTF-8 encoded wstring to the socket.
This is NOT Java's modified UTF-8, see SEQUOIA-133.
str | string to write |
SocketIOException |