Example #1
0
/**
* Execute the queued commands
**/
void CmdManager::exec(GameState* st)
{
    Cmd* c;
    while (work.trypop(c)) {
        c->exec(st);
    }
}
Example #2
0
void *threadFn(void *arg){
    int fd;
    struct pollfd fds;
    char buf[BUFSIZE];
    char fifo[BUFSIZE];
    struct stat st;
    void **arr = (void**)arg;
    char *target = (char*)arr[0];
    int polr = 0;
    bzero(fifo, BUFSIZE);
    strcpy(fifo, target);
    Cmd *cmd = (Cmd*)arr[1];

    fd = open(fifo, O_RDONLY);
    if (!fd) {
	fprintf(stderr, "Can not open %s\n", fifo);
	return NULL;
    }
    fds.fd = fd;
    fds.events = POLLIN;
    polr = poll(&fds, 1, 1000);
    if (polr < 0) {
	fprintf(stderr, "Poll error for %s\n", fifo);
	return NULL;
    }
    if (!polr) {
	fprintf(stderr, "Can not read from %s by timeout\n", fifo);
	if (!stat(fifo, &st)) unlink(fifo);
	return NULL;
    }

    FILE *fp = fdopen(fd, "r");
    if (!fp) {
	fprintf(stderr, "Can not read %s\n", fifo);
	return NULL;
    }
    fgets(buf, BUFSIZE, fp);
    fclose(fp);

    if (buf[BUFSIZE - 1]) {
	fprintf(stderr, "Recieved string is too long, exit\n");
	return NULL;
    }
    cmd->exec(buf, BUFSIZE);
    fp = fopen(fifo, "w");
    if (!fp) {
	fprintf(stderr, "Can not write to %s\n", fifo);
	return NULL;
    }
    fwrite(buf, 1, strlen(buf), fp);
    fclose(fp);
    sleep(1);
    if (!stat(fifo, &st)) unlink(fifo);
    return NULL;
}