rcolyer.net
RC Lib  Version 201607061602
Public Member Functions | List of all members
RC::Sock Class Reference

A portable socket interface for reading and writing to an open socket. More...

#include <Net.h>

Public Member Functions

 Sock (SOCKET new_sock=INVALID_SOCKET)
 Encapsulate a new socket, receiving the base socket type of the system (int or SOCKET). More...
 
void Close ()
 Manually close the socket. More...
 
void SetRemote (RStr remote_addr, RStr remote_port)
 For record-keeping, set the remote address and port.
 
RStr GetRemoteAddr () const
 Get the remote address this socket is connected to.
 
RStr GetRemotePort () const
 Get the remote port this socket is connected to.
 
bool DataReady (bool block_until_ready=false) const
 Returns true if data is ready for reading. More...
 
bool CanSend (bool block_until_ready=false) const
 Returns true if the socket is ready to send data. More...
 
FileRW ToFileRW ()
 On unix-based systems, return a FileRW coresponding to this socket. More...
 
SOCKET Raw () const
 Return the raw encapsulated socket.
 
template<class T >
size_t Recv (Data1D< T > buf, bool do_block=true)
 Receive data from the socket into buf, up to the size of buf. More...
 
size_t Recv (RStr &str, bool crop_newline=true, bool do_block=true)
 Receive characters into str until a newline or the end of data. More...
 
template<class T >
size_t Send (Data1D< T > buf, bool do_block=true)
 Sends the contents of buf through the socket. More...
 
size_t Send (RStr str, bool do_block=true)
 Sends the contents of str throught he socket. More...
 

Detailed Description

A portable socket interface for reading and writing to an open socket.

This class shares its data in a reference-counted manner upon assignment or copy construction, and automatically closes the socket as the last instance goes out of scope.

Constructor & Destructor Documentation

RC::Sock::Sock ( SOCKET  new_sock = INVALID_SOCKET)
inline

Encapsulate a new socket, receiving the base socket type of the system (int or SOCKET).

Use Connect or Listener::Accept to initialize this.

Member Function Documentation

bool RC::Sock::CanSend ( bool  block_until_ready = false) const
inline

Returns true if the socket is ready to send data.

If block_until_ready is true this will wait until data can be sent or an error occurs, such as the socket closing or a signal being received.

void RC::Sock::Close ( )
inline

Manually close the socket.

Note this is handled automatically when all copies of the Sock go out of scope.)

bool RC::Sock::DataReady ( bool  block_until_ready = false) const
inline

Returns true if data is ready for reading.

If block_until_ready is true this will wait until data is available or an error occurs, such as the socket closing or a signal being received. (Note that rare kernel level events can cause data to no longer be available after it was reported as available.)

template<class T >
size_t RC::Sock::Recv ( Data1D< T >  buf,
bool  do_block = true 
)
inline

Receive data from the socket into buf, up to the size of buf.

Parameters
bufThe buffer into which data is received.
do_blockIf true, wait until some data is available.
Returns
The number of elements of buf received.
size_t RC::Sock::Recv ( RStr str,
bool  crop_newline = true,
bool  do_block = true 
)
inline

Receive characters into str until a newline or the end of data.

Parameters
strThe string into which a line is received.
crop_newlineIf true, removes "\r\n" and "\n" from the end.
do_blockIf true, keeps reading until newline or the socket closes. If false, reads until newline or no data is available.
Returns
The number of bytes received, including null characters and newlines not added to str.
template<class T >
size_t RC::Sock::Send ( Data1D< T >  buf,
bool  do_block = true 
)
inline

Sends the contents of buf through the socket.

Parameters
bufThe buffer containing the data to send.
do_blockIf true, sends all of buf. If false, sends as much of buf as can be sent.
Returns
The amount of data sent.
size_t RC::Sock::Send ( RStr  str,
bool  do_block = true 
)
inline

Sends the contents of str throught he socket.

Parameters
strThe string containing the data to send.
do_blockIf true, sends all of str. If false, sends as much of str as can be sent.
Returns
The amount of data sent.
FileRW RC::Sock::ToFileRW ( )
inline

On unix-based systems, return a FileRW coresponding to this socket.

Throws ErrorMsgNet if it cannot convert the socket.


The documentation for this class was generated from the following file:
email address
— (c) 2015