82 lines
1.6 KiB
C
82 lines
1.6 KiB
C
#pragma once
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
|
|
#include "ringbuf.h"
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
struct bsock {
|
|
struct bsock_ringbuf rbuf;
|
|
struct bsock_ringbuf wbuf;
|
|
struct bsock_ringbuf_io io;
|
|
void *io_ctx;
|
|
};
|
|
|
|
struct bsock_ringbuf_io bsock_io_posix();
|
|
|
|
struct bsock *bsock_create(void *io_ctx, struct bsock_ringbuf_io *io, size_t rbuf_sz,
|
|
size_t wbuf_sz);
|
|
|
|
void bsock_free(struct bsock *bsock);
|
|
|
|
static inline void
|
|
bsock_init(struct bsock *bsock, void *io_ctx, struct bsock_ringbuf_io *io,
|
|
struct bsock_ringbuf *rbuf, struct bsock_ringbuf *wbuf)
|
|
{
|
|
bsock->io_ctx = io_ctx;
|
|
memcpy(&bsock->io, io, sizeof(struct bsock_ringbuf_io));
|
|
memcpy(&bsock->rbuf, rbuf, sizeof(struct bsock_ringbuf));
|
|
memcpy(&bsock->wbuf, wbuf, sizeof(struct bsock_ringbuf));
|
|
}
|
|
|
|
/**
|
|
* returns:
|
|
* success: # of bytes written
|
|
* failure: -1 + errno
|
|
*/
|
|
int bsock_write(struct bsock *sock, char *buf, size_t len);
|
|
|
|
/**
|
|
* returns:
|
|
* success: # of bytes read
|
|
* failure: -1 + errno
|
|
*/
|
|
int bsock_read(struct bsock *sock, char *buf, size_t len);
|
|
|
|
/**
|
|
* returns: # of bytes available for read
|
|
*/
|
|
int bsock_read_avail_size(struct bsock *sock);
|
|
|
|
/**
|
|
* returns: # of bytes available for write before forcing a flush
|
|
*/
|
|
int bsock_write_avail_size(struct bsock *sock);
|
|
|
|
/**
|
|
* returns:
|
|
* success: # of bytes read
|
|
* failure: -1 + errno
|
|
*/
|
|
int bsock_peek(struct bsock *sock, char *buf, size_t len);
|
|
|
|
/**
|
|
* returns:
|
|
* success: # of bytes polled
|
|
* failure: -1 + errno
|
|
*/
|
|
int bsock_poll(struct bsock *sock);
|
|
|
|
/**
|
|
* returns:
|
|
* success: # of bytes flushed
|
|
* failure: -1 + errno
|
|
*/
|
|
int bsock_flush(struct bsock *sock);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif |