Implement patty_ax25_next_event() like I'm not a dumbass or some shit
This commit is contained in:
parent
5fb55973ad
commit
a744fa640c
2 changed files with 67 additions and 10 deletions
|
@ -15,12 +15,15 @@
|
|||
#include <patty/ax25/frame.h>
|
||||
#include <patty/ax25/if.h>
|
||||
|
||||
enum patty_ax25_event {
|
||||
PATTY_AX25_IO_UNKNOWN,
|
||||
PATTY_AX25_IO_RECV,
|
||||
PATTY_AX25_IO_SEND,
|
||||
PATTY_AX25_IO_READ,
|
||||
PATTY_AX25_IO_WRITE
|
||||
#define PATTY_AX25_EVENT_QUEUE_SIZE 32
|
||||
|
||||
enum patty_ax25_event_type {
|
||||
PATTY_AX25_EVENT_UNKNOWN,
|
||||
PATTY_AX25_EVENT_RECV,
|
||||
PATTY_AX25_EVENT_SEND,
|
||||
PATTY_AX25_EVENT_READ,
|
||||
PATTY_AX25_EVENT_WRITE,
|
||||
PATTY_AX25_EVENT_ERROR
|
||||
};
|
||||
|
||||
enum patty_ax25_sock_status {
|
||||
|
@ -55,6 +58,12 @@ typedef struct _patty_ax25_sock {
|
|||
patty_ax25_address * remote;
|
||||
} patty_ax25_sock;
|
||||
|
||||
typedef struct _patty_ax25_event {
|
||||
enum patty_ax25_event_type type;
|
||||
|
||||
int fd;
|
||||
} patty_ax25_event;
|
||||
|
||||
struct _patty_ax25 {
|
||||
patty_list * ifaces;
|
||||
patty_list * socks;
|
||||
|
@ -62,8 +71,13 @@ struct _patty_ax25 {
|
|||
int unix_fdmax;
|
||||
fd_set unix_fds;
|
||||
|
||||
fd_set unix_fds_pending_read;
|
||||
fd_set unix_fds_pending_write;
|
||||
fd_set unix_fds_pending_error;
|
||||
|
||||
patty_dict * fd_lookup;
|
||||
int fd;
|
||||
int current;
|
||||
};
|
||||
|
||||
int patty_ax25_init(patty_ax25 *ax25);
|
||||
|
@ -89,7 +103,7 @@ int patty_ax25_accept(patty_ax25 *ax25,
|
|||
int patty_ax25_close(patty_ax25 *ax25, int fd);
|
||||
|
||||
int patty_ax25_next_event(patty_ax25 *ax25,
|
||||
enum patty_ax25_event *ev);
|
||||
patty_ax25_event *ev);
|
||||
|
||||
int patty_ax25_recv(patty_ax25 *ax25,
|
||||
int fd, patty_ax25_frame *frame);
|
||||
|
|
49
src/ax25.c
49
src/ax25.c
|
@ -1,6 +1,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <sys/select.h>
|
||||
|
||||
#include <patty/ax25.h>
|
||||
|
||||
|
@ -19,8 +20,6 @@ int patty_ax25_init(patty_ax25 *ax25) {
|
|||
goto error_dict_new_fd_lookup;
|
||||
}
|
||||
|
||||
ax25->fd = 0;
|
||||
|
||||
return 0;
|
||||
|
||||
error_dict_new_fd_lookup:
|
||||
|
@ -188,6 +187,50 @@ error_fd_lookup:
|
|||
return -1;
|
||||
}
|
||||
|
||||
int patty_ax25_next_event(patty_ax25 *ax25, enum patty_ax25_event *ev) {
|
||||
int patty_ax25_next_event(patty_ax25 *ax25, patty_ax25_event *ev) {
|
||||
int fd;
|
||||
|
||||
if (ax25->current == 0) {
|
||||
memcpy(&ax25->unix_fds_pending_read, &ax25->unix_fds, sizeof(ax25->unix_fds));
|
||||
memcpy(&ax25->unix_fds_pending_write, &ax25->unix_fds, sizeof(ax25->unix_fds));
|
||||
memcpy(&ax25->unix_fds_pending_error, &ax25->unix_fds, sizeof(ax25->unix_fds));
|
||||
|
||||
if (select(ax25->unix_fdmax,
|
||||
&ax25->unix_fds_pending_read,
|
||||
&ax25->unix_fds_pending_write,
|
||||
&ax25->unix_fds_pending_error,
|
||||
NULL) < 0) {
|
||||
goto error_select;
|
||||
}
|
||||
}
|
||||
|
||||
for (fd=ax25->current; fd<ax25->unix_fdmax; fd++) {
|
||||
if (FD_ISSET(fd, &ax25->unix_fds_pending_read)) {
|
||||
ev->type = PATTY_AX25_EVENT_RECV;
|
||||
ev->fd = fd;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (FD_ISSET(fd, &ax25->unix_fds_pending_write)) {
|
||||
ev->type = PATTY_AX25_EVENT_SEND;
|
||||
ev->fd = fd;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (FD_ISSET(fd, &ax25->unix_fds_pending_error)) {
|
||||
ev->type = PATTY_AX25_EVENT_ERROR;
|
||||
ev->fd = fd;
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
ax25->current = 0;
|
||||
|
||||
return 0;
|
||||
|
||||
error_select:
|
||||
return -1;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue