PTLib
Version 2.14.3
|
A network communications channel. More...
#include <socket.h>
Classes | |
class | SelectList |
List of sockets used for Select() function. More... | |
struct | Slice |
Structure that defines a "slice" of memory to be written to. More... | |
Public Member Functions | |
virtual bool | Read (void *ptr, PINDEX len) |
Low level read from the channel. More... | |
virtual bool | Write (const void *buf, PINDEX len) |
Low level write to the channel. More... | |
~PSocket () | |
Socket options functions | |
PBoolean | SetOption (int option, int value, int level=SOL_SOCKET) |
Set options on the socket. More... | |
PBoolean | SetOption (int option, const void *valuePtr, PINDEX valueSize, int level=SOL_SOCKET) |
Set options on the socket. More... | |
PBoolean | GetOption (int option, int &value, int level=SOL_SOCKET) |
Get options on the socket. More... | |
PBoolean | GetOption (int option, void *valuePtr, PINDEX valueSize, int level=SOL_SOCKET) |
Get options on the socket. More... | |
Scattered read/write functions | |
virtual bool | Read (Slice *slices, size_t sliceCount) |
Low level scattered read from the channel. More... | |
virtual bool | Write (const Slice *slices, size_t sliceCount) |
Low level scattered write to the channel. More... | |
![]() | |
PBoolean | SetErrorValues (Errors errorCode, int osError, ErrorGroup group=LastGeneralError) |
Set error values to those specified. More... | |
virtual Comparison | Compare (const PObject &obj) const |
Get the relative rank of the two strings. More... | |
virtual PINDEX | HashFunction () const |
Calculate a hash value for use in sets and dictionaries. More... | |
virtual PBoolean | IsOpen () const |
Determine if the channel is currently open. More... | |
virtual PString | GetName () const |
Get the platform and I/O channel type name of the channel. More... | |
virtual P_INT_PTR | GetHandle () const |
Get the integer operating system handle for the channel. More... | |
FILE * | FDOpen (const char *mode) |
Re-open the device using the stdio library. More... | |
virtual PChannel * | GetBaseReadChannel () const |
Get the base channel of channel indirection using PIndirectChannel. More... | |
virtual PChannel * | GetBaseWriteChannel () const |
Get the base channel of channel indirection using PIndirectChannel. More... | |
void | SetReadTimeout (const PTimeInterval &time) |
Set the timeout for read operations. More... | |
PTimeInterval | GetReadTimeout () const |
Get the timeout for read operations. More... | |
PINDEX | GetLastReadCount () const |
Get the number of bytes read by the last Read() call. More... | |
virtual int | ReadChar () |
Read a single character from the channel. More... | |
PBoolean | ReadBlock (void *buf, PINDEX len) |
Read len bytes into the buffer from the channel. More... | |
PString | ReadString (PINDEX len) |
Read len character into a string from the channel. More... | |
void | SetWriteTimeout (const PTimeInterval &time) |
Set the timeout for write operations to complete. More... | |
PTimeInterval | GetWriteTimeout () const |
Get the timeout for write operations to complete. More... | |
virtual PBoolean | Write (const void *buf, PINDEX len, const void *) |
Low level write to the channel with marker. More... | |
virtual PINDEX | GetLastWriteCount () const |
Get the number of bytes written by the last Write() call. More... | |
PBoolean | WriteChar (int c) |
Write a single character to the channel. More... | |
PBoolean | WriteString (const PString &str) |
Write a string to the channel. More... | |
~PChannel () | |
Close down the channel. More... | |
Errors | GetErrorCode (ErrorGroup group=NumErrorGroups) const |
Get normalised error code. More... | |
int | GetErrorNumber (ErrorGroup group=NumErrorGroups) const |
Get OS errro code. More... | |
virtual PString | GetErrorText (ErrorGroup group=NumErrorGroups) const |
Get error message description. More... | |
virtual bool | ReadAsync (AsyncContext &context) |
Begin an asynchronous read from channel. More... | |
virtual void | OnReadComplete (AsyncContext &context) |
User callback function for when a ReadAsync() call has completed or timed out. More... | |
virtual bool | WriteAsync (AsyncContext &context) |
Begin an asynchronous write from channel. More... | |
virtual void | OnWriteComplete (AsyncContext &context) |
User callback function for when a WriteAsync() call has completed or timed out. More... | |
virtual PBoolean | Close () |
Close the channel, shutting down the link to the data source. More... | |
virtual bool | SetLocalEcho (bool localEcho) |
Set local echo mode. More... | |
virtual bool | FlowControl (const void *flowData) |
Flow Control information Pass data to the channel for flowControl determination. More... | |
PBoolean | SetBufferSize (PINDEX newSize) |
Set the iostream buffer size for reads and writes. More... | |
PBoolean | SendCommandString (const PString &command) |
Send a command meta-string. More... | |
void | AbortCommandString () |
Abort a command string that is in progress. More... | |
![]() | |
unsigned | GetTraceContextIdentifier () const |
Get PTRACE context identifier. More... | |
void | SetTraceContextIdentifier (unsigned id) |
void | GetTraceContextIdentifier (PObject &obj) |
void | GetTraceContextIdentifier (PObject *obj) |
void | SetTraceContextIdentifier (const PObject &obj) |
void | SetTraceContextIdentifier (const PObject *obj) |
virtual | ~PObject () |
virtual PObject * | Clone () const |
Create a copy of the class on the heap. More... | |
template<class CLS > | |
CLS * | CloneAs () const |
As for Clone() but converts to specified type. More... | |
virtual const char * | GetClass (unsigned ancestor=0) const |
Get the current dynamic type of the object instance. More... | |
PBoolean | IsClass (const char *cls) const |
virtual PBoolean | InternalIsDescendant (const char *clsName) const |
Determine if the dynamic type of the current instance is a descendent of the specified class. More... | |
__inline const PObject * | PTraceObjectInstance () const |
virtual Comparison | CompareObjectMemoryDirect (const PObject &obj) const |
Determine the byte wise comparison of two objects. More... | |
bool | operator== (const PObject &obj) const |
Compare the two objects. More... | |
bool | operator!= (const PObject &obj) const |
Compare the two objects. More... | |
bool | operator< (const PObject &obj) const |
Compare the two objects. More... | |
bool | operator> (const PObject &obj) const |
Compare the two objects. More... | |
bool | operator<= (const PObject &obj) const |
Compare the two objects. More... | |
bool | operator>= (const PObject &obj) const |
Compare the two objects. More... | |
virtual void | PrintOn (ostream &strm) const |
Output the contents of the object to the stream. More... | |
virtual void | ReadFrom (istream &strm) |
Input the contents of the object from the stream. More... | |
Static Public Member Functions | |
Multiple socket selection functions | |
static int | Select (PSocket &sock1, PSocket &sock2) |
Select a socket with available data. More... | |
static int | Select (PSocket &sock1, PSocket &sock2, const PTimeInterval &timeout) |
Select a socket with available data. More... | |
static Errors | Select (SelectList &read) |
Select a socket with available data. More... | |
static Errors | Select (SelectList &read, const PTimeInterval &timeout) |
Select a socket with available data. More... | |
static Errors | Select (SelectList &read, SelectList &write) |
Select a socket with available data. More... | |
static Errors | Select (SelectList &read, SelectList &write, const PTimeInterval &timeout) |
Select a socket with available data. More... | |
static Errors | Select (SelectList &read, SelectList &write, SelectList &except) |
Select a socket with available data. More... | |
static Errors | Select (SelectList &read, SelectList &write, SelectList &except, const PTimeInterval &timeout) |
Select a socket with available data. More... | |
Integer conversion functions | |
static WORD | Host2Net (WORD v) |
Convert from host to network byte order. More... | |
static DWORD | Host2Net (DWORD v) |
Convert from host to network byte order. More... | |
static WORD | Net2Host (WORD v) |
Convert from network to host byte order. More... | |
static DWORD | Net2Host (DWORD v) |
Convert from network to host byte order. More... | |
![]() | |
static PString | GetErrorText (Errors lastError, int osError=0) |
Get error message description. More... | |
Protected Member Functions | |
PSocket () | |
virtual PBoolean | ConvertOSError (P_INT_PTR libcReturnValue, ErrorGroup group=LastGeneralError) |
Convert an operating system error into platform independent error. More... | |
virtual PBoolean | OpenSocket ()=0 |
virtual const char * | GetProtocolName () const =0 |
This function returns the protocol name for the socket type. More... | |
int | os_close () |
int | os_socket (int af, int type, int proto) |
PBoolean | os_connect (struct sockaddr *sin, socklen_t size) |
PBoolean | os_vread (Slice *slices, size_t sliceCount, int flags, struct sockaddr *from, socklen_t *fromlen) |
PBoolean | os_vwrite (const Slice *slices, size_t sliceCount, int flags, struct sockaddr *to, socklen_t tolen) |
PBoolean | os_accept (PSocket &listener, struct sockaddr *addr, socklen_t *size) |
virtual int | os_errno () const |
![]() | |
PChannel (const PChannel &) | |
PChannel & | operator= (const PChannel &) |
int | ReadCharWithTimeout (PTimeInterval &timeout) |
Read a character with specified timeout. More... | |
PBoolean | ReceiveCommandString (int nextChar, const PString &reply, PINDEX &pos, PINDEX start) |
PBoolean | PXSetIOBlock (PXBlockType type, const PTimeInterval &timeout) |
P_INT_PTR | GetOSHandleAsInt () const |
int | PXClose () |
PChannel () | |
Create the channel. More... | |
![]() | |
PObject () | |
Constructor for PObject, made protected so cannot ever create one on its own. More... | |
Protected Attributes | |
WORD | port |
Port to be used by the socket when opening the channel. More... | |
![]() | |
P_INT_PTR | os_handle |
The operating system file handle return by standard open() function. More... | |
Errors | lastErrorCode [NumErrorGroups+1] |
The platform independant error code. More... | |
int | lastErrorNumber [NumErrorGroups+1] |
The operating system error number (eg as returned by errno). More... | |
PINDEX | lastReadCount |
Number of byte last read by the Read() function. More... | |
PINDEX | lastWriteCount |
Number of byte last written by the Write() function. More... | |
PTimeInterval | readTimeout |
Timeout for read operations. More... | |
PTimeInterval | writeTimeout |
Timeout for write operations. More... | |
PString | channelName |
Name of channel. More... | |
PMutex | px_threadMutex |
PXBlockType | px_lastBlockType |
PThread * | px_readThread |
PThread * | px_writeThread |
PMutex | px_writeMutex |
PThread * | px_selectThread [3] |
PMutex | px_selectMutex [3] |
![]() | |
unsigned | m_traceContextIdentifier |
Socket establishment functions | |
enum | Reusability { CanReuseAddress, AddressIsExclusive } |
Flags to reuse of port numbers in Listen() function. More... | |
virtual PBoolean | Connect (const PString &address) |
Connect a socket to a remote host on the specified port number. More... | |
virtual PBoolean | Listen (unsigned queueSize=5, WORD port=0, Reusability reuse=AddressIsExclusive) |
Listen on a socket for a remote host on the specified port number. More... | |
virtual PBoolean | Accept (PSocket &socket) |
Open a socket to a remote host on the specified port number. More... | |
virtual PBoolean | Shutdown (ShutdownValue option) |
Close one or both of the data streams associated with a socket. More... | |
Port/Service database functions | |
virtual WORD | GetPortByService (const PString &service) const |
Get the port number for the specified service name. More... | |
virtual PString | GetServiceByPort (WORD port) const |
Get the service name from the port number. More... | |
void | SetPort (WORD port) |
Set the port number for the channel. More... | |
void | SetPort (const PString &service) |
Set the port number for the channel. More... | |
WORD | GetPort () const |
Get the port the TCP socket channel object instance is using. More... | |
PString | GetService () const |
Get a service name for the port number the TCP socket channel object instance is using. More... | |
static WORD | GetProtocolByName (const PString &name) |
Get the number of the protocol associated with the specified name. More... | |
static PString | GetNameByProtocol (WORD proto) |
Get the name of the protocol number specified. More... | |
static WORD | GetPortByService (const char *protocol, const PString &service) |
Get the port number for the specified service name. More... | |
static PString | GetServiceByPort (const char *protocol, WORD port) |
Get the service name from the port number. More... | |
Additional Inherited Members | |
![]() | |
enum | PXBlockType { PXReadBlock, PXWriteBlock, PXAcceptBlock, PXConnectBlock } |
enum | Errors { NoError, NotFound, FileExists, DiskFull, AccessDenied, DeviceInUse, BadParameter, NoMemory, NotOpen, Timeout, Interrupted, BufferTooSmall, Miscellaneous, ProtocolFailure, Unavailable, NumNormalisedErrors } |
Normalised error codes. More... | |
enum | ErrorGroup { LastReadError, LastWriteError, LastGeneralError, NumErrorGroups } |
Error groups. More... | |
typedef PNotifierTemplate < PChannel::AsyncContext & > | AsyncNotifier |
enum | ShutdownValue { ShutdownRead = 0, ShutdownWrite = 1, ShutdownReadAndWrite = 2 } |
![]() |
A network communications channel.
This is based on the concepts in the Berkley Sockets library.
A socket represents a bidirectional communications channel to a port at a remote host.
enum PSocket::Reusability |
Flags to reuse of port numbers in Listen() function.
Enumerator | |
---|---|
CanReuseAddress | |
AddressIsExclusive |
|
protected |
PSocket::~PSocket | ( | ) |
Open a socket to a remote host on the specified port number.
This is an "accepting" socket. When a "listening" socket has a pending connection to make, this will accept a connection made by the "connecting" socket created to establish a link.
The port that the socket uses is the one used in the Listen() command of the socket
parameter. Note an error occurs if the socket
parameter has not had the Listen() function called on it.
Note that this function will block until a remote system connects to the port number specified in the "listening" socket. The time that the function will block is determined by the read timeout of the socket
parameter. This will normally be PMaxTimeInterval which indicates an infinite time.
The default behaviour is to assert.
socket | Listening socket making the connection. |
Reimplemented in PSocksSocket, PTelnetSocket, and PTCPSocket.
Connect a socket to a remote host on the specified port number.
This is typically used by the client or initiator of a communications channel. This connects to a "listening" socket at the other end of the communications channel.
Use the SetReadTimeout() function to set a maximum time for the Connect().
address | Address of remote machine to connect to. |
Reimplemented in PIPSocket, PSocksUDPSocket, PEthSocket, PSocksSocket, PTelnetSocket, and PUDPSocket.
|
protectedvirtual |
Convert an operating system error into platform independent error.
The internal error codes are set by this function. They may be obtained via the GetErrorCode()
and GetErrorNumber()
functions.
Reimplemented from PChannel.
|
static |
Get the name of the protocol number specified.
proto | Number of protocol. |
PBoolean PSocket::GetOption | ( | int | option, |
int & | value, | ||
int | level = SOL_SOCKET |
||
) |
Get options on the socket.
These options are defined as Berkeley socket options of the class SOL_SOCKET.
option | Option to get. |
value | Integer to receive value. |
level | Level for option. |
PBoolean PSocket::GetOption | ( | int | option, |
void * | valuePtr, | ||
PINDEX | valueSize, | ||
int | level = SOL_SOCKET |
||
) |
Get options on the socket.
These options are defined as Berkeley socket options of the class SOL_SOCKET.
option | Option to get. |
valuePtr | Pointer to buffer for value. |
valueSize | Size of buffer to receive value. |
level | Level for option |
WORD PSocket::GetPort | ( | ) | const |
Get the port the TCP socket channel object instance is using.
|
virtual |
Get the port number for the specified service name.
service | Name of service to get port number for. |
|
static |
Get the port number for the specified service name.
A name is a unique string contained in a system database. The parameter here may be either this unique name, an integer value or both separated by a space (name then integer). In the latter case the integer value is used if the name cannot be found in the database.
The exact behviour of this function is dependent on whether TCP or UDP transport is being used. The PTCPSocket and PUDPSocket classes will implement this function.
The static version of the function is independent of the socket type as its first parameter may be "tcp" or "udp".
protocol | Protocol type for port lookup. |
service | Name of service to get port number for. |
|
static |
Get the number of the protocol associated with the specified name.
name | Name of protocol. |
|
protectedpure virtual |
This function returns the protocol name for the socket type.
Implemented in PEthSocket, PTCPSocket, PUDPSocket, and PICMPSocket.
PString PSocket::GetService | ( | ) | const |
Get a service name for the port number the TCP socket channel object instance is using.
|
virtual |
Get the service name from the port number.
port | Number for service to find name of. |
|
static |
Get the service name from the port number.
A service name is a unique string contained in a system database. The parameter here may be either this unique name, an integer value or both separated by a space (name then integer). In the latter case the integer value is used if the name cannot be found in the database.
The exact behviour of this function is dependent on whether TCP or UDP transport is being used. The PTCPSocket and PUDPSocket classes will implement this function.
The static version of the function is independent of the socket type as its first parameter may be "tcp" or "udp".
protocol | Protocol type for port lookup |
port | Number for service to find name of. |
|
inlinestatic |
Convert from host to network byte order.
|
inlinestatic |
Convert from host to network byte order.
|
virtual |
Listen on a socket for a remote host on the specified port number.
This may be used for server based applications. A "connecting" socket begins a connection by initiating a connection to this socket. An active socket of this type is then used to generate other "accepting" sockets which establish a two way communications channel with the "connecting" socket.
If the port
parameter is zero then the port number as defined by the object instance construction or the descendent classes SetPort() or SetService() function.
queueSize | Number of pending accepts that may be queued. |
port | Port number to use for the connection. |
reuse | Can/Cant listen more than once. |
Reimplemented in PIPSocket, PSocksUDPSocket, PEthSocket, and PSocksSocket.
|
inlinestatic |
Convert from network to host byte order.
|
inlinestatic |
Convert from network to host byte order.
|
protectedpure virtual |
Implemented in PEthSocket, PTCPSocket, PUDPSocket, and PICMPSocket.
|
protected |
|
protected |
|
protected |
|
protectedvirtual |
Reimplemented from PChannel.
|
protected |
|
protected |
|
protected |
|
virtual |
Low level read from the channel.
This function may block until the requested number of characters were read or the read timeout was reached. The GetLastReadCount() function returns the actual number of bytes read.
The GetErrorCode() function should be consulted after Read() returns false to determine what caused the failure.
Reimplemented from PChannel.
Reimplemented in PEthSocket, PUDPSocket, PTelnetSocket, and PTCPSocket.
|
virtual |
Low level scattered read from the channel.
This is identical to Read except that the data will be read into a series of scattered memory slices. By default, this call will default to calling Read multiple times, but this may be implemented by operating systems to do a real scattered read
Select a socket with available data.
sock1 | First socket to check for readability. |
sock2 | Second socket to check for readability. |
|
static |
Select a socket with available data.
sock1 | First socket to check for readability. |
sock2 | Second socket to check for readability. |
timeout | Timeout for wait on read/write data. |
|
static |
Select a socket with available data.
read | List of sockets to check for readability. |
|
static |
Select a socket with available data.
read | List of sockets to check for readability. |
timeout | Timeout for wait on read/write data. |
|
static |
Select a socket with available data.
read | List of sockets to check for readability. |
write | List of sockets to check for writability. |
|
static |
Select a socket with available data.
read | List of sockets to check for readability. |
write | List of sockets to check for writability. |
timeout | Timeout for wait on read/write data. |
|
static |
Select a socket with available data.
read | List of sockets to check for readability. |
write | List of sockets to check for writability. |
except | List of sockets to check for exceptions. |
|
static |
Select a socket with available data.
This function will block until the timeout or data is available to be read or written to the specified sockets.
The read, write and except lists are modified by the call so that only the sockets that have data available are present. If the call timed out then all of these lists will be empty.
If no timeout is specified then the call will block until a socket has data available.
For the versions taking sockets directly instead of lists the integer returned is >0 for an error being a value from the PChannel::Errors enum, 0 for a timeout, -1 for the first socket having read data, -2 for the second socket and -3 for both.
read | List of sockets to check for readability. |
write | List of sockets to check for writability. |
except | List of sockets to check for exceptions. |
timeout | Timeout for wait on read/write data. |
PBoolean PSocket::SetOption | ( | int | option, |
int | value, | ||
int | level = SOL_SOCKET |
||
) |
Set options on the socket.
These options are defined as Berkeley socket options of the class SOL_SOCKET.
option | Option to set. |
value | New value for option. |
level | Level for option. |
PBoolean PSocket::SetOption | ( | int | option, |
const void * | valuePtr, | ||
PINDEX | valueSize, | ||
int | level = SOL_SOCKET |
||
) |
Set options on the socket.
These options are defined as Berkeley socket options of the class SOL_SOCKET.
option | Option to set. |
valuePtr | Pointer to new value for option. |
valueSize | Size of new value. |
level | Level for option. |
void PSocket::SetPort | ( | WORD | port | ) |
Set the port number for the channel.
port | New port number for the channel. |
void PSocket::SetPort | ( | const PString & | service | ) |
Set the port number for the channel.
This a 16 bit number representing an agreed high level protocol type. The string version looks up a database of names to find the number for the string name.
A service name is a unique string contained in a system database. The parameter here may be either this unique name, an integer value or both separated by a space (name then integer). In the latter case the integer value is used if the name cannot be found in the database.
The port number may not be changed while the port is open and the function will assert if an attempt is made to do so.
service | Service name to describe the port number. |
|
virtual |
Close one or both of the data streams associated with a socket.
option | Flag for shutdown of read, write or both. |
Reimplemented from PChannel.
|
virtual |
Low level write to the channel.
This function will block until the requested number of characters are written or the write timeout is reached. The GetLastWriteCount() function returns the actual number of bytes written.
The GetErrorCode() function should be consulted after Write() returns false to determine what caused the failure.
Reimplemented from PChannel.
Reimplemented in PEthSocket, PTCPSocket, PTelnetSocket, and PUDPSocket.
|
virtual |
Low level scattered write to the channel.
This is identical to Write except that the data will be written from a series of scattered memory slices. By default, this call will default to calling Write multiple times, but this can be actually implemented by operating systems to do a real scattered write
|
protected |
Port to be used by the socket when opening the channel.
Referenced by PIPSocket::Listen(), and PIPSocket::AddressAndPort::SetPort().