efl_io_queue: add discard method, useful in real life.

While using efl_io_queue + slice_get I found that we need to discard
data and the only way was to read to an actual buffer, kinda annoying.

Then introduce a discard method to do that.
This commit is contained in:
Gustavo Sverzut Barbieri 2016-11-24 18:44:44 -02:00
parent 1e62cd562e
commit d0b7d876a7
2 changed files with 53 additions and 1 deletions

View File

@ -297,6 +297,30 @@ _efl_io_queue_efl_io_reader_read(Eo *o, Efl_Io_Queue_Data *pd, Eina_Rw_Slice *rw
return EINVAL;
}
EOLIAN static void
_efl_io_queue_discard(Eo *o, Efl_Io_Queue_Data *pd, size_t amount)
{
size_t available;
EINA_SAFETY_ON_TRUE_RETURN(efl_io_closer_closed_get(o));
available = pd->position_write - pd->position_read;
if (amount > available)
{
amount = available;
if (amount == 0)
return;
}
pd->position_read += amount;
efl_io_reader_can_read_set(o, pd->position_read < pd->position_write);
efl_event_callback_call(o, EFL_IO_QUEUE_EVENT_SLICE_CHANGED, NULL);
if ((pd->pending_eos) && (efl_io_queue_usage_get(o) == 0))
efl_io_reader_eos_set(o, EINA_TRUE);
}
EOLIAN static Eina_Bool
_efl_io_queue_efl_io_reader_can_read_get(Eo *o EINA_UNUSED, Efl_Io_Queue_Data *pd)
{

View File

@ -58,8 +58,36 @@ class Efl.Io.Queue (Efl.Object, Efl.Io.Reader, Efl.Io.Writer, Efl.Io.Closer) {
return: bool (false); [[$true on success, $false otherwise]]
}
discard {
[[Discard the given number of bytes.
This has the same effect as reading and discarding the
given amount of bytes, without executing the actual
copy.
It's often paired with @.slice_get, if users read the
information from the slice and once they're done, that
data must be discarded.
As an example, some protocols provide messages with a
"size" header, then @.slice_get is used to peek into the
available memory to see if there is a "size" and if the
rest of the slice is the full payload, in this case the
slice may be handled to some processing function. When
the function is done, that amount of data must be
discarded -- with this function.
]]
params {
amount: size; [[Bytes to discard]]
}
}
clear {
[[Clear the queue. Same as reading all data]]
[[Clear the queue. Same as reading all data.
This is equivalent as calling @.discard with @.usage
amount of bytes.
]]
}
eos_mark {