session creation fixed

This commit is contained in:
C
2022-07-20 12:30:46 +05:00
parent 36ff1cbd7b
commit 81be0c6db8
6 changed files with 139 additions and 70 deletions
+51 -66
View File
@@ -127,31 +127,33 @@ int main(int argc, char* argv[])
// args
// 1 - copies amount
// 2 - path_to_node.js
// 3 - start_http_port
// 4 - start_streamer_port
// args
// 1 - server ip
// 2 - server port
// 3 - copies amount
// 4 - path_to_node.js
// 5 - start_http_port
// 6 - start_streamer_port
// 5 - path_to_application
// 6 - application_args (without -pixelStreamingPort cos its argv[3])
// 7 - path_to_application
// 8 - application_args (without -pixelStreamingPort cos its argv[3])
// sessions
uint16_t sessionLimit = std::atoi(argv[1]);
uint16_t sessionLimit = std::atoi(argv[3]);
sessionManager sessManager;
sessManager.setlimit(sessionLimit);
// merging app arguments
std::string appPath = argv[5];
std::string appPath = argv[7];
std::string appArgs;
for (int i = 6; i < argc; ++i)
for (int i = 8; i < argc; ++i)
appArgs += argv[i] + std::string(" ");
appArgs += "-PixelStreamingPort=";
// first app ports
uint16_t portHttp = std::atoi(argv[3]);
uint16_t portStream = std::atoi(argv[4]);
uint16_t portHttp = std::atoi(argv[5]);
uint16_t portStream = std::atoi(argv[6]);
// node js
/*std::string nodePath = "C:\\Program Files\\nodejs\\node.exe";
@@ -160,14 +162,15 @@ int main(int argc, char* argv[])
// starting node js servers
for (uint16_t i = 0; i < sessionLimit; ++i)
{
system((std::string("start node ") + argv[2] + " " + std::to_string(portHttp + i) + " " + std::to_string(portStream + i)).c_str());
system((std::string("start node ") + argv[4] + " " + std::to_string(portHttp + i) + " " + std::to_string(portStream + i)).c_str());
}
// ip and socket init
std::string ip("192.168.1.115");
uint16_t sess_port = 13002;
std::string ip(argv[1]);
std::cout << "ip: " << ip << std::endl;
auto const address = net::ip::make_address(ip);
auto const port = static_cast<unsigned short>(std::atoi("13001"));
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
std::cout << "port: " << port << std::endl;
net::io_context ioc{ 1 };
tcp::acceptor acceptor{ ioc, {address, port} };
@@ -183,7 +186,6 @@ int main(int argc, char* argv[])
sock = std::move(socket),
&sessManager,
&ip,
&sess_port,
&user_n,
appPath,
appArgs,
@@ -209,26 +211,16 @@ int main(int argc, char* argv[])
res.set(http::field::server, std::string(BOOST_BEAST_VERSION_STRING) + " websocket-server-sync");
}));*/
//session sessTmp;
session* sessCur = nullptr;
uint32_t sessId = 0;
uint16_t sessMinPort = sessManager.getMinPort();
uint16_t sessHttpPort = sessMinPort ? sessMinPort + 1 : portHttp;
uint16_t sessStreamPort = portStream + (sessHttpPort - portHttp);
//std::cout << "minPort: " << sessMinPort << std::endl;
//std::cout << "httpPortDefault: " << portHttp << std::endl
// << "streamPortDefault: " << portStream << std::endl;
//std::cout << "httpPort: " << sessHttpPort << std::endl
// << "streamPort: " << sessStreamPort << std::endl;
nlohmann::json jsonData;
std::string message;
std::string content;
//session sessTmp;
session* sessCur = nullptr;
uint32_t sessId = 0;
while (true)
{
try
@@ -292,30 +284,35 @@ int main(int argc, char* argv[])
ws.write(buffer.data());
continue;
}
if (sessManager.existsOwnerIp(user_ip))
if (sessManager.existsOwnerId(user_id))
{
boost::beast::ostream(buffer)
<< std::string("{\"message\" : \"PERSON_LIMIT\"}").c_str();
ws.write(buffer.data());
continue;
}
//sessManager
boost::beast::ostream(buffer)
<< std::string("{\"message\" : \"CREATING_SESSION\", \"content\" : \"true\"}").c_str();
ws.write(buffer.data());
// get session port
uint16_t sessMinPort = sessManager.getMinPort();
uint16_t sessHttpPort = sessMinPort ? sessMinPort + 1 : portHttp;
uint16_t sessStreamPort = portStream + (sessHttpPort - portHttp);
// creating process
PROCESS_INFORMATION appProcInfo;
//PROCESS_INFORMATION nodeProcInfo;
// create app process
if (!CreateProc(appPath, appPath + " " + appArgs + std::to_string(sessStreamPort), &appProcInfo))
{
std::cout << "CreateProcess failed (" << GetLastError() << ")." << std::endl;
continue;
}
boost::beast::ostream(buffer)
<< std::string("{\"message\" : \"CREATING_SESSION\", \"content\" : \"true\"}").c_str();
ws.write(buffer.data());
// bind app process close listen
std::thread([appProcInfo]()
@@ -325,34 +322,13 @@ int main(int argc, char* argv[])
CloseHandle(appProcInfo.hThread);
}).detach();
//std::string nodeArgs = nodePath + " " + nodeFilePath + " "
// + std::to_string(sessHttpPort) + " "
// + std::to_string(sessStreamPort);
//// create node js server proc
//if (!CreateProc(nodePath, nodePath + " " + nodeFilePath + " "
// + std::to_string(sessHttpPort) + " "
// + std::to_string(sessStreamPort), &nodeProcInfo))
//{
// std::cout << "CreateProcess failed (" << GetLastError() << ")." << std::endl;
// continue;
//}
////bind node js close listen
//std::thread([nodeProcInfo]()
// {
// WaitForSingleObject(nodeProcInfo.hProcess, INFINITE);
// CloseHandle(nodeProcInfo.hProcess);
// CloseHandle(nodeProcInfo.hThread);
// }).detach();
sessId = hash6(user_id);
// create session
sessManager.add(
session(
sessId,
ip + std::to_string(sess_port),
ip + std::to_string(sessHttpPort),
user_ip,
sessHttpPort
));
@@ -367,7 +343,8 @@ int main(int argc, char* argv[])
//sessCur->addProcId(GetProcessId(nodeProcInfo.hProcess));
}
std::cout << "user(" << thisUserNum << ") created session (id: " << sessId << ")" << std::endl;
std::cout << "user(" << thisUserNum << ") created session (id: " << sessId <<
"), (httpPort: " << sessHttpPort << "), (streamPort: " << sessStreamPort << ")" << std::endl;
// send respond
buffer.clear();
@@ -378,7 +355,6 @@ int main(int argc, char* argv[])
}
else if (message == "SESS_CONNECT")
{
std::cout << "sess_connect" << std::endl;
// read session id
try
{
@@ -402,10 +378,17 @@ int main(int argc, char* argv[])
std::cout << "Unknown exception" << e.what() << std::endl;
}
sessId = atoi(content.c_str());
// connect to session
if (sessManager.getById(atoi(content.c_str()), &sessCur))
if (sessManager.getById(sessId, &sessCur))
{
if (sessCur->existsUserId(user_id))
{
std::cout << "user exists: " << user_id << std::endl;
continue;
}
sessCur->addUser(user(user_id));
std::cout << "user(" << thisUserNum << ") connected to session (id: " << sessCur->getId()
<< "), (users: " << sessCur->getUsersCount() << ")" << std::endl;
@@ -431,6 +414,8 @@ int main(int argc, char* argv[])
catch (beast::system_error const& se)
{
std::cout << "user(" << thisUserNum << ") disconnected" << std::endl;
if (!sessManager.getById(sessId, &sessCur))
std::cout << "no session with id: " << sessId << std::endl;
if (sessCur)
{
sessCur->removeUserById(user_id);