Initial submit
This commit is contained in:
89
user.cpp
89
user.cpp
@@ -1,12 +1,91 @@
|
||||
#include "user.h"
|
||||
|
||||
#include <tools.h>
|
||||
#include <json/json.h>
|
||||
#include <unistd.h>
|
||||
#include <future>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include "room.h"
|
||||
#include <iostream>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <sstream>
|
||||
|
||||
namespace Yc {
|
||||
namespace Lib {
|
||||
namespace Lib {
|
||||
|
||||
User::User()
|
||||
{
|
||||
User::User(Room *parent, std::string name, std::string color, int socket) :
|
||||
_parent(parent),
|
||||
_name(name),
|
||||
_color(color),
|
||||
_socket(socket),
|
||||
_stop(false) {
|
||||
_token = Yc::Lib::Tools::generateRandomString(32);
|
||||
sendMsg(token, _token, "", "");
|
||||
std::async(std::bind(&User::checkerTask, this));
|
||||
}
|
||||
|
||||
}
|
||||
std::string User::name() const {
|
||||
return _name;
|
||||
}
|
||||
|
||||
} // namespace Lib
|
||||
bool User::validateToken(std::string token) {
|
||||
return (token == _token);
|
||||
}
|
||||
|
||||
bool User::isUser(User *toValidate) {
|
||||
return (toValidate == this);
|
||||
}
|
||||
|
||||
void User::sendMsg(MsgType type, std::string message , std::string userName, std::string color) {
|
||||
Json::Value sendMessage;
|
||||
sendMessage["type"] = type;
|
||||
sendMessage["message"] = message;
|
||||
sendMessage["userName"] = userName;
|
||||
sendMessage["color"] = color;
|
||||
std::string outString;
|
||||
std::stringstream outStream;
|
||||
outStream << sendMessage;
|
||||
outString = outStream.str();
|
||||
write(_socket, outString.c_str(), outString.length());
|
||||
}
|
||||
|
||||
void User::sendMsg(User::MsgType type, Json::Value message, std::string userName, std::string color) {
|
||||
Json::Value sendMessage;
|
||||
sendMessage["type"] = type;
|
||||
sendMessage["message"] = message;
|
||||
sendMessage["userName"] = userName;
|
||||
sendMessage["color"] = color;
|
||||
std::string outString;
|
||||
std::stringstream outStream;
|
||||
outStream << sendMessage;
|
||||
outString = outStream.str();
|
||||
write(_socket, outString.c_str(), outString.length());
|
||||
}
|
||||
|
||||
void User::checkerTask() {
|
||||
while (!_stop) {
|
||||
fd_set readSd;
|
||||
FD_ZERO(&readSd);
|
||||
FD_SET(_socket, &readSd);
|
||||
timeval tv;
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = 100;
|
||||
if (select(_socket + 1, &readSd, NULL, NULL, &tv) < 0 && errno != ETIMEDOUT) {
|
||||
_parent->removeUser(_token);
|
||||
_stop = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void User::stop() {
|
||||
_stop = true;
|
||||
}
|
||||
|
||||
std::string User::color() const {
|
||||
return _color;
|
||||
}
|
||||
|
||||
} // namespace Lib
|
||||
} // namespace Yc
|
||||
|
||||
Reference in New Issue
Block a user