Receive and send messages

This commit is contained in:
Torsten Schulz
2017-07-21 21:49:19 +02:00
parent 922ee7ac12
commit b5b4c94f65
10 changed files with 144 additions and 44 deletions

View File

@@ -6,7 +6,6 @@
#include <thread>
#include <future>
#include <functional>
#include <strings.h>
#include <unistd.h>
#include <json/json.h>
#include <iostream>
@@ -91,15 +90,7 @@ namespace Yc {
}
int flags = 1;
setsockopt(userSock, IPPROTO_TCP, TCP_NODELAY, (void *)&flags, sizeof(flags));
std::string msg("");
char buffer[256];
bzero(buffer, 256);
while (int received = recv(userSock, buffer, 255, 0) > 0) {
msg += std::string(buffer);
if (received < 255) {
break;
}
}
std::string msg = readSocket(userSock);
if (msg == "") {
return;
}
@@ -107,11 +98,7 @@ namespace Yc {
}
void Server::inputSwitcher(int userSocket, std::string input) {
Json::Value inputTree;
Json::CharReaderBuilder rbuilder;
std::unique_ptr<Json::CharReader> const reader(rbuilder.newCharReader());
JSONCPP_STRING inputJsonString(input);
reader->parse(inputJsonString.data(), inputJsonString.data() + inputJsonString.size(), &inputTree, NULL);
Json::Value inputTree = getJsonTree(input);
if (inputTree["type"] == "init") {
initUser(userSocket, inputTree);
}
@@ -128,6 +115,10 @@ namespace Yc {
void Server::initUser(int userSocket, Json::Value data) {
if (userExists(data["name"].asString())) {
Json::Value errorJson;
errorJson["type"] = User::error;
errorJson["message"] = "loggedin";
send(userSocket, errorJson);
close(userSocket);
return;
}