optimized
This commit is contained in:
+30
-32
@@ -122,7 +122,7 @@ string fetchData(TCPSocket* socket, bool& listening)
|
|||||||
{
|
{
|
||||||
char data[1024];
|
char data[1024];
|
||||||
ssize_t datalen;
|
ssize_t datalen;
|
||||||
ostringstream ss;
|
ostringstream ostream;
|
||||||
string message;
|
string message;
|
||||||
|
|
||||||
int ret;
|
int ret;
|
||||||
@@ -191,45 +191,43 @@ string fetchData(TCPSocket* socket, bool& listening)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
if (newData) {
|
if (newData) {
|
||||||
if (socket->isValid()) {
|
if (socket->isValid()) {
|
||||||
datalen = socket->recv(data, sizeof(data));
|
datalen = socket->recv(data, sizeof(data));
|
||||||
|
|
||||||
if (datalen < 0) {
|
if (datalen < 0) {
|
||||||
perror("recv");
|
perror("recv");
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < datalen; i++)
|
|
||||||
ss << data[i];
|
|
||||||
|
|
||||||
if ((ss.str().length() >= 2
|
|
||||||
&& ss.str()[ss.str().length()-2] == '\n'
|
|
||||||
&& ss.str()[ss.str().length()-1] == '\n')
|
|
||||||
|| listening)
|
|
||||||
break;
|
|
||||||
|
|
||||||
}
|
|
||||||
else
|
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < datalen; i++)
|
||||||
|
ostream << data[i];
|
||||||
|
|
||||||
|
string str = ostream.str();
|
||||||
|
if (listening)
|
||||||
|
return str;
|
||||||
|
if (str.length() >= 2 && str[str.length()-2] == '\n' && str[str.length()-1] == '\n')
|
||||||
|
return str;
|
||||||
}
|
}
|
||||||
else if (newInput) {
|
else
|
||||||
getline(cin, message);
|
break;
|
||||||
message += '\n';
|
}
|
||||||
|
else if (newInput) {
|
||||||
|
getline(cin, message);
|
||||||
|
message += '\n';
|
||||||
|
|
||||||
socket->send(message.c_str(), message.size());
|
socket->send(message.c_str(), message.size());
|
||||||
|
|
||||||
if (strcasecmp(message.c_str(), "Q") == 0
|
if (strcasecmp(message.c_str(), "Q") == 0
|
||||||
|| strcasecmp(message.c_str(), "QUIT") == 0
|
|| strcasecmp(message.c_str(), "QUIT") == 0
|
||||||
|| strcasecmp(message.c_str(), "STOP") == 0)
|
|| strcasecmp(message.c_str(), "STOP") == 0)
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
|
|
||||||
message.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
message.clear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ss.str();
|
return ostream.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
void connect(const char* host, uint16_t port, char* const *args, int argCount)
|
void connect(const char* host, uint16_t port, char* const *args, int argCount)
|
||||||
|
|||||||
Reference in New Issue
Block a user