Lines Matching full:socket
27 #include <sys/socket.h>
35 #include "osi/include/socket.h"
54 LOG_ERROR("%s unable to allocate memory for socket.", __func__);
58 ret->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
60 LOG_ERROR("%s unable to create socket: %s", __func__, strerror(errno));
84 LOG_ERROR("%s unable to allocate memory for socket.", __func__);
92 void socket_free(socket_t *socket) {
93 if (!socket)
96 socket_unregister(socket);
97 close(socket->fd);
98 osi_free(socket);
101 bool socket_listen(const socket_t *socket, port_t port) {
102 assert(socket != NULL);
108 if (bind(socket->fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
109 LOG_ERROR("%s unable to bind socket to port %u: %s", __func__, port, strerror(errno));
113 if (listen(socket->fd, 10) == -1) {
121 socket_t *socket_accept(const socket_t *socket) {
122 assert(socket != NULL);
124 int fd = accept(socket->fd, NULL, NULL);
126 LOG_ERROR("%s unable to accept socket: %s", __func__, strerror(errno));
133 LOG_ERROR("%s unable to allocate memory for socket.", __func__);
141 ssize_t socket_read(const socket_t *socket, void *buf, size_t count) {
142 assert(socket != NULL);
145 return recv(socket->fd, buf, count, MSG_DONTWAIT);
148 ssize_t socket_write(const socket_t *socket, const void *buf, size_t count) {
149 assert(socket != NULL);
152 return send(socket->fd, buf, count, MSG_DONTWAIT);
155 ssize_t socket_write_and_transfer_fd(const socket_t *socket, const void *buf, size_t count, int fd) {
156 assert(socket != NULL);
160 return socket_write(socket, buf, count);
182 ssize_t ret = sendmsg(socket->fd, &msg, MSG_DONTWAIT);
187 ssize_t socket_bytes_available(const socket_t *socket) {
188 assert(socket != NULL);
191 if (ioctl(socket->fd, FIONREAD, &size) == -1)
196 void socket_register(socket_t *socket, reactor_t *reactor, void *context, socket_cb read_cb, socket_cb write_cb) {
197 assert(socket != NULL);
199 // Make sure the socket isn't currently registered.
200 socket_unregister(socket);
202 socket->read_ready = read_cb;
203 socket->write_ready = write_cb;
204 socket->context = context;
209 socket->reactor_object = reactor_register(reactor, socket->fd, socket, read_fn, write_fn);
212 void socket_unregister(socket_t *socket) {
213 assert(socket != NULL);
215 if (socket->reactor_object)
216 reactor_unregister(socket->reactor_object);
217 socket->reactor_object = NULL;
223 socket_t *socket = (void *)context;
224 socket->read_ready(socket, socket->context);
230 socket_t *socket = (void *)context;
231 socket->write_ready(socket, socket->context);