我的代码中有这个错误,这里是代码: `
#pragma once
#include <WinSock2.h>
#include <Windows.h>
#include "TriviaServer.h"
#include "Validator.h"
#define PORT 8820
#define IFACE 0
#include <algorithm>
using namespace std;
TriviaServer::TriviaServer()
{
_roomIdSequence = 0;
_socket = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (_socket == INVALID_SOCKET)
throw std::exception(__FUNCTION__ " - socket");
/*DataBase Build*/
}
TriviaServer::~TriviaServer()
{
map<int, Room*>::iterator it = _roomsList.begin();
for (it; it != _roomsList.end(); it++)
{
delete it->second;
}
map<SOCKET, User*>::iterator it2 = _connectedUsers.begin();
for (it2; it2 != _connectedUsers.end(); it2++)
{
delete it2->second;
closesocket(it2->first);
}
closesocket(_socket);
}
void TriviaServer::server()
{
bindAndListen();
thread handleMessages(&TriviaServer::handleRecievedMessages);
handleMessages.detach();
while (true)
{
cout << "accepting client..." << endl;
accept();
}
}
void TriviaServer::bindAndListen()
{
struct sockaddr_in sa = { 0 };
sa.sin_port = htons(PORT);
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = IFACE;
if (::bind(_socket, (struct sockaddr*)&sa, sizeof(sa)) == SOCKET_ERROR)
throw std::exception(__FUNCTION__ " - bind");
cout << "binded" << endl;
if (::listen(_socket, SOMAXCONN) == SOCKET_ERROR)
throw std::exception(__FUNCTION__ " - listen");
cout << "listening..." << endl;
}
void TriviaServer::accept()
{
SOCKET client_socket = ::accept(_socket, NULL, NULL);
if (client_socket == INVALID_SOCKET)
throw std::exception(__FUNCTION__);
cout << "Client accepted !" << endl;
// create new thread for client and detach from it
std::thread tr(&TriviaServer::clientHandler, this, client_socket);
tr.detach();
}
void TriviaServer::clientHandler(SOCKET s)
{
bool clientConnected = true;
while (clientConnected)
{
int code = Helper::getMessageTypeCode(s);
RecievedMessage* m = buildRecieveMessage(s, code);
addRecievedMessage(m);
}
}
void TriviaServer::safeDeleteUser(RecievedMessage* m)
{
try
{
SOCKET soc = m->getSock();
handleSignout(m);
if (soc != NULL)
{
closesocket(soc);
}
}
catch (...)
{ }
}
void TriviaServer::addRecievedMessage(RecievedMessage* m)
{
std::unique_lock<std::mutex> locker(_mtxRecievedMessages);
m->setUser(getUserBySocket(m->getSock()));
locker.lock();
_queRcvMessages.push(m);
locker.unlock();
_cond.notify_one();
}
RecievedMessage* TriviaServer::buildRecieveMessage(SOCKET s, int code)
{
vector<string> values;
int len;
switch (code)
{
case SIGN_IN_REQUEST:
for (int i = 0; i < 2; i++)
{
len = Helper::getIntPartFromSocket(s, 2);
values.push_back(Helper::getStringPartFromSocket(s, len));
}
break;
case SIGN_OUT_REQUEST:
break;
case SIGN_UP_REQUEST:
for (int i = 0; i < 3; i++)
{
len = Helper::getIntPartFromSocket(s, 2);
values.push_back(Helper::getStringPartFromSocket(s, len));
}
break;
case AVAILABLE_ROOM_REQUEST:
break;
case ROOM_USERS_REQUEST:
case ROOM_JOIN_REQUEST:
values.push_back(Helper::getStringPartFromSocket(s, 4));
break;
case ROOM_LEAVE_REQUEST:
break;
case ROOM_CREATE_REQUEST:
len = Helper::getIntPartFromSocket(s, 2);
values.push_back(Helper::getStringPartFromSocket(s, len));
values.push_back(Helper::getStringPartFromSocket(s, 1));
values.push_back(Helper::getStringPartFromSocket(s, 2));
values.push_back(Helper::getStringPartFromSocket(s, 2));
break;
case ROOM_CLOSE_REQUEST:
break;
case EXIT_APPLICATION:
break;
}
RecievedMessage* m;
if (!values.empty())
{
m = new RecievedMessage(s, code, values);
}
else
{
m = new RecievedMessage(s, code);
}
return m;
}
void TriviaServer::handleRecievedMessages()
{
std::unique_lock<std::mutex> locker(_mtxRecievedMessages);
while (true)
{
if (_queRcvMessages.empty())
{
_cond.wait(locker);
}
switch (_queRcvMessages.front()->getMessageCode())
{
case SIGN_IN_REQUEST:
handleSignin(_queRcvMessages.front());
break;
case SIGN_OUT_REQUEST:
handleSignout(_queRcvMessages.front());
break;
case SIGN_UP_REQUEST:
handleSignup(_queRcvMessages.front());
break;
case AVAILABLE_ROOM_REQUEST:
handleGetRooms(_queRcvMessages.front());
break;
case ROOM_USERS_REQUEST:
handleGetUsersInRoom(_queRcvMessages.front());
break;
case ROOM_JOIN_REQUEST:
handleJoinRoom(_queRcvMessages.front());
break;
case ROOM_LEAVE_REQUEST:
handleLeaveRoom(_queRcvMessages.front());
break;
case ROOM_CREATE_REQUEST:
handleCreateRoom(_queRcvMessages.front());
break;
case ROOM_CLOSE_REQUEST:
handleCloseRoom(_queRcvMessages.front());
break;
case EXIT_APPLICATION:
handleSignout(_queRcvMessages.front());
break;
}
_queRcvMessages.pop();
}
}
User* TriviaServer::getUserBySocket(SOCKET s)
{
std::map<SOCKET, User*>::iterator it;
it = find(_connectedUsers.begin(), _connectedUsers.end(), s);
if (it != _connectedUsers.end())
{
return it->second;
}
else
{
return NULL;
}
}
User* TriviaServer::handleSignin(RecievedMessage* m)
{
int flag = true; // if wrong details
map<string, string>::iterator it;
map<SOCKET,User*>::iterator itConnected;
User* login = nullptr;
for (it = _dbUsers.begin(); it != _dbUsers.end(); it++)
{
if ((it->first) == ((*(m->getValues()))[0]) && (it->second) == ((*(m->getValues()))[1]))
{
flag = false;
login = new User(it->first, m->getSock());
for (itConnected = _connectedUsers.begin(); itConnected != _connectedUsers.end(); it++)
{
if (itConnected->second->getUsername() == login->getUsername())
{
delete login;
login = nullptr;
}
}
}
}
if (login)
{
Helper::sendData(m->getSock(), to_string(SIGN_IN_SUCCESS));
}
else if (flag)
{
Helper::sendData(m->getSock(), to_string(SIGN_IN_WRONG_DETAILS));
}
else
{
Helper::sendData(m->getSock(), to_string(SIGN_IN_USER_CONNECTED));
}
return login;
}
bool TriviaServer::handleSignup(RecievedMessage* m)
{
//[203##username##pass##email]
string user = (*m->getValues())[0];
string pass = (*m->getValues())[1];
string email = (*m->getValues())[2];
if (!Validator::isPasswordValid(pass))
{
Helper::sendData(m->getSock(), to_string(SIGN_UP_PASS_ILLEGAL));
return false;
}
if (!Validator::isUsernameValid(user))
{
Helper::sendData(m->getSock(), to_string(SIGN_UP_USERNAME_ILLEGAL));
return false;
}
//////////////////// TEMP CODE
map<string, string>::iterator it = _dbUsers.begin();
for (it; it != _dbUsers.end(); it++)
{
if (user == it->first)
{
Helper::sendData(m->getSock(), to_string(SIGN_UP_USERNAME_EXISTS));
return false;
}
}
_dbUsers.insert(pair<string, string>(user, pass));
//////////////////// END TEMP CODE
//////////////////// FUTURE CODE:
/*if (!DataBase::isUserExists(user))
{
Helper::sendData(m->getSock(), to_string(SIGN_UP_USERNAME_EXISTS));
return false;
}
if(!DataBase::addNewUser(user, pass, email))
{
Helper::sendData(m->getSock(), to_string(SIGN_UP_OTHER));
return false;
}
*/
Helper::sendData(m->getSock(), to_string(SIGN_UP_SUCCESS));
return true;
}
void TriviaServer::handleSignout(RecievedMessage* m)
{
User* u;
if (u = m->getUser())
{
map<SOCKET, User*>::iterator it = _connectedUsers.begin();
for (it; it != _connectedUsers.end(); it++)
{
if (it->first == u->getSocket())
{
delete it->second;
closesocket(it->first);
}
}
handleCloseRoom(m);
handleLeaveRoom(m);
//handleLeaveGame(m);
}
}
bool TriviaServer::handleCreateRoom(RecievedMessage* m)
{
User* curr = m->getUser();
if (curr)
{
vector<string>* values = m->getValues();
if (curr->createRoom(++_roomIdSequence, m->getUser(),(*values)[0] ,stoi((*values)[1], nullptr, 0), stoi((*values)[3], nullptr, 0), stoi((*values)[2], nullptr, 0)))
{
_roomsList.insert(pair<int, Room*>(_roomIdSequence, curr->getRoom()));
return true;
}
return false;
}
else
{
return false;
}
}
bool TriviaServer::handleCloseRoom(RecievedMessage* m)
{
}
bool TriviaServer::handleJoinRoom(RecievedMessage* m)
{
}
bool TriviaServer::handleLeaveRoom(RecievedMessage* m)
{
}
void TriviaServer::handleGetUsersInRoom(RecievedMessage* m)
{
}
void TriviaServer::handleGetRooms(RecievedMessage* m)
{
}
`
我试图对所有'=='和'!='发表评论,以搜索问题所在,但错误仍然显示。
它是在这个cpp文件中,据我所知,从代码(我无法编译的唯一文件)
感谢您的帮助, 对不起长代码, 奥马尔
答案 0 :(得分:0)
问题是其他标题中包含的顺序,包括, 它应该是
#include <WinSock2.h>
#include <Windows.h>
而不是相反!