From 81be0c6db81bf225c21f65eb2374fa4b702a2aa0 Mon Sep 17 00:00:00 2001 From: C Date: Wed, 20 Jul 2022 12:30:46 +0500 Subject: [PATCH] session creation fixed --- ServerPixel.sln | 6 ++ ServerPixel/ServerPixel.cpp | 117 ++++++++++++++------------------ ServerPixel/ServerPixel.vcxproj | 61 +++++++++++++++++ ServerPixel/session.h | 9 +++ ServerPixel/sessionManager.h | 8 +++ ServerPixel/user.h | 8 +-- 6 files changed, 139 insertions(+), 70 deletions(-) diff --git a/ServerPixel.sln b/ServerPixel.sln index b099999..6c6452b 100644 --- a/ServerPixel.sln +++ b/ServerPixel.sln @@ -7,12 +7,18 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ServerPixel", "ServerPixel\ EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution + build|x64 = build|x64 + build|x86 = build|x86 Debug|x64 = Debug|x64 Debug|x86 = Debug|x86 Release|x64 = Release|x64 Release|x86 = Release|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution + {46B65B94-51FC-4B93-9B89-262FD262C49F}.build|x64.ActiveCfg = Release|x64 + {46B65B94-51FC-4B93-9B89-262FD262C49F}.build|x64.Build.0 = Release|x64 + {46B65B94-51FC-4B93-9B89-262FD262C49F}.build|x86.ActiveCfg = build|Win32 + {46B65B94-51FC-4B93-9B89-262FD262C49F}.build|x86.Build.0 = build|Win32 {46B65B94-51FC-4B93-9B89-262FD262C49F}.Debug|x64.ActiveCfg = Debug|x64 {46B65B94-51FC-4B93-9B89-262FD262C49F}.Debug|x64.Build.0 = Debug|x64 {46B65B94-51FC-4B93-9B89-262FD262C49F}.Debug|x86.ActiveCfg = Debug|Win32 diff --git a/ServerPixel/ServerPixel.cpp b/ServerPixel/ServerPixel.cpp index 9d97d38..6d83d75 100644 --- a/ServerPixel/ServerPixel.cpp +++ b/ServerPixel/ServerPixel.cpp @@ -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(std::atoi("13001")); + auto const port = static_cast(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); diff --git a/ServerPixel/ServerPixel.vcxproj b/ServerPixel/ServerPixel.vcxproj index 8b3d01a..75ea8d4 100644 --- a/ServerPixel/ServerPixel.vcxproj +++ b/ServerPixel/ServerPixel.vcxproj @@ -1,6 +1,14 @@ + + build + Win32 + + + build + x64 + Debug Win32 @@ -39,6 +47,13 @@ true Unicode + + Application + false + v143 + true + Unicode + Application true @@ -52,6 +67,13 @@ true Unicode + + Application + false + v143 + true + Unicode + @@ -63,12 +85,18 @@ + + + + + + @@ -98,6 +126,22 @@ true + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + Level3 @@ -112,6 +156,23 @@ + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + C:\Libraries\json-develop\include;C:\Libraries\boost\include\boost_1_79_0;%(AdditionalIncludeDirectories) + + + Console + true + true + true + + + Level3 true diff --git a/ServerPixel/session.h b/ServerPixel/session.h index d9e7870..43e8265 100644 --- a/ServerPixel/session.h +++ b/ServerPixel/session.h @@ -33,11 +33,20 @@ public: if (users[i].getId() == id) users.erase(users.begin() + i); } + bool existsUserId(uint32_t id) const + { + for (auto& u : users) + if (u.getId() == id) + return true; + return false; + } inline size_t getUsersCount() const { return users.size(); } inline const uint32_t& getId() const { return sessId; } + inline const uint32_t& getOwnerId() const { return sessId; } inline const std::vector& getProcIds() const { return procIds; } inline void addProcId(DWORD procId) { procIds.push_back(procId); } inline const std::string& getOwnerIp() const { return ownerIp; } inline const uint16_t getPort() const { return port; } + }; diff --git a/ServerPixel/sessionManager.h b/ServerPixel/sessionManager.h index 3ca88a6..b79feb6 100644 --- a/ServerPixel/sessionManager.h +++ b/ServerPixel/sessionManager.h @@ -43,6 +43,14 @@ public: return false; } + bool existsOwnerId(uint32_t id) + { + for (auto& s : sessions) + if (s.getOwnerId() == id) + return true; + return false; + } + size_t getCount() { return sessions.size(); } void setlimit(uint32_t limit) { this->limit = limit; } bool isLimitReached() diff --git a/ServerPixel/user.h b/ServerPixel/user.h index ef40458..2cd0294 100644 --- a/ServerPixel/user.h +++ b/ServerPixel/user.h @@ -10,7 +10,7 @@ #pragma once class user { -public: +private: std::string name = ""; char age = 0; uint32_t id = 0; @@ -25,8 +25,8 @@ public: { this->id = id; } - std::string getName() { return name; } - char getAge() { return age; } - uint32_t getId() { return id; } + std::string getName() const { return name; } + char getAge() const { return age; } + uint32_t getId() const { return id; } };