// callback executed when a client done with a request static void s3client_pool_on_client_released (gpointer client, gpointer ctx) { PoolClient *pc = (PoolClient *) ctx; RequestData *data; // if we have a request pending data = g_queue_pop_head (pc->pool->q_requests); if (data) { data->on_client_ready (client, data->ctx); g_free (data); } }
/*override*/ void AESMWorkerThread::run() { while (!isStopped()) { RequestData* requestData = m_queue->blockingPop(); if (isStopped()) break; IAEResponse *response = requestData->getRequest()->execute(&m_aesmLogic); m_transporter.sendResponse(response, requestData->getSocket()); delete requestData; delete response; } }
void TMsgSocket::DoneRecvReliableUDP(const TMsgSocket::RecvRawMsgResult& result, RequestData* requestData) { TMsgSocket* thisSocket = (TMsgSocket*)(result.theSocket); if (result.closed) requestData->Done(RecvRawMsgResult(thisSocket, 0, 0, true)); AutoCrit autoCrit(thisSocket->tagCrit); thisSocket->recving = false; if (!thisSocket->abortingRecv) { if (!result.msgLength) { if (GetTickCount() >= requestData->startTime + requestData->timeout) requestData->Done(RecvRawMsgResult(thisSocket, 0, 0, false)); else { requestData->ResetTimeout(); thisSocket->TMsgSocket::SendRawMsg(requestData->outgoingMsgLength, requestData->outgoingMsg, -1, true, true); } thisSocket->IssueRecv(); return; } } if (result.msg != 0) { // handle message if (*(result.msg) == HeaderWithTag) // 13 { unsigned short tag = getLittleEndian(*(unsigned short*)(result.msg + 1)); TagMap::iterator itor = thisSocket->tagMap.find(tag); if (itor != thisSocket->tagMap.end()) { RequestData* replyTo = (*itor).second; unsigned char* actualMsg = new unsigned char[result.msgLength - 3]; if (actualMsg) { memcpy(actualMsg, result.msg + 3, result.msgLength - 3); replyTo->Done(RecvRawMsgResult(thisSocket, actualMsg, result.msgLength-3, false)); } } } delete result.msg; } if (!(thisSocket->abortingRecv)) thisSocket->IssueRecv(); else thisSocket->abortingRecv = false; thisSocket->doneRecvEvent.Set(); }
bool login(RequestData& data, user& usr) { if (data.count("name") && data.count("pass")) { if (data["name"] == "guest" && data["pass"] == "password") { usr.id = "1"; usr.name = "guest"; usr.email = "\"Guest\" <*****@*****.**>"; usr.location = "Liverpool, England, UK"; return true; } } return false; }
string Rest::buildParams(RequestData const ¶ms) { if(params.empty()) { return ""; } RequestData::const_iterator pb = params.begin(), pe = params.end(); string data = pb->first+ "=" + pb->second; ++ pb; if(pb == pe){ return data; } for(; pb!= pe; ++ pb){ data+= "&" + pb->first + "=" + pb->second; } return data; }
int doSomething( Durations &durationsRef, std::mutex &lockerRef, const AnySessionHdr &sessionHdr, const RequestData &inBuffer, AnySessionSpecific &sessionSpecific, AnyAnswerHdr &answerHdr, AnswerData &outBuffer) { const char *tag = "At a session named "; outBuffer = AnswerData(tag, tag + strlen(tag)); std::copy(getName(sessionHdr), getName(sessionHdr) + strlen(getName(sessionHdr)), std::back_inserter(outBuffer)); outBuffer.push_back('\n'); std::copy(inBuffer.begin(), inBuffer.end(), std::back_inserter(outBuffer)); uint32_t &numOfRequestsLeft = *getNumPtr(answerHdr); numOfRequestsLeft = -- sessionSpecific.numOfRequestsLeft; if (!numOfRequestsLeft) { time_t duration = time(0) - sessionSpecific.startedAt; std::stringstream sessionFooter; { std::lock_guard<std::mutex> lockGuard(lockerRef); auto inserted = durationsRef.insert(duration).first; sessionFooter << std::endl << " Session duration: " << duration << " second(s); " << std::endl << " Rating position: " << std::distance(durationsRef.begin(), inserted) + 1 << "; "; } std::string sessionFooterStr = sessionFooter.str(); std::copy(sessionFooterStr.begin(), sessionFooterStr.end(), std::back_inserter(outBuffer)); } return numOfRequestsLeft; }
static size_t curlwrite_cb(void *ptr, size_t size, size_t nmemb, void *data) { RequestData *requestdata = data; double contentlength; g_string_append_len(requestdata->response, ptr, size*nmemb); curl_easy_getinfo(requestdata->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &contentlength); if (requestdata->user_cb) { NetStatusProgress progress = {0}; progress.current = requestdata->response->len; progress.total = (int)contentlength; requestdata->user_cb(NET_STATUS_PROGRESS, &progress, requestdata->user_data); } return size*nmemb; }
static void s3http_connection_on_responce_cb (struct evhttp_request *req, void *ctx) { RequestData *data = (RequestData *) ctx; struct evbuffer *inbuf; const char *buf = NULL; size_t buf_len; LOG_debug (CON_LOG, "Got HTTP response from server !"); if (!req) { LOG_err (CON_LOG, "Request failed !"); if (data->error_cb) data->error_cb (data->con, data->ctx); goto done; } // XXX: handle redirect // 200 and 204 (No Content) are ok if (evhttp_request_get_response_code (req) != 200 && evhttp_request_get_response_code (req) != 204 && evhttp_request_get_response_code (req) != 307) { LOG_err (CON_LOG, "Server returned HTTP error: %d !", evhttp_request_get_response_code (req)); LOG_debug (CON_LOG, "Error str: %s", req->response_code_line); if (data->error_cb) data->error_cb (data->con, data->ctx); goto done; } inbuf = evhttp_request_get_input_buffer (req); buf_len = evbuffer_get_length (inbuf); buf = (const char *) evbuffer_pullup (inbuf, buf_len); if (data->responce_cb) data->responce_cb (data->con, data->ctx, buf, buf_len, evhttp_request_get_input_headers (req)); else LOG_debug (CON_LOG, ">>> NO callback function !"); done: g_free (data); }
int main( int argc, char **argv) { bool helpNeeded; std::string addr; int port; options_description inOpts; inOpts.add_options() ("help,h", bool_switch(&helpNeeded), "print this help text and exit") ("ip,i", value<std::string>(&addr), "server's ip (in dot-notation)") ("port,p", value<int>(&port), "server port"); if (argc < 3) { return usage(1, argv[0], inOpts); } try { variables_map vm; store(parse_command_line(argc, argv, inOpts), vm); notify(vm); if (helpNeeded) { return usage(0, argv[0], inOpts); } Client<ProtoWithAnyHdr> client(addr, port); std::string requestString; while (std::cout << "request '[pause/]string'> ", std::cin >> requestString) { AnyHdr hdr; bzero(&hdr, sizeof(hdr)); uint32_t hdrOpt = numOfOpts; std::string::size_type endOfSubstr = requestString.find('/'); std::string data; if (endOfSubstr == std::string::npos) { data = requestString; hdr.set<pauseField>(0); } else { data = std::string(requestString, endOfSubstr + 1); try { hdr.set<pauseField>(stoi(std::string(requestString, 0, endOfSubstr))); } catch (const std::invalid_argument &exc) { std::cerr << "Cannot understand what pause is to be (" << exc.what() << "); " << std::endl; continue; } } boost::tokenizer<> dataTokenized(data); if (dataTokenized.begin() != dataTokenized.end()) { std::string elem = *dataTokenized.begin(); if (elem == "help") { usage(0, argv[0], inOpts); continue; } hdrOpt = elem == "sum" ? sumNumbers : (elem == "sort" ? sortNumbers : (elem == "terminate" ? terminateSession : (elem == "log" ? logRequest : echo))); hdr.set<optField>(hdrOpt); } RequestData requestBuffer; for (const std::string &elem : dataTokenized) { try { requestBuffer.push_back(stoi(elem)); } catch (const std::invalid_argument &) {} } if (hdrOpt == logRequest || hdrOpt == terminateSession) { client.send(hdr, requestBuffer); } else { AnswerData answerData = client.request(hdr, requestBuffer); std::cout << "Answer: " << std::endl; for (const Answer &answer : answerData) { std::cout << answer << std::endl; } } } } catch(std::runtime_error &exc) { std::cerr << "ERROR: " << exc.what() << "; " << std::endl; return 1; } return 0; }