Prev: Native Objects
Native Streams
A special class of native objects are native streams. These are subclasses of
class NativeStream, declared in
NativeStream.h. Once created, instances of native streams are accessed by module
io.
When subclassing NativeStream, you must implement the following abstract virtual functions:
- virtual TBool ReadL(TDes8 &buf, TRequestStatus &status);
The raw read method. If the read occurs at EOF, buf.Length() must be set to zero. Returns ETrue if the call was asynchronous, EFalse if the data was already available.
- virtual TBool WriteL(const TDesC8 &buf, TRequestStatus &status);
The raw write method. All buf.Length() bytes must be written (not necessarily in a single call). Returns ETrue if the call was asynchronous, EFalse if the data was already written.
If the above methods ever return
ETrue, the following method must also be overridden:
- virtual void Cancel();
Cancel an asynchronous I/O operation.
In addition, the class
can override the virtual functions
SeekL(),
Close(),
SizeL(),
AvailL(),
HandleError().
Here is a sample implementation of a native stream subclass reading from and writing to a socket:
class MyStream : public NativeStream {
RSocket *socket; // owned by this class
TSockXfrLength xfrLength;
~MyStream() {
// garbage collecting the stream should also delete the socket
delete socket;
}
public:
static MyStream *NewLC(RSocket *socket) {
MyStream *self = new (ELeave) MyStream;
CleanupStack::PushL(self);
self->socket = socket;
self->ConstructL();
return self;
}
protected: // from NativeStream
TInt Close() {
socket->Close();
return NativeStream::Close();
}
TBool ReadL(TDes8 &buf, TRequestStatus &status) {
socket->RecvOneOrMore(buf, 0, status, xfrLength);
return ETrue; // was asynchronous
}
TBool WriteL(const TDesC8 &buf, TRequestStatus &status) {
socket->Write(buf, status);
return ETrue; // was asynchronous
}
void Cancel() {
NativeStream::Cancel();
socket->CancelAll();
}
};
|
Next: Native Icons© 2004-2010 airbit AG, CH-8008 Zürich
Document AB-M-NMI-869