aboutsummaryrefslogtreecommitdiff
path: root/daemon/AnnotateListener.cpp
blob: 50110b4dc84c40b120f5506cf4e16f69e36c17ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
 * Copyright (C) ARM Limited 2014. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include "AnnotateListener.h"

#include <unistd.h>

#include "OlySocket.h"

struct AnnotateClient {
	AnnotateClient *next;
	int fd;
};

AnnotateListener::AnnotateListener() : mClients(NULL), mSock(NULL) {
}

AnnotateListener::~AnnotateListener() {
	close();
	delete mSock;
}

void AnnotateListener::setup() {
	mSock = new OlyServerSocket(8082);
}

int AnnotateListener::getFd() {
	return mSock->getFd();
}

void AnnotateListener::handle() {
	AnnotateClient *const client = new AnnotateClient();
	client->fd = mSock->acceptConnection();
	client->next = mClients;
	mClients = client;
}

void AnnotateListener::close() {
	mSock->closeServerSocket();
	while (mClients != NULL) {
		::close(mClients->fd);
		AnnotateClient *next = mClients->next;
		delete mClients;
		mClients = next;
	}
}

void AnnotateListener::signal() {
	const char ch = 0;
	AnnotateClient **ptr = &mClients;
	AnnotateClient *client = mClients;
	while (client != NULL) {
		if (write(client->fd, &ch, sizeof(ch)) != 1) {
			::close(client->fd);
			AnnotateClient *next = client->next;
			delete client;
			*ptr = next;
			client = next;
			continue;
		}
		ptr = &client->next;
		client = client->next;
	}
}