void* wk_fileServ(void* sockp) { FileServer_Private client; pthread_mutex_lock(&s_clientMutex); client.m_sock = reinterpret_cast<DS::SocketHandle>(sockp); client.m_readerId = 0; s_clients.push_back(&client); pthread_mutex_unlock(&s_clientMutex); try { file_init(client); for ( ;; ) { DS::RecvValue<uint32_t>(client.m_sock); // Message size uint32_t msgId = DS::RecvValue<uint32_t>(client.m_sock); switch (msgId) { case e_CliToFile_PingRequest: cb_ping(client); break; case e_CliToFile_BuildIdRequest: cb_buildId(client); break; case e_CliToFile_ManifestRequest: cb_manifest(client); break; case e_CliToFile_ManifestEntryAck: cb_manifestAck(client); break; case e_CliToFile_DownloadRequest: cb_downloadStart(client); break; case e_CliToFile_DownloadChunkAck: cb_downloadNext(client); break; default: /* Invalid message */ fprintf(stderr, "[File] Got invalid message ID %d from %s\n", msgId, DS::SockIpAddress(client.m_sock).c_str()); DS::CloseSock(client.m_sock); throw DS::SockHup(); } } } catch (DS::AssertException ex) { fprintf(stderr, "[File] Assertion failed at %s:%ld: %s\n", ex.m_file, ex.m_line, ex.m_cond); } catch (DS::SockHup) { // Socket closed... } pthread_mutex_lock(&s_clientMutex); auto client_iter = s_clients.begin(); while (client_iter != s_clients.end()) { if (*client_iter == &client) client_iter = s_clients.erase(client_iter); else ++client_iter; } pthread_mutex_unlock(&s_clientMutex); DS::FreeSock(client.m_sock); return 0; }
void wk_fileServ(DS::SocketHandle sockp) { FileServer_Private client; s_clientMutex.lock(); client.m_sock = sockp; client.m_readerId = 0; s_clients.push_back(&client); s_clientMutex.unlock(); try { file_init(client); for ( ;; ) { DS::RecvValue<uint32_t>(client.m_sock); // Message size uint32_t msgId = DS::RecvValue<uint32_t>(client.m_sock); switch (msgId) { case e_CliToFile_PingRequest: cb_ping(client); break; case e_CliToFile_BuildIdRequest: cb_buildId(client); break; case e_CliToFile_ManifestRequest: cb_manifest(client); break; case e_CliToFile_ManifestEntryAck: cb_manifestAck(client); break; case e_CliToFile_DownloadRequest: cb_downloadStart(client); break; case e_CliToFile_DownloadChunkAck: cb_downloadNext(client); break; default: /* Invalid message */ fprintf(stderr, "[File] Got invalid message ID %d from %s\n", msgId, DS::SockIpAddress(client.m_sock).c_str()); DS::CloseSock(client.m_sock); throw DS::SockHup(); } } } catch (const DS::AssertException& ex) { fprintf(stderr, "[File] Assertion failed at %s:%ld: %s\n", ex.m_file, ex.m_line, ex.m_cond); } catch (const DS::PacketSizeOutOfBounds& ex) { fprintf(stderr, "[File] Client packet size too large: Requested %u bytes\n", ex.requestedSize()); } catch (const DS::SockHup&) { // Socket closed... } s_clientMutex.lock(); auto client_iter = s_clients.begin(); while (client_iter != s_clients.end()) { if (*client_iter == &client) client_iter = s_clients.erase(client_iter); else ++client_iter; } s_clientMutex.unlock(); DS::FreeSock(client.m_sock); }