[lib][minip] change tcp socket handles to an opaque tcp_socket_t struct

This commit is contained in:
Travis Geiselbrecht
2014-08-06 15:01:28 -07:00
parent b8bd9b9edb
commit 45b8467ff0
4 changed files with 30 additions and 24 deletions

View File

@@ -35,6 +35,7 @@
static int chargen_worker(void *socket)
{
uint64_t count = 0;
tcp_socket_t *s = socket;
#define CHARGEN_BUFSIZE (64*1024)
/* allocate a large buffer to flood the socket layer */
@@ -50,7 +51,7 @@ static int chargen_worker(void *socket)
}
for (;;) {
ssize_t ret = tcp_write(socket, buf, CHARGEN_BUFSIZE);
ssize_t ret = tcp_write(s, buf, CHARGEN_BUFSIZE);
//TRACEF("tcp_write returns %d\n", ret);
if (ret < 0)
break;
@@ -60,7 +61,7 @@ static int chargen_worker(void *socket)
TRACEF("chargen worker exiting, wrote %llu bytes\n", count);
free(buf);
tcp_close(socket);
tcp_close(s);
return 0;
}
@@ -68,7 +69,7 @@ static int chargen_worker(void *socket)
static int chargen_server(void *arg)
{
status_t err;
void *listen_socket;
tcp_socket_t *listen_socket;
err = tcp_open_listen(&listen_socket, 19);
if (err < 0) {
@@ -77,7 +78,7 @@ static int chargen_server(void *arg)
}
for (;;) {
void *accept_socket;
tcp_socket_t *accept_socket;
err = tcp_accept(listen_socket, &accept_socket);
TRACEF("tcp_accept returns returns %d, handle %p\n", err, accept_socket);
@@ -95,11 +96,12 @@ static int discard_worker(void *socket)
{
uint64_t count = 0;
uint32_t crc = 0;
tcp_socket_t *s = socket;
for (;;) {
uint8_t buf[1024];
ssize_t ret = tcp_read(socket, buf, sizeof(buf));
ssize_t ret = tcp_read(s, buf, sizeof(buf));
if (ret <= 0)
break;
@@ -109,7 +111,7 @@ static int discard_worker(void *socket)
}
TRACEF("discard worker exiting, read %llu bytes, crc32 0x%x\n", count, crc);
tcp_close(socket);
tcp_close(s);
return 0;
}
@@ -117,7 +119,7 @@ static int discard_worker(void *socket)
static int discard_server(void *arg)
{
status_t err;
void *listen_socket;
tcp_socket_t *listen_socket;
err = tcp_open_listen(&listen_socket, 9);
if (err < 0) {
@@ -126,7 +128,7 @@ static int discard_server(void *arg)
}
for (;;) {
void *accept_socket;
tcp_socket_t *accept_socket;
err = tcp_accept(listen_socket, &accept_socket);
TRACEF("tcp_accept returns returns %d, handle %p\n", err, accept_socket);

View File

@@ -46,7 +46,7 @@ size_t lkb_iobuffer_size = 16*1024*1024;
#define STATE_ERROR 4
typedef struct LKB {
void *s;
tcp_socket_t *s;
int state;
size_t avail;
} lkb_t;
@@ -203,8 +203,8 @@ fail:
}
static int lkboot_server(void *arg) {
void *listen_socket;
void *s;
tcp_socket_t *listen_socket;
tcp_socket_t *s;
if (tcp_open_listen(&listen_socket, 1023) < 0) {
printf("lkboot: error opening listen socket\n");
@@ -253,3 +253,5 @@ APP_START(lkboot)
.init = lkboot_init,
.flags = 0,
APP_END
// vim: noexpandtab

View File

@@ -75,10 +75,12 @@ int minip_udp_send(const void *data, size_t len,
int minip_udp_listen(uint16_t port, udp_callback_t rx_handler, void *arg);
/* tcp */
status_t tcp_open_listen(void **handle, uint16_t port);
status_t tcp_accept(void *listen_socket, void **accept_socket);
status_t tcp_close(void *socket);
ssize_t tcp_read(void *socket, void *buf, size_t len);
ssize_t tcp_write(void *socket, const void *buf, size_t len);
typedef struct tcp_socket tcp_socket_t;
status_t tcp_open_listen(tcp_socket_t **handle, uint16_t port);
status_t tcp_accept(tcp_socket_t *listen_socket, tcp_socket_t **accept_socket);
status_t tcp_close(tcp_socket_t *socket);
ssize_t tcp_read(tcp_socket_t *socket, void *buf, size_t len);
ssize_t tcp_write(tcp_socket_t *socket, const void *buf, size_t len);
// vim: set ts=4 sw=4 expandtab:

View File

@@ -736,7 +736,7 @@ static tcp_socket_t *create_tcp_socket(bool alloc_buffers)
/* user api */
status_t tcp_open_listen(void **handle, uint16_t port)
status_t tcp_open_listen(tcp_socket_t **handle, uint16_t port)
{
tcp_socket_t *s;
@@ -762,12 +762,12 @@ status_t tcp_open_listen(void **handle, uint16_t port)
return NO_ERROR;
}
status_t tcp_accept(void *listen_socket, void **accept_socket)
status_t tcp_accept(tcp_socket_t *listen_socket, tcp_socket_t **accept_socket)
{
if (!listen_socket || !accept_socket)
return ERR_INVALID_ARGS;
tcp_socket_t *s = (tcp_socket_t *)listen_socket;
tcp_socket_t *s = listen_socket;
/* block to accept a socket */
sem_wait(&s->accept_sem);
@@ -784,7 +784,7 @@ status_t tcp_accept(void *listen_socket, void **accept_socket)
return NO_ERROR;
}
ssize_t tcp_read(void *socket, void *buf, size_t len)
ssize_t tcp_read(tcp_socket_t *socket, void *buf, size_t len)
{
if (!socket)
return ERR_INVALID_ARGS;
@@ -829,7 +829,7 @@ out:
return ret;
}
ssize_t tcp_write(void *socket, const void *buf, size_t len)
ssize_t tcp_write(tcp_socket_t *socket, const void *buf, size_t len)
{
if (!socket)
return ERR_INVALID_ARGS;
@@ -886,7 +886,7 @@ ssize_t tcp_write(void *socket, const void *buf, size_t len)
return len;
}
status_t tcp_close(void *socket)
status_t tcp_close(tcp_socket_t *socket)
{
if (!socket)
return ERR_INVALID_ARGS;
@@ -949,12 +949,12 @@ usage:
if (!strcmp(argv[1].str, "listen")) {
if (argc < 3) goto notenoughargs;
void *handle;
tcp_socket_t *handle;
err = tcp_open_listen(&handle, argv[2].u);
printf("tcp_open_listen returns %d, handle %p\n", err, handle);
void *accepted;
tcp_socket_t *accepted;
err = tcp_accept(handle, &accepted);
printf("tcp_accept returns returns %d, handle %p\n", err, accepted);