1/******************************************************************************
2 *
3 *  Copyright (C) 2014 Google, Inc.
4 *
5 *  Licensed under the Apache License, Version 2.0 (the "License");
6 *  you may not use this file except in compliance with the License.
7 *  You may obtain a copy of the License at:
8 *
9 *  http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 *
17 ******************************************************************************/
18
19#pragma once
20
21#include <stdbool.h>
22#include <stddef.h>
23#include <stdint.h>
24
25#include "allocator.h"
26#include "osi/include/thread.h"
27
28typedef struct eager_reader_t eager_reader_t;
29typedef struct reactor_t reactor_t;
30
31typedef void (*eager_reader_cb)(eager_reader_t *reader, void *context);
32
33// Creates a new eager reader object, which pulls data from |fd_to_read| into
34// buffers of size |buffer_size| allocated using |allocator|, and has an
35// internal read thread named |thread_name|. The returned object must be freed using
36// |eager_reader_free|. |fd_to_read| must be valid, |buffer_size| and |max_buffer_count|
37// must be greater than zero. |allocator| and |thread_name| may not be NULL.
38eager_reader_t *eager_reader_new(
39  int fd_to_read,
40  const allocator_t *allocator,
41  size_t buffer_size,
42  size_t max_buffer_count,
43  const char *thread_name
44);
45
46// Frees an eager reader object, and associated internal resources.
47// |reader| may be NULL.
48void eager_reader_free(eager_reader_t *reader);
49
50// Registers |reader| with the |reactor|. When the reader has data
51// |read_cb| will be called. The |context| parameter is passed, untouched, to |read_cb|.
52// Neither |reader|, nor |reactor|, nor |read_cb| may be NULL. |context| may be NULL.
53void eager_reader_register(eager_reader_t *reader, reactor_t *reactor, eager_reader_cb read_cb, void *context);
54
55// Unregisters |reader| from whichever reactor it is registered with, if any. This
56// function is idempotent.
57void eager_reader_unregister(eager_reader_t *reader);
58
59// Reads up to |max_size| bytes into |buffer|. If |block| is true, blocks until
60// |max_size| bytes are read. Otherwise only reads from currently available bytes.
61// NOT SAFE FOR READING FROM MULTIPLE THREADS
62// but you should probably only be reading from one thread anyway,
63// otherwise the byte stream probably doesn't make sense.
64size_t eager_reader_read(eager_reader_t *reader, uint8_t *buffer, size_t max_size, bool block);
65
66// Returns the inbound read thread for a given |reader| or NULL if the thread
67// is not running.
68thread_t* eager_reader_get_read_thread(const eager_reader_t *reader);
69