close server socket on exit

This commit is contained in:
Benjamin Sergeant 2019-01-24 21:16:32 -08:00
parent ea219e3ddd
commit 75d01c0c11
5 changed files with 31 additions and 27 deletions

View File

@ -71,9 +71,11 @@ namespace ix
(char*) &enable, sizeof(enable)) < 0) (char*) &enable, sizeof(enable)) < 0)
{ {
std::stringstream ss; std::stringstream ss;
ss << "SocketServer::listen() error calling setsockopt(SO_REUSEADDR): " ss << "SocketServer::listen() error calling setsockopt(SO_REUSEADDR) "
<< strerror(errno); << "at address " << _host << ":" << _port
<< " : " << strerror(Socket::getErrno());
::close(_serverFd);
return std::make_pair(false, ss.str()); return std::make_pair(false, ss.str());
} }
@ -93,21 +95,25 @@ namespace ix
if (bind(_serverFd, (struct sockaddr *)&server, sizeof(server)) < 0) if (bind(_serverFd, (struct sockaddr *)&server, sizeof(server)) < 0)
{ {
std::stringstream ss; std::stringstream ss;
ss << "SocketServer::listen() error calling bind: " ss << "SocketServer::listen() error calling bind "
<< strerror(Socket::getErrno()); << "at address " << _host << ":" << _port
<< " : " << strerror(Socket::getErrno());
::close(_serverFd);
return std::make_pair(false, ss.str()); return std::make_pair(false, ss.str());
} }
/* //
* Listen for connections. Specify the tcp backlog. // Listen for connections. Specify the tcp backlog.
*/ //
if (::listen(_serverFd, _backlog) != 0) if (::listen(_serverFd, _backlog) < 0)
{ {
std::stringstream ss; std::stringstream ss;
ss << "SocketServer::listen() error calling listen: " ss << "SocketServer::listen() error calling listen "
<< strerror(Socket::getErrno()); << "at address " << _host << ":" << _port
<< " : " << strerror(Socket::getErrno());
::close(_serverFd);
return std::make_pair(false, ss.str()); return std::make_pair(false, ss.str());
} }
@ -136,6 +142,7 @@ namespace ix
_stop = false; _stop = false;
_conditionVariable.notify_one(); _conditionVariable.notify_one();
::close(_serverFd);
} }
void SocketServer::run() void SocketServer::run()

View File

@ -162,7 +162,9 @@ namespace ix
{ {
if (pollResult == PollResultType_Timeout) if (pollResult == PollResultType_Timeout)
{ {
sendPing(kHeartBeatPingMessage); std::stringstream ss;
ss << kHeartBeatPingMessage << "::" << _heartBeatPeriod << "s";
sendPing(ss.str());
return; return;
} }

View File

@ -72,11 +72,11 @@ namespace
log(std::string("Connecting to url: ") + url); log(std::string("Connecting to url: ") + url);
_webSocket.setOnMessageCallback( _webSocket.setOnMessageCallback(
[this](ix::WebSocketMessageType messageType, [](ix::WebSocketMessageType messageType,
const std::string& str, const std::string& str,
size_t wireSize, size_t wireSize,
const ix::WebSocketErrorInfo& error, const ix::WebSocketErrorInfo& error,
const ix::WebSocketOpenInfo& openInfo, const ix::WebSocketOpenInfo& openInfo,
const ix::WebSocketCloseInfo& closeInfo) const ix::WebSocketCloseInfo& closeInfo)
{ {
std::stringstream ss; std::stringstream ss;

View File

@ -52,12 +52,12 @@ namespace
log(std::string("Connecting to url: ") + url); log(std::string("Connecting to url: ") + url);
_webSocket.setOnMessageCallback( _webSocket.setOnMessageCallback(
[this](ix::WebSocketMessageType messageType, [](ix::WebSocketMessageType messageType,
const std::string& str, const std::string& str,
size_t wireSize, size_t wireSize,
const ix::WebSocketErrorInfo& error, const ix::WebSocketErrorInfo& error,
const ix::WebSocketOpenInfo& openInfo, const ix::WebSocketOpenInfo& openInfo,
const ix::WebSocketCloseInfo& closeInfo) const ix::WebSocketCloseInfo& closeInfo)
{ {
std::stringstream ss; std::stringstream ss;
if (messageType == ix::WebSocket_MessageType_Open) if (messageType == ix::WebSocket_MessageType_Open)

View File

@ -4,11 +4,6 @@
* Copyright (c) 2017 Machine Zone. All rights reserved. * Copyright (c) 2017 Machine Zone. All rights reserved.
*/ */
//
// Simple chat program that talks to the node.js server at
// websocket_chat_server/broacast-server.js
//
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <queue> #include <queue>