//send data to a socket at a specific address. inline void sendData(const std::string& address, char * src, uint32_t size){ SOCKET socket = 0; for (const auto& e : readList.sockets){ if (readList.getAddressString(e.first) == address){ socket = e.first; Socket::sendData(socket, src, size); } } }
//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"; }
//get a strign that reprisents the address of a port inline std::string getAddressString(SOCKET sock){ std::lock_guard<std::recursive_mutex> lock(mutex); return readList.getAddressString(sock); }