aboutsummaryrefslogtreecommitdiffstats
path: root/libcoopgamma_set_nonblocking.c
blob: 91bae5a1b799483136073b197e645f51048c3d4b (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
/* See LICENSE file for copyright and license details. */
#include "common.h"


/**
 * By default communication is blocking, this function
 * can be used to switch between blocking and nonblocking
 * 
 * After setting the communication to nonblocking,
 * `libcoopgamma_flush`, `libcoopgamma_synchronise` and
 * and request-sending functions can fail with EAGAIN and
 * EWOULDBLOCK. It is safe to continue with `libcoopgamma_flush`
 * (for `libcoopgamma_flush` it selfand equest-sending functions)
 * or `libcoopgamma_synchronise` just like EINTR failure.
 * 
 * @param   ctx          The state of the library, must be connected
 * @param   nonblocking  Nonblocking mode?
 * @return               Zero on success, -1 on error
 */
int
libcoopgamma_set_nonblocking(libcoopgamma_context_t *restrict ctx, int nonblocking)
{
	int flags = fcntl(ctx->fd, F_GETFL);
	if (flags == -1)
		return -1;
	if (nonblocking)
		flags |= O_NONBLOCK;
	else
		flags &= ~O_NONBLOCK;
	if (fcntl(ctx->fd, F_SETFL, flags) == -1)
		return -1;
	ctx->blocking = !nonblocking;
	return 0;
}