Lely core libraries  2.2.5
io.c
Go to the documentation of this file.
1 
24 #include "io.h"
25 #include "handle.h"
26 #include <lely/util/errnum.h>
27 
28 #include <assert.h>
29 
30 static int lely_io_ref;
31 
32 int
34 {
35  if (lely_io_ref++)
36  return 0;
37 
38 #ifdef _WIN32
39  int errc = 0;
40 
41  WORD wVersionRequested = MAKEWORD(2, 2);
42  WSADATA wsaData;
43  if ((errc = WSAStartup(wVersionRequested, &wsaData)))
44  goto error_WSAStartup;
45 #endif
46 
47  return 0;
48 
49 #ifdef _WIN32
50  // WSACleanup();
51 error_WSAStartup:
52  lely_io_ref--;
53  set_errc(errc);
54  return -1;
55 #endif
56 }
57 
58 void
60 {
61  if (lely_io_ref <= 0) {
62  lely_io_ref = 0;
63  return;
64  }
65  if (--lely_io_ref)
66  return;
67 
68 #ifdef _WIN32
69  WSACleanup();
70 #endif
71 }
72 
73 int
75 {
76  if (handle == IO_HANDLE_ERROR) {
78  return -1;
79  }
80 
81  io_handle_release(handle);
82 
83  return 0;
84 }
85 
86 int
88 {
89  if (handle == IO_HANDLE_ERROR) {
91  return -1;
92  }
93 
94  assert(handle->vtab);
95  return handle->vtab->type;
96 }
97 
98 HANDLE
100 {
101  if (handle == IO_HANDLE_ERROR) {
103  return INVALID_HANDLE_VALUE;
104  }
105 
106  return handle->fd;
107 }
108 
109 int
111 {
112  if (handle == IO_HANDLE_ERROR) {
114  return -1;
115  }
116 
117  io_handle_lock(handle);
118  int flags = handle->flags;
119  io_handle_unlock(handle);
120  return flags;
121 }
122 
123 int
124 io_set_flags(io_handle_t handle, int flags)
125 {
126  if (handle == IO_HANDLE_ERROR) {
128  return -1;
129  }
130 
132 
133  int result = 0;
134  io_handle_lock(handle);
135  if (flags != handle->flags) {
136  if (handle->vtab->flags) {
137  result = handle->vtab->flags(handle, flags);
138  if (!result)
139  handle->flags = flags;
140  } else {
142  result = -1;
143  }
144  }
145  io_handle_unlock(handle);
146  return result;
147 }
148 
149 ssize_t
150 io_read(io_handle_t handle, void *buf, size_t nbytes)
151 {
152  if (handle == IO_HANDLE_ERROR) {
154  return -1;
155  }
156 
157  assert(handle->vtab);
158  if (!handle->vtab->read) {
160  return -1;
161  }
162 
163  return handle->vtab->read(handle, buf, nbytes);
164 }
165 
166 ssize_t
167 io_write(io_handle_t handle, const void *buf, size_t nbytes)
168 {
169  if (handle == IO_HANDLE_ERROR) {
171  return -1;
172  }
173 
174  assert(handle->vtab);
175  if (!handle->vtab->write) {
177  return -1;
178  }
179 
180  return handle->vtab->write(handle, buf, nbytes);
181 }
182 
183 int
185 {
186  if (handle == IO_HANDLE_ERROR) {
188  return -1;
189  }
190 
191  assert(handle->vtab);
192  if (!handle->vtab->flush) {
194  return -1;
195  }
196 
197  return handle->vtab->flush(handle);
198 }
io_flush
int io_flush(io_handle_t handle)
Flushes the write buffer of a an I/O device.
Definition: io.c:184
IO_FLAG_NONBLOCK
@ IO_FLAG_NONBLOCK
Perform I/O operations in non-blocking mode.
Definition: io.h:62
ERRNUM_NXIO
@ ERRNUM_NXIO
No such device or address.
Definition: errnum.h:197
io_handle_vtab::flush
int(* flush)(struct io_handle *handle)
A pointer to the flush method.
Definition: handle.h:92
io_handle_vtab::write
ssize_t(* write)(struct io_handle *handle, const void *buf, size_t nbytes)
A pointer to the write method.
Definition: handle.h:89
io_get_fd
HANDLE io_get_fd(io_handle_t handle)
Returns the native file descriptor of an I/O device.
Definition: io.c:99
io_handle_release
void io_handle_release(io_handle_t handle)
Decrements the reference count of an I/O device handle.
Definition: handle.c:47
io_handle_vtab::flags
int(* flags)(struct io_handle *handle, int flags)
A pointer to the static flags method.
Definition: handle.h:85
io_get_flags
int io_get_flags(io_handle_t handle)
Obtains the flags of an I/O device.
Definition: io.c:110
io_read
ssize_t io_read(io_handle_t handle, void *buf, size_t nbytes)
Performs a read operation.
Definition: io.c:150
io_get_type
int io_get_type(io_handle_t handle)
Returns the type of an I/O device (one of IO_TYPE_CAN, IO_TYPE_FILE, IO_TYPE_PIPE,...
Definition: io.c:87
io_handle_vtab::type
int type
The type of the device (one of IO_TYPE_CAN, IO_TYPE_FILE, IO_TYPE_PIPE, IO_TYPE_SERIAL or IO_TYPE_SOC...
Definition: handle.h:79
io_handle_unlock
void io_handle_unlock(struct io_handle *handle)
Unlocks a locked I/O device handle.
Definition: handle.c:147
io_handle
An I/O device handle.
Definition: handle.h:41
ERRNUM_BADF
@ ERRNUM_BADF
Bad file descriptor.
Definition: errnum.h:90
set_errnum
void set_errnum(errnum_t errnum)
Sets the current (thread-specific) platform-independent error number to errnum.
Definition: errnum.h:375
set_errc
void set_errc(int errc)
Sets the current (thread-specific) native error code to errc.
Definition: errnum.c:957
lely_io_init
int lely_io_init(void)
Initializes the I/O library and makes the I/O functions available for use.
Definition: io.c:33
handle.h
io.h
IO_FLAG_LOOPBACK
@ IO_FLAG_LOOPBACK
Receive own messages (i.e., sent by the same device).
Definition: io.h:64
errnum.h
IO_HANDLE_ERROR
#define IO_HANDLE_ERROR
The value of an invalid I/O device handle.
Definition: io.h:34
io_handle::vtab
const struct io_handle_vtab * vtab
A pointer to the virtual table.
Definition: handle.h:43
IO_FLAG_NO_CLOSE
@ IO_FLAG_NO_CLOSE
Do not close the native file descriptor when closing an I/O device.
Definition: io.h:60
io_handle::flags
int flags
The I/O device flags (any combination of IO_FLAG_NO_CLOSE and IO_FLAG_NONBLOCK).
Definition: handle.h:62
io_set_flags
int io_set_flags(io_handle_t handle, int flags)
Sets the flags of an I/O device.
Definition: io.c:124
io_handle::fd
int fd
The native file descriptor.
Definition: handle.h:56
io_handle_vtab::read
ssize_t(* read)(struct io_handle *handle, void *buf, size_t nbytes)
A pointer to the read method.
Definition: handle.h:87
io_handle_lock
void io_handle_lock(struct io_handle *handle)
Locks an unlocked I/O device handle, so the flags (and other device-specific fields) can safely be ac...
Definition: handle.c:139
io_close
int io_close(io_handle_t handle)
Closes an I/O device.
Definition: io.c:74
lely_io_fini
void lely_io_fini(void)
Finalizes the I/O library and terminates the availability of the I/O functions.
Definition: io.c:59
io_write
ssize_t io_write(io_handle_t handle, const void *buf, size_t nbytes)
Performs a write operation.
Definition: io.c:167