reader: add sp_capture_reader_copy()

This function allows copying a capture so that we can do
additional reads. This does, however, copy the buffers which
might be more memory than we want for large usage. We can
tweak things as we go to figure out the cursors.
This commit is contained in:
Christian Hergert
2016-09-25 00:23:28 -07:00
parent 99ff8f2a5c
commit b8f465659b
2 changed files with 36 additions and 0 deletions

View File

@ -812,3 +812,38 @@ sp_capture_reader_get_start_time (SpCaptureReader *self)
return self->header.time;
}
/**
* sp_capture_reader_copy:
*
* This function makes a copy of the reader. Since readers use
* positioned reads with pread(), this allows you to have multiple
* readers with the shared file descriptor. This uses dup() to create
* another file descriptor.
*
* Returns: (transfer full): A copy of @self with a new file-descriptor.
*/
SpCaptureReader *
sp_capture_reader_copy (SpCaptureReader *self)
{
SpCaptureReader *copy;
int fd;
g_return_val_if_fail (self != NULL, NULL);
if (-1 == (fd = dup (self->fd)))
return NULL;
copy = g_new0 (SpCaptureReader, 1);
*copy = *self;
copy->ref_count = 1;
copy->filename = g_strdup (self->filename);
copy->fd = fd;
copy->buf = g_malloc (self->bufsz);
memcpy (copy->buf, self->buf, self->bufsz);
return copy;
}

View File

@ -29,6 +29,7 @@ SpCaptureReader *sp_capture_reader_new (const
GError **error);
SpCaptureReader *sp_capture_reader_new_from_fd (int fd,
GError **error);
SpCaptureReader *sp_capture_reader_copy (SpCaptureReader *self);
SpCaptureReader *sp_capture_reader_ref (SpCaptureReader *self);
void sp_capture_reader_unref (SpCaptureReader *self);
const gchar *sp_capture_reader_get_filename (SpCaptureReader *self);