10 #include "iwvstream.h"
11 #include "wvtimeutils.h"
12 #include "wvstreamsdebugger.h"
73 virtual void seterr(
int _errnum);
74 void seterr(WvStringParm specialerr)
76 void seterr(WVSTRING_FORMAT_DECL)
80 virtual bool isok()
const;
83 virtual size_t read(
void *buf,
size_t count);
94 virtual size_t read(
WvBuf &outbuf,
size_t count);
109 virtual size_t write(
const void *buf,
size_t count);
118 virtual size_t write(
WvBuf &inbuf,
size_t count = INT_MAX);
130 { max_outbuf_size = size; }
146 virtual size_t uread(
void *buf,
size_t count)
156 virtual size_t uwrite(
const void *buf,
size_t count)
176 char separator =
'\n',
int readahead = 1024)
183 char separator =
'\n',
int readahead = 1024)
185 return getline(time_t(wait_msec), separator, readahead);
190 char separator =
'\n',
int readahead = 1024)
192 return getline(time_t(wait_msec), separator, readahead);
200 char *
getline(
char,
int i = 0);
201 char *
getline(
bool,
int i = 0);
216 int readahead = 1024);
223 int readahead = 1024);
233 { queue_min = count; }
248 outbuf_delayed_flush = is_delayed;
249 want_to_flush = !is_delayed;
259 { is_auto_flush = is_automatic; }
267 virtual bool flush(time_t msec_timeout);
377 {
return _select(msec_timeout,
false,
false,
false,
true); }
416 bool readable,
bool writable,
bool isex =
false)
417 {
return _select(msec_timeout, readable, writable, isex,
false); }
434 void force_select(
bool readable,
bool writable,
bool isexception =
false);
441 bool isexception =
false);
525 void alarm(time_t msec_timeout);
539 {
return write(s.cstr(), s.len()); }
540 size_t print(WvStringParm s)
542 size_t operator() (WvStringParm s)
548 size_t operator() (WVSTRING_FORMAT_DECL)
551 const char *wsname()
const
552 {
return my_wsname; }
553 void set_wsname(WvStringParm wsname)
554 { my_wsname = wsname; }
555 void set_wsname(WVSTRING_FORMAT_DECL)
556 { set_wsname(
WvString(WVSTRING_FORMAT_CALL)); }
558 const char *wstype()
const {
return "WvStream"; }
560 WSID wsid()
const {
return my_wsid; }
561 static IWvStream *find_by_wsid(WSID wsid);
563 virtual WvString getattr(WvStringParm name)
const
564 {
return attrs.get(name); }
569 #ifdef __WVSTREAM_UNIT_TEST
572 {
return outbuf.
used(); }
574 {
return inbuf.
used(); }
575 void inbuf_putstr(WvStringParm t)
580 void setattr(WvStringParm name, WvStringParm value)
581 { attrs.set(name, value); }
587 void _build_selectinfo(SelectInfo &si, time_t msec_timeout,
588 bool readable,
bool writable,
bool isexcept,
594 int _do_select(SelectInfo &si);
598 bool _process_selectinfo(SelectInfo &si,
bool forceable);
604 bool flush_outbuf(time_t msec_timeout);
608 virtual bool flush_internal(time_t msec_timeout);
613 virtual int getrfd()
const;
614 virtual int getwfd()
const;
618 friend class WvHTTPClientProxyStream;
622 IWvStreamCallback callfunc;
623 wv::function<
void*(
void*)> call_ctx;
625 IWvStreamCallback readcb, writecb, exceptcb, closecb;
627 size_t max_outbuf_size;
628 bool outbuf_delayed_flush;
638 time_t autoclose_time;
658 static void debugger_streams_display_header(WvStringParm cmd,
659 WvStreamsDebugger::ResultCallback result_cb);
660 static void debugger_streams_display_one_stream(
WvStream *s,
662 WvStreamsDebugger::ResultCallback result_cb);
663 static void debugger_streams_maybe_display_one_stream(
WvStream *s,
666 WvStreamsDebugger::ResultCallback result_cb);
670 bool _select(time_t msec_timeout,
671 bool readable,
bool writable,
bool isexcept,
674 void legacy_callback();
679 static void add_debugger_commands();
681 static WvString debugger_streams_run_cb(WvStringParm cmd,
683 WvStreamsDebugger::ResultCallback result_cb,
void *);
684 static WvString debugger_close_run_cb(WvStringParm cmd,
686 WvStreamsDebugger::ResultCallback result_cb,
void *);
700 #endif // __WVSTREAM_H
void delay_output(bool is_delayed)
force write() to always buffer output.
WvStream * write_requires_readable
If this is set, select() doesn't return true for write unless the given stream also returns true for ...
virtual void maybe_autoclose()
Auto-close the stream if the time is right.
void pre_select(SelectInfo &si, const SelectRequest &r)
A more convenient version of pre_select() usable for overriding the 'want' value temporarily.
time_t alarm_remaining()
return the number of milliseconds remaining before the alarm will go off; -1 means no alarm is set (i...
void runonce(time_t msec_timeout=-1)
Exactly the same as: if (select(timeout)) callback();.
virtual size_t write(const void *buf, size_t count)
Write data to the stream.
char * blocking_getline(time_t wait_msec, int separator='\n', int readahead=1024)
This is a version of getline() that allows you to block for more data to arrive.
void auto_flush(bool is_automatic)
if true, force write() to call flush() each time, the default behavour.
void flush_then_close(int msec_timeout)
flush the output buffer automatically as select() is called.
virtual void execute()
The callback() function calls execute(), and then calls the user- specified callback if one is define...
WvStream * read_requires_writable
If this is set, select() doesn't return true for read unless the given stream also returns true for w...
virtual size_t uread(void *buf, size_t count)
unbuffered I/O functions; these ignore the buffer, which is handled by read().
virtual bool flush(time_t msec_timeout)
flush the output buffer, if we can do it without delaying more than msec_timeout milliseconds at a ti...
bool select(time_t msec_timeout)
Return true if any of the requested features are true on the stream.
void putstr(WvStringParm str)
Copies a WvString into the buffer, excluding the null-terminator.
bool alarm_was_ticking
This will be true during callback execution if the callback was triggered by the alarm going off.
Based on (and interchangeable with) struct timeval.
virtual void callback()
if the stream has a callback function defined, call it now.
void queuemin(size_t count)
force read() to not return any bytes unless 'count' bytes can be read at once.
virtual void seterr(int _errnum)
Set the errnum variable – we have an error.
IWvStreamCallback setexceptcallback(IWvStreamCallback _callback)
Sets a callback to be invoked when the stream is in exception state.
IWvStreamCallback setreadcallback(IWvStreamCallback _callback)
Sets a callback to be invoked when the stream is readable.
virtual bool iswritable()
Returns true if the stream is writable (without using the outbuf).
WvStream()
Basic constructor for just a do-nothing WvStream.
size_t personal_stack_size
Specifies the stack size to reserve for continue_select().
virtual void close()
Close the stream if it is open; isok() becomes false from now on.
virtual void seterr(int _errnum)
Override seterr() from WvError so that it auto-closes the stream.
bool uses_continue_select
If this is set, enables the use of continue_select().
WvString is an implementation of a simple and efficient printable-string class.
void force_select(bool readable, bool writable, bool isexception=false)
Use force_select() to force one or more particular modes (readable, writable, or isexception) to true...
virtual bool isreadable()
Returns true if the stream is readable.
bool post_select(SelectInfo &si, const SelectRequest &r)
A more convenient version of post_select() usable for overriding the 'want' value temporarily.
the data structure used by pre_select()/post_select() and internally by select().
char * getline(time_t wait_msec=0, char separator='\n', int readahead=1024)
Read up to one line of data from the stream and return a pointer to the internal buffer containing th...
char * continue_getline(time_t wait_msec, int separator='\n', int readahead=1024)
This is a version of blocking_getline() that uses continue_select to avoid blocking other streams.
void terminate_continue_select()
you MUST run this from your destructor if you use continue_select(), or very weird things will happen...
void * _callwrap(void *)
A wrapper that's compatible with WvCont, but calls the "real" callback.
Base class for different address types, each of which will have the ability to convert itself to/from...
Specialization of WvBufBase for unsigned char type buffers intended for use with raw memory buffers.
void _callback()
Actually call the registered callfunc and execute().
void xpre_select(SelectInfo &si, const SelectRequest &r)
Like pre_select(), but still exists even if you override the other pre_select() in a subclass.
virtual bool post_select(SelectInfo &si)
post_select() is called after ::select(), and returns true if this object is now ready.
virtual size_t read(void *buf, size_t count)
read a data block on the stream.
void autoforward(WvStream &s)
set the callback function for this stream to an internal routine that auto-forwards all incoming stre...
virtual void unread(WvBuf &outbuf, size_t count)
Puts data back into the stream's internal buffer.
void alarm(time_t msec_timeout)
set an alarm, ie.
virtual bool isok() const
return true if the stream is actually usable right now
Unified support for streams, that is, sequences of bytes that may or may not be ready for read/write ...
char * getline(double wait_msec, char separator='\n', int readahead=1024)
Auto-convert double to time_t.
virtual bool should_flush()
Returns true if we want to flush the output buffer right now.
A SelectRequest is a convenient way to remember what we want to do to a particular stream: read from ...
void setcallback(IWvStreamCallback _callfunc)
define the callback function for this stream, called whenever the callback() member is run,...
void noautoforward()
Stops autoforwarding.
IWvStream::SelectRequest get_select_request()
Use get_select_request() to save the current state of the selection state of this stream.
virtual void nowrite()
Shuts down the writing side of the stream.
char * getline(int wait_msec, char separator='\n', int readahead=1024)
Auto-convert int to time_t.
size_t used() const
Returns the number of elements in the buffer currently available for reading.
IWvStreamCallback setclosecallback(IWvStreamCallback _callback)
Sets a callback to be invoked on close().
void undo_force_select(bool readable, bool writable, bool isexception=false)
Undo a previous force_select() - ie.
IWvStreamCallback setwritecallback(IWvStreamCallback _callback)
Sets a callback to be invoked when the stream is writable.
bool continue_select(time_t msec_timeout)
return to the caller from execute(), but don't really return exactly; this uses WvCont::yield() to re...
virtual size_t uwrite(const void *buf, size_t count)
unbuffered I/O functions; these ignore the buffer, which is handled by write().
bool xpost_select(SelectInfo &si, const SelectRequest &r)
Like post_select(), but still exists even if you override the other post_select() in a subclass.
size_t print(WVSTRING_FORMAT_DECL)
preformat and write() a string.
virtual const WvAddr * src() const
get the remote address from which the last data block was received.
This is a WvList of WvStrings, and is a really handy way to parse strings.
bool select(time_t msec_timeout, bool readable, bool writable, bool isex=false)
This version of select() sets forceable==false, so we use the exact readable/writable/isexception opt...
void outbuf_limit(size_t size)
set the maximum size of outbuf, beyond which a call to write() will return 0.
size_t write(WvStringParm s)
print a preformatted WvString to the stream.
virtual void pre_select(SelectInfo &si)
pre_select() sets up for eventually calling ::select().
virtual void noread()
Shuts down the reading side of the stream.
bool stop_read
True if noread()/nowrite()/close() have been called, respectively.
void drain()
drain the input buffer (read and discard data until select(0) returns false)