PTLib
Version 2.14.3
|
Abstract class defining I/O channel semantics. More...
#include <channel.h>
Classes | |
class | AsyncContext |
class | AsyncContextBase |
Parameters for asynchronous I/O operation. More... | |
Public Types | |
enum | PXBlockType { PXReadBlock, PXWriteBlock, PXAcceptBlock, PXConnectBlock } |
![]() | |
enum | Comparison { LessThan = -1, EqualTo = 0, GreaterThan = 1 } |
Result of the comparison operation performed by the Compare() function. More... | |
Public Member Functions | |
PBoolean | SetErrorValues (Errors errorCode, int osError, ErrorGroup group=LastGeneralError) |
Set error values to those specified. More... | |
Overrides from class PObject | |
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... | |
Information functions | |
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... | |
Reading functions | |
void | SetReadTimeout (const PTimeInterval &time) |
Set the timeout for read operations. More... | |
PTimeInterval | GetReadTimeout () const |
Get the timeout for read operations. More... | |
virtual PBoolean | Read (void *buf, PINDEX len) |
Low level read from the channel. 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... | |
Writing functions | |
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) |
Low level write to the channel. 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... | |
![]() | |
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 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 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 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... | |
Protected Member Functions | |
PChannel (const PChannel &) | |
PChannel & | operator= (const PChannel &) |
virtual PBoolean | ConvertOSError (P_INT_PTR libcReturnValue, ErrorGroup group=LastGeneralError) |
Convert an operating system error into platform independent error. More... | |
int | ReadCharWithTimeout (PTimeInterval &timeout) |
Read a character with specified timeout. More... | |
PBoolean | ReceiveCommandString (int nextChar, const PString &reply, PINDEX &pos, PINDEX start) |
virtual int | os_errno () const |
PBoolean | PXSetIOBlock (PXBlockType type, const PTimeInterval &timeout) |
P_INT_PTR | GetOSHandleAsInt () const |
int | PXClose () |
![]() | |
PObject () | |
Constructor for PObject, made protected so cannot ever create one on its own. More... | |
Protected Attributes | |
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 |
Construction | |
PChannel () | |
Create the channel. More... | |
~PChannel () | |
Close down the channel. More... | |
Error functions | |
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... | |
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... | |
static PString | GetErrorText (Errors lastError, int osError=0) |
Get error message description. More... | |
Asynchronous I/O functions | |
typedef PNotifierTemplate < PChannel::AsyncContext & > | AsyncNotifier |
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... | |
Miscellaneous functions | |
enum | ShutdownValue { ShutdownRead = 0, ShutdownWrite = 1, ShutdownReadAndWrite = 2 } |
virtual PBoolean | Close () |
Close the channel, shutting down the link to the data source. More... | |
virtual PBoolean | Shutdown (ShutdownValue option) |
Close one or both of the data streams associated with a channel. 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... | |
Additional Inherited Members | |
![]() | |
static Comparison | InternalCompareObjectMemoryDirect (const PObject *obj1, const PObject *obj2, PINDEX size) |
Internal function caled from CompareObjectMemoryDirect() More... | |
static const char * | Class () |
Get the name of the class as a C string. More... | |
static __inline const PObject * | PTraceObjectInstance (const char *) |
static __inline const PObject * | PTraceObjectInstance (const PObject *obj) |
![]() |
Abstract class defining I/O channel semantics.
An I/O channel can be a serial port, pipe, network socket or even just a simple file. Anything that requires opening and closing then reading and/or writing from.
A descendent would typically have constructors and an open function which enables access to the I/O channel it represents. The Read()
and Write()
functions would then be overridden to the platform and I/O specific mechanisms required.
The general model for a channel is that the channel accepts and/or supplies a stream of bytes. The access to the stream of bytes is via a set of functions that allow certain types of transfer. These include direct transfers, buffered transfers (via iostream) or asynchronous transfers.
The model also has the fundamental state of the channel being open
or closed
. A channel instance that is closed contains sufficient information to describe the channel but does not allocate or lock any system resources. An open channel allocates or locks the particular system resource. The act of opening a channel is a key event that may fail. In this case the channel remains closed and error values are set.
enum PChannel::ErrorGroup |
Error groups.
To aid in multithreaded applications where reading and writing may be happening simultaneously, read and write errors are separated from other errors.
Enumerator | |
---|---|
LastReadError |
Error during Read() operation. |
LastWriteError |
Error during Write() operation. |
LastGeneralError |
Error during other operation, eg Open() |
NumErrorGroups |
enum PChannel::Errors |
Normalised error codes.
The error result of the last file I/O operation in this object.
|
protected |
Create the channel.
PChannel::~PChannel | ( | ) |
Close down the channel.
|
protected |
void PChannel::AbortCommandString | ( | ) |
Abort a command string that is in progress.
Note that as the SendCommandString() function blocks the calling thread when it runs, this can only be called from within another thread.
|
virtual |
Close the channel, shutting down the link to the data source.
Reimplemented in PRFC822Channel, PSSLChannel, PVXMLChannel, PPOP3Client, PMonitoredSocketChannel, PVXMLSession, PFile, XMPP::Stream, XMPP::Transport, PSMTPClient, PConsoleChannel, PFTPClient, PMemoryFile, PEthSocket, PQueueChannel, PIndirectChannel, PSSDP, XMPP::C2S::TCPTransport, PSoundChannelPortAudio, PVideoChannel, and PSerialChannel.
|
virtual |
Get the relative rank of the two strings.
The system standard function, eg strcmp(), is used.
EqualTo
for same, LessThan
for obj
logically less than the object and GreaterThan
for obj
logically greater than the object. obj | Other PString to compare against. |
Reimplemented from PObject.
Reimplemented in PFile, PIndirectChannel, and PMemoryFile.
|
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.
libcReturnValue | Return value from standard library |
group | Error group to set |
Reimplemented in PSSLChannel, and PSocket.
FILE* PChannel::FDOpen | ( | const char * | mode | ) |
Re-open the device using the stdio library.
This closes the PChannel
|
virtual |
Flow Control information Pass data to the channel for flowControl determination.
Reimplemented in PVideoChannel.
|
virtual |
Get the base channel of channel indirection using PIndirectChannel.
This function returns the eventual base channel for reading of a series of indirect channels provided by descendents of PIndirectChannel
.
The behaviour for this function is to return "this".
Reimplemented in PIndirectChannel.
|
virtual |
Get the base channel of channel indirection using PIndirectChannel.
This function returns the eventual base channel for writing of a series of indirect channels provided by descendents of PIndirectChannel
.
The behaviour for this function is to return "this".
Reimplemented in PIndirectChannel.
Errors PChannel::GetErrorCode | ( | ErrorGroup | group = NumErrorGroups | ) | const |
Get normalised error code.
Return the error result of the last file I/O operation in this object.
group | Error group to get |
int PChannel::GetErrorNumber | ( | ErrorGroup | group = NumErrorGroups | ) | const |
Get OS errro code.
Return the operating system error number of the last file I/O operation in this object.
group | Error group to get |
|
virtual |
Get error message description.
Return a string indicating the error message that may be displayed to the user. The error for the last I/O operation in this object is used.
group | Error group to get |
Reimplemented in PSSLChannel, PIndirectChannel, and PSoundChannelPortAudio.
Get error message description.
Return a string indicating the error message that may be displayed to the user. The osError
parameter is used unless zero, in which case the lastError
parameter is used.
lastError | Error code to translate. |
osError | OS error number to translate. |
|
virtual |
Get the integer operating system handle for the channel.
Reimplemented in PIndirectChannel.
PINDEX PChannel::GetLastReadCount | ( | ) | const |
Get the number of bytes read by the last Read() call.
This will be from 0 to the maximum number of bytes as passed to the Read() call.
Note that the number of bytes read may often be less than that asked for. Aside from the most common case of being at end of file, which the applications semantics may regard as an exception, there are some cases where this is normal. For example, if a PTextFile channel on the MSDOS platform is read from, then the translation of CR/LF pairs to
characters will result in the number of bytes returned being less than the size of the buffer supplied.
|
virtual |
Get the number of bytes written by the last Write() call.
Note that the number of bytes written may often be less, or even more, than that asked for. A common case of it being less is where the disk is full. An example of where the bytes written is more is as follows. On a PTextFile
channel on the MSDOS platform, there is translation of
to CR/LF pairs. This will result in the number of bytes returned being more than that requested.
|
inlinevirtual |
Get the platform and I/O channel type name of the channel.
For example, it would return the filename in PFile
type channels.
Reimplemented in PIPSocket, PFile, PConsoleChannel, PSoundChannelPortAudio, PIndirectChannel, and PVideoChannel.
References channelName.
|
inlineprotected |
PTimeInterval PChannel::GetReadTimeout | ( | ) | const |
Get the timeout for read operations.
Note that this function may not be available, or meaningfull, for all channels. In that case it returns the previously set value.
PTimeInterval PChannel::GetWriteTimeout | ( | ) | const |
Get the timeout for write operations to complete.
Note that this function may not be available, or meaningfull, for all channels. In that case it returns the previously set value.
|
virtual |
Calculate a hash value for use in sets and dictionaries.
The hash function for strings will produce a value based on the sum of the first three characters of the string. This is a fairly basic function and make no assumptions about the string contents. A user may descend from PString and override the hash function if they can take advantage of the types of strings being used, eg if all strings start with the letter 'A' followed by 'B or 'C' then the current hash function will not perform very well.
Reimplemented from PObject.
|
virtual |
Determine if the channel is currently open.
This indicates that read and write operations can be executed on the channel. For example, in the PFile
class it returns if the file is currently open.
Reimplemented in PVXMLChannel, PMonitoredSocketChannel, PIndirectChannel, PVideoChannel, and PSoundChannelPortAudio.
|
virtual |
User callback function for when a ReadAsync()
call has completed or timed out.
The default behaviour calls the m_callback field of context.
context | Context for asynchronous operation |
|
virtual |
User callback function for when a WriteAsync() call has completed or timed out.
The default behaviour calls the m_callback field of context.
context | Context for asynchronous operation |
Referenced by P_timeval::P_timeval().
|
protectedvirtual |
Reimplemented in PSocket.
|
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.
buf | Pointer to a block of memory to receive the read bytes. |
len | Maximum number of bytes to read into the buffer. |
Reimplemented in PSSLChannel, PNullChannel, PVXMLChannel, PMonitoredSocketChannel, PDelayChannel, PFile, PIndirectChannel, PVideoChannel, PMemoryFile, PEthSocket, PInternetProtocol, PQueueChannel, PUDPSocket, PTelnetSocket, PSoundChannelPortAudio, PSocket, and PTCPSocket.
|
virtual |
Begin an asynchronous read from channel.
The read timeout is used as in other read operations, in this case calling the OnReadComplete() function.
If the channel is not capable of asynchronous read then returns false.
Note: the life time of the context and the m_buffer within that context must exceed the duration of the asynchronous operation. After OnReadComplete has been called, these objects may be destroyed.
context | Context for asynchronous operation |
PBoolean PChannel::ReadBlock | ( | void * | buf, |
PINDEX | len | ||
) |
Read len bytes into the buffer from the channel.
This function uses Read(), so most remarks pertaining to that function also apply to this one. The only difference being that this function will not return until all of the bytes have been read, or an error occurs.
len
bytes was sucessfull. buf | Pointer to a block of memory to receive the read bytes. |
len | Maximum number of bytes to read into the buffer. |
|
virtual |
Read a single character from the channel.
If one was not available within the read timeout period, or an I/O error occurred, then the function gives with a -1 return value.
Note, normally the character read will be an 8 bit value, but for some derived classes, e.g. PConsoleChannel, this may be a code for special input.
Reimplemented in PIndirectChannel, PConsoleChannel, and PInternetProtocol.
|
protected |
Read a character with specified timeout.
This reads a single character from the channel waiting at most the amount of time specified for it to arrive. The timeout
parameter is adjusted for amount of time it actually took, so it can be used for a multiple character timeout.
PString PChannel::ReadString | ( | PINDEX | len | ) |
Read len
character into a string from the channel.
This function simply uses ReadBlock(), so all remarks pertaining to that function also apply to this one.
len | Length of string data to read. |
|
protected |
Send a command meta-string.
A meta-string is a string of characters that may contain escaped commands. The escape command is the \ as in the C language.
The escape commands are:
\a | alert (ascii value 7) |
\b | backspace (ascii value 8) |
\f | formfeed (ascii value 12) |
\n | newline (ascii value 10) |
\r | return (ascii value 13) |
\t | horizontal tab (ascii value 9) |
\v | vertical tab (ascii value 11) |
\\ | backslash |
\ooo | where ooo is octal number, ascii value ooo |
\xhh | where hh is hex number (ascii value 0xhh) |
\0 | null character (ascii zero) |
\dns | delay for n seconds |
\dnm | delay for n milliseconds |
\s | characters following this, up to a \w command or the end of string, are to be sent to modem |
\wns | characters following this, up to a \s, \d or another \w or the end of the string are expected back from the modem. If the string is not received within n seconds, a failed command is registered. The exception to this is if the command is at the end of the string or the next character in the string is the \s, \d or \w in which case all characters are ignored from the modem until n seconds of no data. |
\wnm | as for above but timeout is in milliseconds. |
command | Command to send to the channel |
PBoolean PChannel::SetBufferSize | ( | PINDEX | newSize | ) |
Set the iostream buffer size for reads and writes.
newSize | New buffer size |
PBoolean PChannel::SetErrorValues | ( | Errors | errorCode, |
int | osError, | ||
ErrorGroup | group = LastGeneralError |
||
) |
Set error values to those specified.
Return true if errorCode is NoError, false otherwise
errorCode | Error code to translate. |
osError | OS error number to translate. |
group | Error group to set |
|
virtual |
Set local echo mode.
For some classes of channel, e.g. PConsoleChannel, data read by this channel is automatically echoed. This disables the function so things like password entry can work.
Default behaviour does nothing and return true if the channel is open.
Reimplemented in PIndirectChannel, PConsoleChannel, and PTelnetSocket.
void PChannel::SetReadTimeout | ( | const PTimeInterval & | time | ) |
Set the timeout for read operations.
This may be zero for immediate return of data through to PMaxTimeInterval
which will wait forever for the read request to be filled.
Note that this function may not be available, or meaningfull, for all channels. In that case it is set but ignored.
time | The new time interval for read operations. |
void PChannel::SetWriteTimeout | ( | const PTimeInterval & | time | ) |
Set the timeout for write operations to complete.
This may be zero for immediate return through to PMaxTimeInterval which will wait forever for the write request to be completed.
Note that this function may not be available, or meaningfull, for all channels. In this case the parameter is et but ignored.
time | The new time interval for write operations. |
|
virtual |
Close one or both of the data streams associated with a channel.
The default behavour is to do nothing and return false.
Reimplemented in PSSLChannel, PIndirectChannel, and PSocket.
|
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.
buf | Pointer to a block of memory to write. |
len | Number of bytes to write. |
Reimplemented in PRFC822Channel, PSSLChannel, PNullChannel, PVXMLChannel, PMonitoredSocketChannel, PDelayChannel, PFile, PIndirectChannel, XMPP::Stream, PMemoryFile, PEthSocket, PVideoChannel, PInternetProtocol, PTCPSocket, PQueueChannel, PTelnetSocket, PUDPSocket, PSoundChannelPortAudio, and PSocket.
Referenced by PTones::Write().
|
inlinevirtual |
Low level write to the channel with marker.
(H323plus Implementation)
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. By default it calls the Write(void *,len) function
The GetErrorCode() function should be consulted after Write() returns false to determine what caused the failure.
buf | Pointer to a block of memory to write. |
len | Number of bytes to write. |
References Write().
Referenced by Write().
|
virtual |
Begin an asynchronous write from channel.
The write timeout is used as in other write operations, in this case calling the OnWriteComplete() function.
If the channel is not capable of asynchronous write then returns false.
Note: the life time of the context and the m_buffer within that context must exceed the duration of the asynchronous operation. After OnWriteComplete has been called, these objects may be destroyed.
context | Context for asynchronous operation |
PBoolean PChannel::WriteChar | ( | int | c | ) |
Write a single character to the channel.
This function simply uses the Write() function so all comments on that function also apply.
Note that this asserts if the value is not in the range 0..255.
Write a string to the channel.
This function simply uses the Write() function so all comments on that function also apply.
str | String to write |
|
protected |
The platform independant error code.
|
protected |
The operating system error number (eg as returned by errno).
|
protected |
Number of byte last read by the Read() function.
|
protected |
Number of byte last written by the Write() function.
|
protected |
The operating system file handle return by standard open() function.
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
Timeout for read operations.
|
protected |
Timeout for write operations.