//send the hint to the server that the connecting is about to be closed. inline void close(SOCKET socket){ sendProtocolMessage(socket, PROTOCOLCODES::CLOSEME, 1); closesocket(socket); if (readList.sockExists(socket)){ readList.popSocket(socket); } }
//send the hint to the server that the connecting is about to be closed. inline void closeAll(){ for (const auto& e : readList.sockets){ SOCKET socket = e.first; sendProtocolMessage(socket, PROTOCOLCODES::CLOSEME, 1); closesocket(socket); if (readList.sockExists(socket)){ readList.popSocket(socket); } if (readList.sockets.size() == 0){ break; } } }
//start listening for connections and processing messages. inline void synchListen(){ _DB std::cout << "Entering synchListen()\n"; listen(serverSocket, SOMAXCONN); SOCKET accepted = -1; bool listening = true; auto lamb = [&](bool& listening){ while (listening && runListenLoop){ socklen_t size = sizeof(serverAddress); accepted = accept(serverSocket, (sockaddr *)&serverAddress, &size); if (accepted == SOCKET_ERROR){ //error } else{ std::lock_guard<std::recursive_mutex> lock(mutex); readList.pushSocket(accepted, serverAddress); _DB std::cout << "Adding SOCKET `" << accepted << "` to connected to `" << readList.getAddressString(accepted) << "`." << std::endl; } } }; auto f = std::async(std::launch::async, lamb, std::ref(listening)); _DB std::cout << "Fired the socket aceptor!\n"; bool run = true; runListenLoop = true; while (run && runListenLoop){ auto list = readList.getReadable(); while (list.size() > 0){ std::lock_guard<std::recursive_mutex> lock(mutex); _DB std::cout << "_________________________________________\n"; //std::lock_guard<std::mutex> lock(mutex); ProtMsg msg = getProtocolMessage(list.front()); int ret = handleProtocol(msg, list.front(), run); _DB std::cout << "handling protocol messages.\n"; if (ret == PROTOCOLCODES::CLOSEME){ readList.popSocket(list.front()); } list.pop_front(); } } _DB std::cout << "Exiting sychlisten loop.\n"; }