mirror of
https://github.com/brmlab/brmlife.git
synced 2025-08-02 18:03:37 +02:00
Connection communication in separate thread
This commit is contained in:
parent
ec3897b29b
commit
92940b6054
4 changed files with 92 additions and 7 deletions
5
Makefile
5
Makefile
|
@ -1,9 +1,10 @@
|
|||
CFLAGS=-Wall -g
|
||||
CFLAGS=-Wall -g -pthread
|
||||
LDFLAGS=-pthread
|
||||
|
||||
OBJS=main.o map.o agent.o connection.o
|
||||
|
||||
brmlife: $(OBJS)
|
||||
$(CXX) -o $@ $^
|
||||
$(CXX) $(LDFLAGS) -o $@ $^
|
||||
|
||||
|
||||
clean:
|
||||
|
|
6
agent.cc
6
agent.cc
|
@ -51,6 +51,8 @@ agent::on_senses_update(void)
|
|||
agent::~agent()
|
||||
{
|
||||
tile->on_agent_leave(*this);
|
||||
if (conn)
|
||||
delete conn;
|
||||
if (conn) {
|
||||
conn->cancel();
|
||||
conn = NULL;
|
||||
}
|
||||
};
|
||||
|
|
|
@ -4,6 +4,8 @@
|
|||
#include <iostream>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
#include "agent.h"
|
||||
#include "connection.h"
|
||||
|
||||
|
@ -12,6 +14,66 @@ connection::senses(int tick_id, char around[4])
|
|||
{
|
||||
char buf[1024];
|
||||
snprintf(buf, sizeof(buf), "tick %d\naround %c%c%c%c\n\n", tick_id, around[0], around[1], around[2], around[3]);
|
||||
if (write(fd, buf, strlen(buf)) < (ssize_t) strlen(buf))
|
||||
error = true;
|
||||
|
||||
pthread_mutex_lock(&buf_lock);
|
||||
out_buf.append(buf);
|
||||
pthread_mutex_unlock(&buf_lock);
|
||||
}
|
||||
|
||||
void
|
||||
connection::cancel(void)
|
||||
{
|
||||
pthread_cond_signal(&cancel_cond);
|
||||
}
|
||||
|
||||
|
||||
void *
|
||||
conn_thread_worker(void *ctx)
|
||||
{
|
||||
class connection *conn = (class connection *) ctx;
|
||||
conn->thread_loop();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void
|
||||
connection::spawn_thread(void)
|
||||
{
|
||||
pthread_mutex_init(&buf_lock, NULL);
|
||||
pthread_cond_init(&cancel_cond, NULL);
|
||||
pthread_mutex_init(&cancel_lock, NULL);
|
||||
|
||||
pthread_t tid;
|
||||
pthread_create(&tid, NULL, conn_thread_worker, (void *) this);
|
||||
pthread_detach(tid);
|
||||
}
|
||||
|
||||
void
|
||||
connection::thread_loop(void)
|
||||
{
|
||||
while (!error) {
|
||||
/* Repeatedly try to write and read stuff. */
|
||||
std::string buf;
|
||||
ssize_t len;
|
||||
|
||||
pthread_mutex_lock(&buf_lock);
|
||||
buf = out_buf;
|
||||
pthread_mutex_unlock(&buf_lock);
|
||||
|
||||
len = write(fd, buf.c_str(), buf.size());
|
||||
if (len < 0) {
|
||||
pthread_mutex_lock(&cancel_lock);
|
||||
error = true;
|
||||
} else {
|
||||
pthread_mutex_lock(&buf_lock);
|
||||
out_buf.erase(0, len);
|
||||
pthread_mutex_unlock(&buf_lock);
|
||||
}
|
||||
|
||||
/* TODO: The reading. ;-) */
|
||||
|
||||
usleep(10000); // XXX: Signal-oriented instead.
|
||||
}
|
||||
|
||||
pthread_cond_wait(&cancel_cond, &cancel_lock);
|
||||
delete this;
|
||||
}
|
||||
|
|
22
connection.h
22
connection.h
|
@ -1,6 +1,9 @@
|
|||
#ifndef BRMLIFE__CONNECTION_H
|
||||
#define BRMLIFE__CONNECTION_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "map.h"
|
||||
|
@ -10,7 +13,11 @@ public:
|
|||
int fd;
|
||||
bool error;
|
||||
|
||||
connection(int fd_) : fd(fd_) {}
|
||||
connection(int fd_)
|
||||
: fd(fd_), error(false)
|
||||
{
|
||||
spawn_thread();
|
||||
}
|
||||
|
||||
~connection()
|
||||
{
|
||||
|
@ -18,6 +25,19 @@ public:
|
|||
}
|
||||
|
||||
void senses(int tick_id, char around[4]);
|
||||
|
||||
void cancel(void);
|
||||
|
||||
private:
|
||||
std::string out_buf;
|
||||
pthread_mutex_t buf_lock;
|
||||
|
||||
pthread_cond_t cancel_cond;
|
||||
pthread_mutex_t cancel_lock;
|
||||
|
||||
void spawn_thread(void);
|
||||
friend void *conn_thread_worker(void *ctx);
|
||||
void thread_loop(void);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue