Handle partial transmissions of the buffer to the monitor

This commit is contained in:
dongresource 2020-12-02 11:40:34 +01:00
parent c6112d04da
commit ce1a5a7664

View File

@ -43,16 +43,18 @@ void Monitor::init() {
REGISTER_SHARD_TIMER(tick, 5000); REGISTER_SHARD_TIMER(tick, 5000);
} }
// XXX static bool transmit(std::list<int>::iterator& it, char *buff, int len) {
static bool transmit(std::list<int>::iterator it, char *buff, int len) {
int n = 0; int n = 0;
int sock = *it; int sock = *it;
while (n < len) { while (n < len) {
n = send(sock, buff+n, len-n, 0); n += write(sock, buff+n, len-n);
if (n < 0) { if (n < 0) {
perror("send"); perror("send");
close(sock); close(sock);
std::cout << "[INFO] Disconnected a monitor" << std::endl;
it = sockets.erase(it); it = sockets.erase(it);
return false; return false;
} }
@ -65,41 +67,23 @@ static bool transmit(std::list<int>::iterator it, char *buff, int len) {
void Monitor::tick(CNServer *serv, time_t delta) { void Monitor::tick(CNServer *serv, time_t delta) {
std::lock_guard<std::mutex> lock(sockLock); std::lock_guard<std::mutex> lock(sockLock);
char buff[256]; char buff[256];
int n;
auto it = sockets.begin(); auto it = sockets.begin();
while (it != sockets.end()) { while (it != sockets.end()) {
int sock = *it; if (!transmit(it, (char*)"begin\n", 6))
n = send(sock, "begin\n", 6, 0);
if (n < 0) {
perror("send");
close(sock);
it = sockets.erase(it);
continue; continue;
}
for (auto& pair : PlayerManager::players) { for (auto& pair : PlayerManager::players) {
n = std::snprintf(buff, sizeof(buff), "player %d %d %s\n", int n = std::snprintf(buff, sizeof(buff), "player %d %d %s\n",
pair.second->x, pair.second->y, pair.second->x, pair.second->y,
PlayerManager::getPlayerName(pair.second, false).c_str()); PlayerManager::getPlayerName(pair.second, false).c_str());
n = send(sock, buff, n, 0); if (!transmit(it, buff, n))
if (n < 0) {
perror("send");
close(sock);
it = sockets.erase(it);
continue; continue;
} }
}
n = send(sock, "end\n", 4, 0); if (!transmit(it, (char*)"end\n", 4))
if (n < 0) {
perror("send");
close(sock);
it = sockets.erase(it);
continue; continue;
}
it++; it++;
} }