use send/recv to replace write/read

This commit is contained in:
heliclei 2014-02-24 12:01:04 +08:00
parent 92571ebe04
commit be45047b6b
4 changed files with 32 additions and 53 deletions

View File

@ -112,14 +112,14 @@ static ssize_t mydprintf(int sock, const char *format, ...)
vsnprintf(buf, sizeof(buf), format, args);
va_end(args);
auto console = Director::getInstance()->getConsole();
return console->socketWrite(sock, buf, strlen(buf));
return send(sock, buf, strlen(buf),0);
}
static void sendPrompt(int fd)
{
const char prompt[] = "> ";
auto console = Director::getInstance()->getConsole();
console->socketWrite(fd, prompt, sizeof(prompt));
send(fd, prompt, sizeof(prompt),0);
}
static int printSceneGraph(int fd, Node* node, int level)
@ -127,7 +127,7 @@ static int printSceneGraph(int fd, Node* node, int level)
int total = 1;
auto console = Director::getInstance()->getConsole();
for(int i=0; i<level; ++i)
console->socketWrite(fd, "-", 1);
send(fd, "-", 1,0);
mydprintf(fd, " %s\n", node->getDescription().c_str());
@ -140,7 +140,7 @@ static int printSceneGraph(int fd, Node* node, int level)
static void printSceneGraphBoot(int fd)
{
auto console = Director::getInstance()->getConsole();
console->socketWrite(fd,"\n",1);
send(fd,"\n",1,0);
auto scene = Director::getInstance()->getRunningScene();
int total = printSceneGraph(fd, scene, 0);
mydprintf(fd, "Total Nodes: %d\n", total);
@ -293,23 +293,6 @@ Console::~Console()
stop();
}
ssize_t Console::socketWrite(int fd, const char* buf, size_t len)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
return send(fd, buf, len , 0);
#else
return write(fd, buf, len);
#endif
}
ssize_t Console::socketRead(int fd, char* buf, size_t len)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
return recv(fd, buf, len , 0);
#else
return read(fd, buf, len);
#endif
}
bool Console::listenOnTCP(int port)
{
@ -413,7 +396,7 @@ void Console::commandHelp(int fd, const std::string &args)
{
const char help[] = "\nAvailable commands:\n";
auto console = Director::getInstance()->getConsole();
console->socketWrite(fd, help, sizeof(help));
send(fd, help, sizeof(help),0);
for(auto it=_commands.begin();it!=_commands.end();++it)
{
auto cmd = it->second;
@ -627,7 +610,7 @@ bool Console::parseCommand(int fd)
{
const char err[] = "Unknown error!\n";
sendPrompt(fd);
console->socketWrite(fd, err, sizeof(err));
send(fd, err, sizeof(err),0);
return false;
}
std::string cmdLine;
@ -639,7 +622,7 @@ bool Console::parseCommand(int fd)
if(args.empty())
{
const char err[] = "Unknown command. Type 'help' for options\n";
console->socketWrite(fd, err, sizeof(err));
send(fd, err, sizeof(err),0);
sendPrompt(fd);
return false;
}
@ -661,7 +644,7 @@ bool Console::parseCommand(int fd)
cmd.callback(fd, args2);
}else if(strcmp(buf, "\r\n") != 0) {
const char err[] = "Unknown command. Type 'help' for options\n";
console->socketWrite(fd, err, sizeof(err));
send(fd, err, sizeof(err),0);
}
sendPrompt(fd);
@ -681,7 +664,7 @@ ssize_t Console::readline(int fd, char* ptr, int maxlen)
auto console = Director::getInstance()->getConsole();
for( n=1; n<maxlen-1; n++ ) {
if( (rc = console->socketRead(fd, &c, 1 )) ==1 ) {
if( (rc = recv(fd, &c, 1, 0)) ==1 ) {
*ptr++ = c;
if(c == '\n') {
break;
@ -798,7 +781,7 @@ void Console::loop()
_DebugStringsMutex.lock();
for(const auto &str : _DebugStrings) {
for(const auto &fd : _fds) {
console->socketWrite(fd, str.c_str(), str.length());
send(fd, str.c_str(), str.length(),0);
}
}
_DebugStrings.clear();

View File

@ -96,12 +96,7 @@ public:
void addCommand(const Command& cmd);
/** log something in the console */
void log(const char *buf);
/** write to socket */
ssize_t socketWrite(int fd, const char* buf, size_t len);
/** read from socket */
ssize_t socketRead(int fd, char* buf, size_t len);
protected:
void loop();
ssize_t readline(int fd, char *buf, int maxlen);

View File

@ -27,6 +27,7 @@
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
#include <unistd.h>
#include <sys/socket.h>
#else
#include <io.h>
#endif
@ -170,9 +171,9 @@ ConsoleCustomCommand::ConsoleCustomCommand()
const char msg[] = "how are you?\nArguments passed: ";
auto _console = Director::getInstance()->getConsole();
_console->socketWrite(fd, msg, sizeof(msg));
_console->socketWrite(fd, args.c_str(), args.length());
_console->socketWrite(fd, "\n",1);
send(fd, msg, sizeof(msg),0);
send(fd, args.c_str(), args.length(),0);
send(fd, "\n",1,0);
}},
};
_console->addCommand(commands[0]);

View File

@ -13,6 +13,7 @@
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
#include <unistd.h>
#include <sys/socket.h>
#else
#include <io.h>
#endif
@ -250,29 +251,28 @@ void TestController::addConsoleAutoTest()
[](int fd, const std::string& args)
{
Scheduler *sched = Director::getInstance()->getScheduler();
auto _console = Director::getInstance()->getConsole();
if(args == "help" || args == "-h")
{
const char msg[] = "usage: autotest ActionsTest\n\tavailable tests: ";
_console->socketWrite(fd, msg, sizeof(msg));
_console->socketWrite(fd, "\n",1);
send(fd, msg, sizeof(msg),0);
send(fd, "\n",1,0);
for(int i = 0; i < g_testCount; i++)
{
_console->socketWrite(fd, "\t",1);
_console->socketWrite(fd, g_aTestNames[i].test_name, strlen(g_aTestNames[i].test_name)+1);
_console->socketWrite(fd, "\n",1);
send(fd, "\t",1,0);
send(fd, g_aTestNames[i].test_name, strlen(g_aTestNames[i].test_name)+1,0);
send(fd, "\n",1,0);
}
const char help_main[] = "\tmain, return to main menu\n";
_console->socketWrite(fd, help_main, sizeof(help_main));
send(fd, help_main, sizeof(help_main),0);
const char help_next[] = "\tnext, run next test\n";
_console->socketWrite(fd, help_next, sizeof(help_next));
send(fd, help_next, sizeof(help_next),0);
const char help_back[] = "\tback, run prev test\n";
_console->socketWrite(fd, help_back, sizeof(help_back));
send(fd, help_back, sizeof(help_back),0);
const char help_restart[] = "\trestart, restart current test\n";
_console->socketWrite(fd, help_restart, sizeof(help_restart));
send(fd, help_restart, sizeof(help_restart),0);
return;
}
if(args == "main")
@ -303,7 +303,7 @@ void TestController::addConsoleAutoTest()
}
else
{
_console->socketWrite(fd, msg_notest, sizeof(msg_notest));
send(fd, msg_notest, sizeof(msg_notest),0);
}
return;
}
@ -317,7 +317,7 @@ void TestController::addConsoleAutoTest()
}
else
{
_console->socketWrite(fd, msg_notest, sizeof(msg_notest));
send(fd, msg_notest, sizeof(msg_notest),0);
}
return;
}
@ -332,7 +332,7 @@ void TestController::addConsoleAutoTest()
}
else
{
_console->socketWrite(fd, msg_notest, sizeof(msg_notest));
send(fd, msg_notest, sizeof(msg_notest),0);
}
return;
}
@ -348,8 +348,8 @@ void TestController::addConsoleAutoTest()
{
std::string msg("autotest: running test:");
msg += args;
_console->socketWrite(fd, msg.c_str(), strlen(msg.c_str()));
_console->socketWrite(fd, "\n",1);
send(fd, msg.c_str(), strlen(msg.c_str()),0);
send(fd, "\n",1,0);
currentController = &g_aTestNames[i];
sched->performFunctionInCocosThread( [&](){
@ -364,8 +364,8 @@ void TestController::addConsoleAutoTest()
//no match found,print warning message
std::string msg("autotest: could not find test:");
msg += args;
_console->socketWrite(fd, msg.c_str(), strlen(msg.c_str()));
_console->socketWrite(fd, "\n",1);
send(fd, msg.c_str(), strlen(msg.c_str()),0);
send(fd, "\n",1,0);
}
};