aboutsummaryrefslogtreecommitdiff
path: root/include/sockbuf.h
blob: 0a11c4f5337c115d7749da988411fd41a11bb537 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#pragma once
#ifndef SOCKBUF_H_
#define SOCKBUF_H_

#include <inttypes.h>
#include <stdlib.h>

#define SOCKBUF_BUFLEN 128
#define SOCKBUF_CLOSED 1

typedef struct {
  uint8_t buf[SOCKBUF_BUFLEN];

  uint8_t next;
  uint8_t last;

  int socket;
  uint8_t flags;
} sockbuf_t;

/* Initialize a sockbuf from the provided file descriptor. */
sockbuf_t* init_sockbuf(sockbuf_t* sockbuf, int socket);

/* Writes a null-terminated string to the socket. */
void sockbuf_write(sockbuf_t* sockbuf, const char* value);

/*
 * Return the next character of the sockbuf or -1 if EOF was reached.
 *
 * This does not advance the pointer.
 */
int peek_char(sockbuf_t* sockbuf);

/*
 * Return and consume the next character of the sockbuf or -1 if EOF was
 * reached.
 */
int get_char(sockbuf_t* sockbuf);

/*
 * Reads a token from the sockbuf. Tokens are separated by spaces.
 */
char* read_token(sockbuf_t* sockbuf, char* into, size_t sz);

#endif /* SOCKBUF_H_ */