static bool dragonnet_peer_init_accepted(DragonnetPeer *p, int sock,
struct sockaddr_in6 addr, DragonnetListener *l)
{
- p->mu = malloc(sizeof *p->mu);
- pthread_rwlock_init(p->mu, NULL);
- pthread_rwlock_wrlock(p->mu);
+ pthread_rwlock_init(&p->mu, NULL);
+ pthread_rwlock_wrlock(&p->mu);
p->sock = sock;
p->laddr = l->laddr;
return false;
}
- pthread_rwlock_unlock(p->mu);
+ pthread_rwlock_unlock(&p->mu);
return true;
}
void (*on_connect)(DragonnetPeer *p))
{
DragonnetListener *l = malloc(sizeof *l);
- l->mu = malloc(sizeof *l->mu);
- pthread_rwlock_init(l->mu, NULL);
- pthread_rwlock_wrlock(l->mu);
+ pthread_rwlock_init(&l->mu, NULL);
+ pthread_rwlock_wrlock(&l->mu);
l->sock = socket(AF_INET6, SOCK_STREAM, 0);
l->on_connect = on_connect;
return NULL;
}
- pthread_rwlock_unlock(l->mu);
+ pthread_rwlock_unlock(&l->mu);
return l;
}
void dragonnet_listener_run(DragonnetListener *l)
{
- pthread_rwlock_wrlock(l->mu);
+ pthread_rwlock_wrlock(&l->mu);
assert(l->state == DRAGONNET_LISTENER_CREATED);
l->state++;
- pthread_rwlock_unlock(l->mu);
+ pthread_rwlock_unlock(&l->mu);
while (l->state == DRAGONNET_LISTENER_ACTIVE) {
struct sockaddr_in6 clt_addr;
void dragonnet_listener_close(DragonnetListener *l)
{
- pthread_rwlock_wrlock(l->mu);
+ pthread_rwlock_wrlock(&l->mu);
assert(l->state == DRAGONNET_LISTENER_ACTIVE);
close(l->sock);
l->sock = 0;
l->state++;
- pthread_rwlock_unlock(l->mu);
+ pthread_rwlock_unlock(&l->mu);
}
void dragonnet_listener_delete(DragonnetListener *l)
{
- pthread_rwlock_destroy(l->mu);
+ pthread_rwlock_destroy(&l->mu);
free(l);
}
void (*on_connect)(DragonnetPeer *p);
DragonnetListenerState state;
- pthread_rwlock_t *mu;
+ pthread_rwlock_t mu;
} DragonnetListener;
DragonnetListener *dragonnet_listener_new(char *addr,
static bool dragonnet_peer_init(DragonnetPeer *p, char *addr)
{
- p->mu = malloc(sizeof *p->mu);
- pthread_rwlock_init(p->mu, NULL);
- pthread_rwlock_wrlock(p->mu);
+ pthread_rwlock_init(&p->mu, NULL);
+ pthread_rwlock_wrlock(&p->mu);
p->sock = socket(AF_INET6, SOCK_STREAM, 0);
p->raddr = dragonnet_addr_parse_str(addr);
p->laddr = dragonnet_addr_parse_sock(sock_name);
- pthread_rwlock_unlock(p->mu);
+ pthread_rwlock_unlock(&p->mu);
return true;
}
void dragonnet_peer_close(DragonnetPeer *p)
{
- pthread_rwlock_wrlock(p->mu);
+ pthread_rwlock_wrlock(&p->mu);
if (p->state == DRAGONNET_PEER_ACTIVE) {
shutdown(p->sock, SHUT_RDWR);
p->state++;
}
- pthread_rwlock_unlock(p->mu);
+ pthread_rwlock_unlock(&p->mu);
}
void dragonnet_peer_delete(DragonnetPeer *p)
{
- pthread_rwlock_destroy(p->mu);
+ pthread_rwlock_destroy(&p->mu);
free(p);
}
DragonnetAddr laddr, raddr;
DragonnetPeerState state;
- pthread_rwlock_t *mu;
+ pthread_rwlock_t mu;
} DragonnetPeer;
DragonnetPeer *dragonnet_connect(char *addr);
{
DragonnetPeer *p = (DragonnetPeer *) g_peer;
- pthread_rwlock_wrlock(p->mu);
+ pthread_rwlock_wrlock(&p->mu);
assert(p->state == DRAGONNET_PEER_CREATED);
p->state++;
- pthread_rwlock_unlock(p->mu);
+ pthread_rwlock_unlock(&p->mu);
while (true) {
uint16_t msg;
- pthread_rwlock_rdlock(p->mu);
+ pthread_rwlock_rdlock(&p->mu);
ssize_t len = recv(p->sock, &msg, sizeof msg, MSG_WAITALL);
- pthread_rwlock_unlock(p->mu);
+ pthread_rwlock_unlock(&p->mu);
if (len < 0 && errno != EWOULDBLOCK) {
perror("recv");
// connection closed
if ((len >= 0 && len != sizeof msg) || errno == EWOULDBLOCK) {
- pthread_rwlock_wrlock(p->mu);
+ pthread_rwlock_wrlock(&p->mu);
close(p->sock);
p->sock = 0;
p->state++;
- pthread_rwlock_unlock(p->mu);
+ pthread_rwlock_unlock(&p->mu);
return NULL;
}