2013-12-05 08:26:21 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 cocos2d-x.org
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2013-11-27 10:58:36 +08:00
|
|
|
|
2013-12-05 11:33:50 +08:00
|
|
|
#include "CCConsole.h"
|
2013-11-27 10:58:36 +08:00
|
|
|
|
2013-12-04 10:46:54 +08:00
|
|
|
#include <thread>
|
2013-12-05 10:28:09 +08:00
|
|
|
#include <algorithm>
|
2013-12-04 10:46:54 +08:00
|
|
|
|
2013-11-27 10:58:36 +08:00
|
|
|
#include <stdio.h>
|
2013-12-05 04:26:43 +08:00
|
|
|
#include <time.h>
|
|
|
|
#include <fcntl.h>
|
2013-12-05 16:09:38 +08:00
|
|
|
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#include <io.h>
|
|
|
|
#include <WS2tcpip.h>
|
|
|
|
|
|
|
|
#define bzero(a, b) memset(a, 0, b);
|
|
|
|
|
|
|
|
#else
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <unistd.h>
|
2013-12-05 04:26:43 +08:00
|
|
|
#include <arpa/inet.h>
|
2013-12-04 10:46:54 +08:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
2013-12-05 16:09:38 +08:00
|
|
|
#endif
|
2013-11-27 10:58:36 +08:00
|
|
|
|
|
|
|
#include "CCDirector.h"
|
2013-12-04 10:46:54 +08:00
|
|
|
#include "CCScheduler.h"
|
2013-12-05 08:57:44 +08:00
|
|
|
#include "CCScene.h"
|
|
|
|
|
2013-12-05 16:09:38 +08:00
|
|
|
NS_CC_BEGIN
|
2013-11-27 10:58:36 +08:00
|
|
|
|
2013-12-05 13:51:08 +08:00
|
|
|
|
|
|
|
// helper free functions
|
|
|
|
|
|
|
|
// dprintf() is not defined in Android
|
|
|
|
// so we add our own 'dpritnf'
|
|
|
|
static ssize_t mydprintf(int sock, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
char buf[1024];
|
|
|
|
|
|
|
|
va_start(args, format);
|
|
|
|
vsnprintf(buf, sizeof(buf), format, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
return write(sock, buf, strlen(buf));
|
|
|
|
}
|
|
|
|
|
2013-12-13 06:30:22 +08:00
|
|
|
static int printSceneGraph(int fd, Node* node, int level)
|
2013-12-05 13:51:08 +08:00
|
|
|
{
|
2013-12-13 06:30:22 +08:00
|
|
|
int total = 1;
|
2013-12-05 13:51:08 +08:00
|
|
|
for(int i=0; i<level; ++i)
|
|
|
|
write(fd, "-", 1);
|
|
|
|
|
2013-12-13 06:30:22 +08:00
|
|
|
mydprintf(fd, " %s\n", node->getDescription().c_str());
|
2013-12-05 13:51:08 +08:00
|
|
|
|
2013-12-05 14:07:23 +08:00
|
|
|
for(const auto& child: node->getChildren())
|
2013-12-13 06:30:22 +08:00
|
|
|
total += printSceneGraph(fd, child, level+1);
|
|
|
|
|
|
|
|
return total;
|
2013-12-05 13:51:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void printSceneGraphBoot(int fd)
|
|
|
|
{
|
|
|
|
write(fd,"\n",1);
|
|
|
|
auto scene = Director::getInstance()->getRunningScene();
|
2013-12-13 06:30:22 +08:00
|
|
|
int total = printSceneGraph(fd, scene, 0);
|
|
|
|
mydprintf(fd, "Total Nodes: %d\n", total);
|
2013-12-05 13:51:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Console code
|
|
|
|
//
|
|
|
|
|
2013-12-04 10:46:54 +08:00
|
|
|
Console* Console::create()
|
2013-11-27 10:58:36 +08:00
|
|
|
{
|
|
|
|
auto ret = new Console;
|
|
|
|
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
Console::Console()
|
2013-12-04 10:46:54 +08:00
|
|
|
: _listenfd(-1)
|
2013-11-27 10:58:36 +08:00
|
|
|
, _running(false)
|
|
|
|
, _endThread(false)
|
2013-12-05 16:09:38 +08:00
|
|
|
, _maxCommands(5)
|
|
|
|
, _userCommands(nullptr)
|
|
|
|
, _maxUserCommands(0)
|
|
|
|
{
|
2013-12-05 16:49:05 +08:00
|
|
|
// VS2012 doesn't support initializer list, so we create a new array and assign its elements to '_command'.
|
2013-12-05 16:09:38 +08:00
|
|
|
Command commands[] = {
|
2013-12-05 16:26:04 +08:00
|
|
|
{ "fps on", [](int fd, const char* command) {
|
2013-12-05 08:26:21 +08:00
|
|
|
Director *dir = Director::getInstance();
|
|
|
|
Scheduler *sched = dir->getScheduler();
|
|
|
|
sched->performFunctionInCocosThread( std::bind(&Director::setDisplayStats, dir, true));
|
|
|
|
} },
|
2013-12-05 13:51:08 +08:00
|
|
|
{ "fps off", [](int fd, const char* command) {
|
2013-12-05 08:26:21 +08:00
|
|
|
Director *dir = Director::getInstance();
|
|
|
|
Scheduler *sched = dir->getScheduler();
|
|
|
|
sched->performFunctionInCocosThread( std::bind(&Director::setDisplayStats, dir, false));
|
|
|
|
} },
|
2013-12-05 16:33:13 +08:00
|
|
|
{ "scene graph", std::bind(&Console::commandSceneGraph, this, std::placeholders::_1, std::placeholders::_2) },
|
|
|
|
{ "exit", std::bind(&Console::commandExit, this, std::placeholders::_1, std::placeholders::_2) },
|
|
|
|
{ "help", std::bind(&Console::commandHelp, this, std::placeholders::_1, std::placeholders::_2) } };
|
2013-12-05 16:09:38 +08:00
|
|
|
|
|
|
|
for (size_t i = 0; i < sizeof(commands)/sizeof(commands[0]) && i < sizeof(_commands)/sizeof(_commands[0]); ++i)
|
|
|
|
{
|
|
|
|
_commands[i] = commands[i];
|
|
|
|
}
|
2013-12-05 09:38:11 +08:00
|
|
|
}
|
2013-11-27 10:58:36 +08:00
|
|
|
|
|
|
|
Console::~Console()
|
|
|
|
{
|
2013-12-05 08:26:21 +08:00
|
|
|
cancel();
|
2013-11-27 10:58:36 +08:00
|
|
|
}
|
|
|
|
|
2013-12-04 10:46:54 +08:00
|
|
|
bool Console::listenOnTCP(int port)
|
|
|
|
{
|
|
|
|
int listenfd, n;
|
|
|
|
const int on = 1;
|
2013-12-05 10:19:51 +08:00
|
|
|
struct addrinfo hints, *res, *ressave;
|
2013-12-04 10:46:54 +08:00
|
|
|
char serv[30];
|
|
|
|
|
2013-12-05 13:51:08 +08:00
|
|
|
snprintf(serv, sizeof(serv)-1, "%d", port );
|
2013-12-04 10:46:54 +08:00
|
|
|
serv[sizeof(serv)-1]=0;
|
|
|
|
|
|
|
|
bzero(&hints, sizeof(struct addrinfo));
|
|
|
|
hints.ai_flags = AI_PASSIVE;
|
2013-12-05 13:51:08 +08:00
|
|
|
hints.ai_family = AF_INET; // AF_UNSPEC: Do we need IPv6 ?
|
2013-12-04 10:46:54 +08:00
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
|
2013-12-05 16:09:38 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
|
|
|
|
WSADATA wsaData;
|
|
|
|
n = WSAStartup(MAKEWORD(2, 2),&wsaData);
|
|
|
|
#endif
|
|
|
|
|
2013-12-04 10:46:54 +08:00
|
|
|
if ( (n = getaddrinfo(NULL, serv, &hints, &res)) != 0) {
|
|
|
|
fprintf(stderr,"net_listen error for %s: %s", serv, gai_strerror(n));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ressave = res;
|
|
|
|
|
|
|
|
do {
|
|
|
|
listenfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
|
|
|
if (listenfd < 0)
|
2013-12-05 10:19:51 +08:00
|
|
|
continue; /* error, try next one */
|
2013-12-04 10:46:54 +08:00
|
|
|
|
2013-12-05 16:09:38 +08:00
|
|
|
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
|
2013-12-04 10:46:54 +08:00
|
|
|
if (bind(listenfd, res->ai_addr, res->ai_addrlen) == 0)
|
2013-12-05 10:19:51 +08:00
|
|
|
break; /* success */
|
2013-12-04 10:46:54 +08:00
|
|
|
|
2013-12-05 10:19:51 +08:00
|
|
|
close(listenfd); /* bind error, close and try next one */
|
2013-12-04 10:46:54 +08:00
|
|
|
} while ( (res = res->ai_next) != NULL);
|
|
|
|
|
|
|
|
if (res == NULL) {
|
|
|
|
perror("net_listen:");
|
|
|
|
freeaddrinfo(ressave);
|
|
|
|
return false;
|
|
|
|
}
|
2013-12-05 08:26:21 +08:00
|
|
|
|
2013-12-04 10:46:54 +08:00
|
|
|
listen(listenfd, 50);
|
2013-12-05 08:26:21 +08:00
|
|
|
|
|
|
|
if (res->ai_family == AF_INET) {
|
|
|
|
char buf[INET_ADDRSTRLEN] = "";
|
|
|
|
struct sockaddr_in *sin = (struct sockaddr_in*) res->ai_addr;
|
2013-12-05 13:51:08 +08:00
|
|
|
if( inet_ntop(res->ai_family, &sin->sin_addr, buf, sizeof(buf)) != NULL )
|
|
|
|
log("Console: listening on %s : %d", buf, ntohs(sin->sin_port));
|
|
|
|
else
|
|
|
|
perror("inet_ntop");
|
|
|
|
} else if (res->ai_family == AF_INET6) {
|
|
|
|
char buf[INET6_ADDRSTRLEN] = "";
|
|
|
|
struct sockaddr_in6 *sin = (struct sockaddr_in6*) res->ai_addr;
|
|
|
|
if( inet_ntop(res->ai_family, &sin->sin6_addr, buf, sizeof(buf)) != NULL )
|
|
|
|
log("Console: listening on %s : %d", buf, ntohs(sin->sin6_port));
|
2013-12-05 08:26:21 +08:00
|
|
|
else
|
|
|
|
perror("inet_ntop");
|
|
|
|
}
|
|
|
|
|
2013-12-05 13:51:08 +08:00
|
|
|
|
2013-12-04 10:46:54 +08:00
|
|
|
freeaddrinfo(ressave);
|
|
|
|
|
|
|
|
return listenOnFileDescriptor(listenfd);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Console::listenOnFileDescriptor(int fd)
|
2013-11-27 10:58:36 +08:00
|
|
|
{
|
|
|
|
CCASSERT(!_running, "already running");
|
2013-12-04 10:46:54 +08:00
|
|
|
_listenfd = fd;
|
2013-11-27 10:58:36 +08:00
|
|
|
_thread = std::thread( std::bind( &Console::loop, this) );
|
2013-12-04 10:46:54 +08:00
|
|
|
|
|
|
|
return true;
|
2013-11-27 10:58:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Console::cancel()
|
|
|
|
{
|
2013-12-04 10:46:54 +08:00
|
|
|
if( _running ) {
|
|
|
|
_endThread = true;
|
|
|
|
_thread.join();
|
|
|
|
}
|
2013-11-27 10:58:36 +08:00
|
|
|
}
|
|
|
|
|
2013-12-05 09:38:11 +08:00
|
|
|
void Console::setUserCommands(Command *commands, int numberOfCommands)
|
|
|
|
{
|
|
|
|
_userCommands = commands;
|
|
|
|
_maxUserCommands = numberOfCommands;
|
|
|
|
}
|
|
|
|
|
2013-12-05 08:26:21 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
// commands
|
|
|
|
//
|
|
|
|
|
2013-12-05 13:51:08 +08:00
|
|
|
void Console::commandHelp(int fd, const char* command)
|
2013-12-04 10:46:54 +08:00
|
|
|
{
|
2013-12-05 08:26:21 +08:00
|
|
|
const char help[] = "\nAvailable commands:\n";
|
|
|
|
write(fd, help, sizeof(help));
|
2013-12-05 09:38:11 +08:00
|
|
|
for(int i=0; i<_maxCommands; ++i) {
|
|
|
|
write(fd,"\t",1);
|
|
|
|
write(fd, _commands[i].name, strlen(_commands[i].name));
|
|
|
|
write(fd,"\n",1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// User commands
|
|
|
|
for(int i=0; i<_maxUserCommands; ++i) {
|
2013-12-05 08:26:21 +08:00
|
|
|
write(fd,"\t",1);
|
2013-12-05 09:38:11 +08:00
|
|
|
write(fd, _userCommands[i].name, strlen(_userCommands[i].name));
|
2013-12-05 08:26:21 +08:00
|
|
|
write(fd,"\n",1);
|
2013-12-04 10:46:54 +08:00
|
|
|
}
|
2013-12-05 09:38:11 +08:00
|
|
|
|
2013-12-05 08:26:21 +08:00
|
|
|
}
|
2013-12-04 10:46:54 +08:00
|
|
|
|
2013-12-05 13:51:08 +08:00
|
|
|
void Console::commandExit(int fd, const char *command)
|
2013-12-05 08:26:21 +08:00
|
|
|
{
|
|
|
|
FD_CLR(fd, &_read_set);
|
|
|
|
_fds.erase(std::remove(_fds.begin(), _fds.end(), fd), _fds.end());
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2013-12-05 13:51:08 +08:00
|
|
|
void Console::commandSceneGraph(int fd, const char *command)
|
2013-12-05 08:57:44 +08:00
|
|
|
{
|
|
|
|
Scheduler *sched = Director::getInstance()->getScheduler();
|
|
|
|
sched->performFunctionInCocosThread( std::bind(&printSceneGraphBoot, fd) );
|
|
|
|
}
|
|
|
|
|
2013-12-05 09:38:11 +08:00
|
|
|
bool Console::parseCommand(int fd)
|
2013-12-05 08:26:21 +08:00
|
|
|
{
|
|
|
|
auto r = readline(fd);
|
|
|
|
if(r < 1)
|
|
|
|
return false;
|
|
|
|
|
2013-12-05 09:38:11 +08:00
|
|
|
bool found=false;
|
|
|
|
for(int i=0; i < _maxCommands; ++i) {
|
|
|
|
if( strncmp(_buffer, _commands[i].name,strlen(_commands[i].name)) == 0 ) {
|
2013-12-05 08:26:21 +08:00
|
|
|
// XXX TODO FIXME
|
|
|
|
// Ideally this loop should execute the function in the cocos2d according to a variable
|
|
|
|
// But clang crashes in runtime when doing that (bug in clang, not in the code).
|
|
|
|
// So, unfortunately, the only way to fix it was to move that logic to the callback itself
|
2013-12-05 13:51:08 +08:00
|
|
|
_commands[i].callback(fd, _buffer);
|
2013-12-05 09:38:11 +08:00
|
|
|
found = true;
|
2013-12-05 08:26:21 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-12-05 09:38:11 +08:00
|
|
|
|
|
|
|
// user commands
|
|
|
|
for(int i=0; i < _maxUserCommands && !found; ++i) {
|
|
|
|
if( strncmp(_buffer, _userCommands[i].name,strlen(_userCommands[i].name)) == 0 ) {
|
2013-12-05 13:51:08 +08:00
|
|
|
_userCommands[i].callback(fd, _buffer);
|
2013-12-05 09:38:11 +08:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!found) {
|
2013-12-05 08:26:21 +08:00
|
|
|
const char err[] = "Unknown command. Type 'help' for options\n";
|
|
|
|
write(fd, err, sizeof(err));
|
|
|
|
}
|
|
|
|
|
|
|
|
sendPrompt(fd);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Helpers
|
|
|
|
//
|
|
|
|
void Console::sendPrompt(int fd)
|
|
|
|
{
|
|
|
|
const char prompt[] = "\n> ";
|
|
|
|
write(fd, prompt, sizeof(prompt));
|
2013-12-04 10:46:54 +08:00
|
|
|
}
|
2013-11-27 10:58:36 +08:00
|
|
|
|
2013-12-04 10:46:54 +08:00
|
|
|
ssize_t Console::readline(int fd)
|
2013-12-03 01:57:16 +08:00
|
|
|
{
|
2013-12-05 08:26:21 +08:00
|
|
|
int maxlen = sizeof(_buffer)-1;
|
2013-12-05 10:19:51 +08:00
|
|
|
ssize_t n, rc;
|
|
|
|
char c, *ptr;
|
2013-12-03 01:57:16 +08:00
|
|
|
|
2013-12-04 10:46:54 +08:00
|
|
|
ptr = _buffer;
|
2013-12-03 01:57:16 +08:00
|
|
|
|
2013-12-05 10:19:51 +08:00
|
|
|
for( n=1; n<maxlen; n++ ) {
|
|
|
|
if( (rc = read(fd, &c, 1 )) ==1 ) {
|
|
|
|
*ptr++ = c;
|
|
|
|
if( c=='\n' )
|
|
|
|
break;
|
|
|
|
} else if( rc == 0 ) {
|
|
|
|
return 0;
|
|
|
|
} else if( errno == EINTR ) {
|
2013-12-03 01:57:16 +08:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
2013-12-05 10:19:51 +08:00
|
|
|
}
|
2013-12-03 01:57:16 +08:00
|
|
|
|
2013-12-05 10:19:51 +08:00
|
|
|
*ptr = 0;
|
|
|
|
return n;
|
2013-12-03 01:57:16 +08:00
|
|
|
}
|
|
|
|
|
2013-12-05 08:26:21 +08:00
|
|
|
void Console::addClient()
|
2013-11-27 10:58:36 +08:00
|
|
|
{
|
2013-12-05 08:26:21 +08:00
|
|
|
struct sockaddr client;
|
2013-12-05 10:19:51 +08:00
|
|
|
socklen_t client_len;
|
2013-12-05 08:26:21 +08:00
|
|
|
|
|
|
|
/* new client */
|
|
|
|
client_len = sizeof( client );
|
|
|
|
int fd = accept(_listenfd, (struct sockaddr *)&client, &client_len );
|
|
|
|
|
|
|
|
// add fd to list of FD
|
|
|
|
if( fd != -1 ) {
|
|
|
|
FD_SET(fd, &_read_set);
|
|
|
|
_fds.push_back(fd);
|
|
|
|
_maxfd = std::max(_maxfd,fd);
|
|
|
|
|
|
|
|
sendPrompt(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Main Loop
|
|
|
|
//
|
|
|
|
|
|
|
|
void Console::loop()
|
|
|
|
{
|
|
|
|
fd_set copy_set;
|
2013-12-05 10:19:51 +08:00
|
|
|
struct timeval timeout, timeout_copy;
|
2013-11-27 10:58:36 +08:00
|
|
|
|
2013-12-05 08:26:21 +08:00
|
|
|
_running = true;
|
|
|
|
|
|
|
|
FD_ZERO(&_read_set);
|
|
|
|
FD_SET(_listenfd, &_read_set);
|
2013-12-05 04:26:43 +08:00
|
|
|
_maxfd = _listenfd;
|
2013-12-04 10:46:54 +08:00
|
|
|
|
2013-12-05 10:19:51 +08:00
|
|
|
timeout.tv_sec = 0;
|
2013-12-05 04:26:43 +08:00
|
|
|
|
|
|
|
/* 0.016 seconds. Wake up once per frame at 60PFS */
|
2013-12-05 10:19:51 +08:00
|
|
|
timeout.tv_usec = 016000;
|
2013-11-27 10:58:36 +08:00
|
|
|
|
2013-12-05 10:19:51 +08:00
|
|
|
while(!_endThread) {
|
2013-11-27 10:58:36 +08:00
|
|
|
|
2013-12-05 10:19:51 +08:00
|
|
|
copy_set = _read_set;
|
2013-12-04 10:46:54 +08:00
|
|
|
timeout_copy = timeout;
|
2013-12-05 10:19:51 +08:00
|
|
|
int nready = select(_maxfd+1, ©_set, NULL, NULL, &timeout_copy);
|
2013-11-27 10:58:36 +08:00
|
|
|
|
2013-12-05 10:19:51 +08:00
|
|
|
if( nready == -1 ) {
|
2013-12-03 01:57:16 +08:00
|
|
|
/* error ?*/
|
2013-12-05 10:19:51 +08:00
|
|
|
if(errno != EINTR)
|
|
|
|
log("Abnormal error in select()\n");
|
|
|
|
continue;
|
2013-11-27 10:58:36 +08:00
|
|
|
|
2013-12-05 10:19:51 +08:00
|
|
|
} else if( nready == 0 ) {
|
2013-11-27 10:58:36 +08:00
|
|
|
/* timeout ? */
|
2013-12-05 10:19:51 +08:00
|
|
|
continue;
|
2013-12-04 10:46:54 +08:00
|
|
|
}
|
|
|
|
|
2013-12-05 08:26:21 +08:00
|
|
|
// new client
|
2013-12-04 10:46:54 +08:00
|
|
|
if(FD_ISSET(_listenfd, ©_set)) {
|
2013-12-05 08:26:21 +08:00
|
|
|
addClient();
|
2013-12-05 10:19:51 +08:00
|
|
|
if(--nready <= 0)
|
|
|
|
continue;
|
2013-12-04 10:46:54 +08:00
|
|
|
}
|
|
|
|
|
2013-12-05 08:26:21 +08:00
|
|
|
// data from client
|
|
|
|
std::vector<int> to_remove;
|
2013-12-05 04:26:43 +08:00
|
|
|
for(const auto &fd: _fds) {
|
|
|
|
if(FD_ISSET(fd,©_set)) {
|
2013-12-05 09:38:11 +08:00
|
|
|
if( ! parseCommand(fd) ) {
|
2013-12-05 08:26:21 +08:00
|
|
|
to_remove.push_back(fd);
|
|
|
|
}
|
2013-12-04 10:46:54 +08:00
|
|
|
if(--nready <= 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-12-05 08:26:21 +08:00
|
|
|
|
|
|
|
// remove closed conections
|
2013-12-05 10:19:51 +08:00
|
|
|
for(int fd: to_remove) {
|
2013-12-05 08:26:21 +08:00
|
|
|
FD_CLR(fd, &_read_set);
|
|
|
|
_fds.erase(std::remove(_fds.begin(), _fds.end(), fd), _fds.end());
|
|
|
|
}
|
2013-12-05 04:26:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// clean up: ignore stdin, stdout and stderr
|
|
|
|
for(const auto &fd: _fds )
|
|
|
|
close(fd);
|
|
|
|
close(_listenfd);
|
2013-12-05 08:26:21 +08:00
|
|
|
|
|
|
|
_running = false;
|
2013-11-27 10:58:36 +08:00
|
|
|
}
|
2013-12-05 16:09:38 +08:00
|
|
|
|
|
|
|
NS_CC_END
|