formatting

This commit is contained in:
john30
2016-10-16 12:32:12 +02:00
parent 3683ef6a51
commit 29bac4b0b9
2 changed files with 48 additions and 54 deletions
+25 -28
View File
@@ -97,9 +97,9 @@ void Connection::run()
#else
#ifdef HAVE_PSELECT
// new data from notify
if (ret < 0 || FD_ISSET(notifyFD, &readfds) || FD_ISSET(notifyFD, &exceptfds))
if (ret < 0 || FD_ISSET(notifyFD, &readfds) || FD_ISSET(notifyFD, &exceptfds)) {
break;
}
// new data from socket
newData = FD_ISSET(sockFD, &readfds);
closed = FD_ISSET(sockFD, &exceptfds);
@@ -110,16 +110,16 @@ void Connection::run()
if (newData || message.isListening()) {
char data[256];
if (!m_socket->isValid())
if (!m_socket->isValid()) {
break;
}
if (newData) {
size_t datalen = m_socket->recv(data, sizeof(data)-1);
// remove closed socket
if (datalen <= 0)
if (datalen <= 0) {
break;
}
data[datalen] = '\0';
} else {
data[0] = '\0';
@@ -133,14 +133,15 @@ void Connection::run()
logDebug(lf_network, "[%05d] wait for result", getID());
string result = message.getResult();
if (!m_socket->isValid())
if (!m_socket->isValid()) {
break;
}
m_socket->send(result.c_str(), result.size());
}
if (message.isDisconnect() || !m_socket->isValid())
if (message.isDisconnect() || !m_socket->isValid()) {
break;
}
}
}
@@ -154,19 +155,17 @@ void Connection::run()
Network::Network(const bool local, const uint16_t port, const uint16_t httpPort, Queue<NetMessage*>* netQueue)
: Thread(), m_netQueue(netQueue), m_listening(false)
{
if (local)
m_tcpServer = new TCPServer(port, "127.0.0.1");
else
m_tcpServer = new TCPServer(port, "0.0.0.0");
m_tcpServer = new TCPServer(port, local ? "127.0.0.1" : "0.0.0.0");
if (m_tcpServer != NULL && m_tcpServer->start() == 0)
if (m_tcpServer != NULL && m_tcpServer->start() == 0) {
m_listening = true;
}
if (httpPort>0) {
m_httpServer = new TCPServer(httpPort, "0.0.0.0");
m_httpServer->start();
} else
} else {
m_httpServer = NULL;
}
}
Network::~Network()
@@ -182,17 +181,19 @@ Network::~Network()
}
join();
if (m_tcpServer != NULL)
if (m_tcpServer != NULL) {
delete m_tcpServer;
if (m_httpServer != NULL)
}
if (m_httpServer != NULL) {
delete m_httpServer;
}
}
void Network::run()
{
if (!m_listening)
if (!m_listening) {
return;
}
int ret;
struct timespec tdiff;
@@ -237,7 +238,6 @@ void Network::run()
#endif
while (true) {
#ifdef HAVE_PPOLL
// wait for new fd event
ret = ppoll(fds, nfds, &tdiff, NULL);
@@ -249,7 +249,6 @@ void Network::run()
ret = pselect(maxfd + 1, &readfds, NULL, NULL, &tdiff, NULL);
#endif
#endif
if (ret == 0) {
cleanConnections();
continue;
@@ -260,7 +259,6 @@ void Network::run()
if (fds[0].revents & POLLIN) {
return;
}
// new data from socket
if (fds[1].revents & POLLIN) {
newData = true;
@@ -273,7 +271,6 @@ void Network::run()
if (FD_ISSET(m_notify.notifyFD(), &readfds)) {
return;
}
// new data from socket
if (FD_ISSET(m_tcpServer->getFD(), &readfds)) {
newData = true;
@@ -284,13 +281,13 @@ void Network::run()
#endif
if (newData) {
TCPSocket* socket = (isHttp ? m_httpServer : m_tcpServer)->newSocket();
if (socket == NULL)
if (socket == NULL) {
continue;
}
Connection* connection = new Connection(socket, isHttp, m_netQueue);
if (connection == NULL)
if (connection == NULL) {
continue;
}
connection->start("connection");
m_connections.push_back(connection);
logInfo(lf_network, "[%05d] %s connection opened %s", connection->getID(), isHttp ? "HTTP" : "client", socket->getIP().c_str());
+23 -26
View File
@@ -35,10 +35,7 @@ TCPSocket::TCPSocket(int sfd, struct sockaddr_in* address) : m_sfd(sfd)
bool TCPSocket::isValid()
{
if (fcntl(m_sfd, F_GETFL) == -1)
return false;
else
return true;
return fcntl(m_sfd, F_GETFL) != -1;
}
@@ -53,37 +50,37 @@ TCPSocket* TCPClient::connect(const string& server, const uint16_t& port)
struct hostent* he;
he = gethostbyname(server.c_str());
if (he == NULL)
if (he == NULL) {
return NULL;
}
memcpy(&address.sin_addr, he->h_addr_list[0], he->h_length);
}
else {
} else {
ret = inet_aton(server.c_str(), &address.sin_addr);
if (ret == 0)
if (ret == 0) {
return NULL;
}
}
address.sin_family = AF_INET;
address.sin_port = (in_port_t)htons(port);
int sfd = socket(AF_INET, SOCK_STREAM, 0);
if (sfd < 0)
if (sfd < 0) {
return NULL;
}
ret = ::connect(sfd, (struct sockaddr*) &address, sizeof(address));
if (ret < 0)
if (ret < 0) {
return NULL;
}
return new TCPSocket(sfd, &address);
}
int TCPServer::start()
{
if (m_listening)
if (m_listening) {
return 0;
}
m_lfd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in address;
@@ -92,40 +89,40 @@ int TCPServer::start()
address.sin_family = AF_INET;
address.sin_port = (in_port_t)htons(m_port);
if (m_address.size() > 0)
if (m_address.size() > 0) {
inet_pton(AF_INET, m_address.c_str(), &(address.sin_addr));
else
} else {
address.sin_addr.s_addr = INADDR_ANY;
}
int optval = 1;
setsockopt(m_lfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
int result = bind(m_lfd, (struct sockaddr*) &address, sizeof(address));
if (result != 0)
if (result != 0) {
return result;
}
result = listen(m_lfd, 5);
if (result != 0)
if (result != 0) {
return result;
}
m_listening = true;
return result;
}
TCPSocket* TCPServer::newSocket()
{
if (!m_listening)
if (!m_listening) {
return NULL;
}
struct sockaddr_in address;
socklen_t len = sizeof(address);
memset(&address, 0, sizeof(address));
int sfd = accept(m_lfd, (struct sockaddr*) &address, &len);
if (sfd < 0)
if (sfd < 0) {
return NULL;
}
return new TCPSocket(sfd, &address);
}