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