PxSocket

Defined in include/foundation/PxSocket.h

class PxSocket : public PxUserAllocated

Socket abstraction API.

Public Functions

PxSocket(bool inEnableBuffering = true, bool blocking = true)
virtual ~PxSocket()
bool connect(const char *host, uint16_t port, uint32_t timeout = 1000)

Opens a network socket for input and/or output

Parameters
  • host – Name of the host to connect to. This can be an IP, URL, etc

  • port – The port to connect to on the remote host

  • timeout – Timeout in ms until the connection must be established.

Returns

True if the connection was successful, false otherwise

bool listen(uint16_t port)

Opens a network socket for input and/or output as a server. Put the connection in listening mode

Parameters

port – The port on which the socket listens

bool accept(bool block)

Accept a connection on a socket that is in listening mode

Note

This method only supports a single connection client. Additional clients that connect to the listening port will overwrite the existing socket handle.

Parameters

block – whether or not the call should block

Returns

whether a connection was established

void disconnect()

Disconnects an open socket

bool isConnected() const

Returns whether the socket is currently open (connected) or not.

Returns

True if the socket is connected, false otherwise

const char *getHost() const

Returns the name of the connected host. This is the same as the string that was supplied to the connect call.

Returns

The name of the connected host

uint16_t getPort() const

Returns the port of the connected host. This is the same as the port that was supplied to the connect call.

Returns

The port of the connected host

bool flush()

Flushes the output stream. Until the stream is flushed, there is no guarantee that the written data has actually reached the destination storage. Flush forces all buffered data to be sent to the output.

Note

flush always blocks. If the socket is in non-blocking mode, this will result the thread spinning.

Returns

True if the flush was successful, false otherwise

uint32_t write(const uint8_t *data, uint32_t length)

Writes data to the output stream.

Parameters
  • data – Pointer to a block of data to write to the stream

  • length – Amount of data to write, in bytes

Returns

Number of bytes actually written. This could be lower than length if the socket is non-blocking.

uint32_t read(uint8_t *data, uint32_t length)

Reads data from the output stream.

Parameters
  • data – Pointer to a buffer where the read data will be stored.

  • length – Amount of data to read, in bytes.

Returns

Number of bytes actually read. This could be lower than length if the stream end is encountered or the socket is non-blocking.

void setBlocking(bool blocking)

Sets blocking mode of the socket. Socket must be connected, otherwise calling this method won’t take any effect.

bool isBlocking() const

Returns whether read/write/flush calls to the socket are blocking.

Returns

True if the socket is blocking.

inline void *operator new(size_t, void *address)
template<typename Alloc>
inline void *operator new(size_t size, Alloc alloc, const char *fileName, int line)
template<typename Alloc>
inline void *operator new(size_t size, size_t, Alloc alloc, const char *fileName, int line)
inline void operator delete(void*, void*)
template<typename Alloc>
inline void operator delete(void *ptr, Alloc alloc, const char *fileName, int line)
inline void operator delete(void *ptr)
template<typename Alloc>
inline void *operator new[](size_t size, Alloc alloc, const char *fileName, int line)
template<typename Alloc>
inline void *operator new[](size_t size, size_t, Alloc alloc, const char *fileName, int line)
template<typename Alloc>
inline void operator delete[](void *ptr, Alloc alloc, const char *fileName, int line)
inline void operator delete[](void *ptr)

Public Static Attributes

static const uint32_t DEFAULT_BUFFER_SIZE