#ifndef NETSOCK_NETSOCK_H_
#define NETSOCK_NETSOCK_H_

#include <stdbool.h>
#include "spinlock/spinlock.h"
#include "pico_socket.h"
#include "ipc/pipe/pipe.h"
#include "rbuf/rbuf.h"

typedef uint16_t IpcNetSockEventBuffer;
#define IPC_NETSOCK_EVENTBUFFER_MAX 512

typedef struct IpcNetSock {
  struct IpcNetSock *next;
  struct pico_socket *picosock;
  IpcPipe *datapipe;
  RBufT eventbuffer;
  SpinLock spinlock;
} IpcNetSock;

typedef struct {
  IpcNetSock *netsocks;
  SpinLock spinlock;
} IpcNetSocks;

extern IpcNetSocks IPC_NETSOCKS;

void ipc_netsockinit(void);
IpcNetSock *ipc_netsockmake(uint16_t net, uint16_t proto, uint16_t port);
int32_t ipc_netsocklisten(IpcNetSock *netsock, size_t maxlisteners);

#endif // NETSOCK_NETSOCK_H_
