void TFRnd::PutSIn(const PSIn& SIn, TCs& Cs){ int BfL=SIn->Len(); char* Bf=new char[BfL]; SIn->GetBf(Bf, BfL); Cs=TCs::GetCsFromBf(Bf, BfL); PutBf(Bf, BfL); delete[] Bf; }
void TSockSys::Send(const uint64& SockId, const PSIn& SIn) { // make sure it's a valid socket IAssert(IsSock(SockId)); uv_tcp_t* SockHnd = SockIdToHndH.GetDat(SockId); // create write request uv_write_req_t* WriteHnd = (uv_write_req_t*)malloc(sizeof(uv_write_req_t)); // copy the data in the buffer WriteHnd->Buffer.len = SIn->Len(); //TODO: handle cases when SIn doesn't have known Len() WriteHnd->Buffer.base = (char*)malloc(WriteHnd->Buffer.len); SIn->GetBf(WriteHnd->Buffer.base, WriteHnd->Buffer.len); // execute the request int ResCd = uv_write((uv_write_t*)WriteHnd, (uv_stream_t*)SockHnd, &WriteHnd->Buffer, 1, OnWrite); // check for errors if (ResCd != 0) { // cleanup first free(WriteHnd->Buffer.base); free(WriteHnd); // and throw exception throw TExcept::New("SockSys.Send: Error sending data: " + SockSys.GetLastErr()); } }
TBlobPt TGBlobBs::PutBlob(const PSIn& SIn){ EAssert((Access==faCreate)||(Access==faUpdate)||(Access==faRestore)); int BfL=SIn->Len(); int MxBfL; int FFreeBlobPtN; GetAllocInfo(BfL, BlockLenV, MxBfL, FFreeBlobPtN); TBlobPt BlobPt; TCs Cs; if (FFreeBlobPtV[FFreeBlobPtN].Empty()){ int FLen=FBlobBs->GetFLen(); if (FLen<=MxSegLen){ EAssert(FLen<=MxBlobFLen); BlobPt=TBlobPt(FLen); FBlobBs->SetFPos(BlobPt.GetAddr()); PutBlobTag(FBlobBs, btBegin); FBlobBs->PutInt(MxBfL); PutBlobState(FBlobBs, bsActive); FBlobBs->PutInt(BfL); FBlobBs->PutSIn(SIn, Cs); FBlobBs->PutCh(TCh::NullCh, MxBfL-BfL); FBlobBs->PutCs(Cs); PutBlobTag(FBlobBs, btEnd); } } else { BlobPt=FFreeBlobPtV[FFreeBlobPtN]; FBlobBs->SetFPos(BlobPt.GetAddr()); AssertBlobTag(FBlobBs, btBegin); int MxBfL=FBlobBs->GetInt(); int FPos=FBlobBs->GetFPos(); AssertBlobState(FBlobBs, bsFree); FFreeBlobPtV[FFreeBlobPtN]=TBlobPt::LoadAddr(FBlobBs); FBlobBs->SetFPos(FPos); PutBlobState(FBlobBs, bsActive); FBlobBs->PutInt(BfL); FBlobBs->PutSIn(SIn, Cs); FBlobBs->PutCh(TCh::NullCh, MxBfL-BfL); FBlobBs->PutCs(Cs); AssertBlobTag(FBlobBs, btEnd); } FBlobBs->Flush(); return BlobPt; }
TBlobPt TGBlobBs::PutBlob(const TBlobPt& BlobPt, const PSIn& SIn){ EAssert((Access==faCreate)||(Access==faUpdate)||(Access==faRestore)); int BfL=SIn->Len(); FBlobBs->SetFPos(BlobPt.GetAddr()); AssertBlobTag(FBlobBs, btBegin); int MxBfL=FBlobBs->GetInt(); AssertBlobState(FBlobBs, bsActive); if (BfL>MxBfL){ DelBlob(BlobPt); return PutBlob(SIn); } else { TCs Cs; FBlobBs->PutInt(BfL); FBlobBs->PutSIn(SIn, Cs); FBlobBs->PutCh(TCh::NullCh, MxBfL-BfL); FBlobBs->PutCs(Cs); PutBlobTag(FBlobBs, btEnd); FBlobBs->Flush(); return BlobPt; } }
void TSAppSrvFun::Exec(const TStrKdV& FldNmValPrV, const PSAppSrvRqEnv& RqEnv) { const PNotify& Notify = RqEnv->GetWebSrv()->GetNotify(); PHttpResp HttpResp; try { // log the call if (NotifyOnRequest) Notify->OnStatus(TStr::Fmt("RequestStart %s", FunNm.CStr())); TTmStopWatch StopWatch(true); // execute the actual function, according to the type PSIn BodySIn; TStr ContTypeVal; if (GetFunOutType() == saotXml) { PXmlDoc ResXmlDoc = ExecXml(FldNmValPrV, RqEnv); TStr ResXmlStr; ResXmlDoc->SaveStr(ResXmlStr); BodySIn = TMIn::New(XmlHdStr + ResXmlStr); ContTypeVal = THttp::TextXmlFldVal; } else if (GetFunOutType() == saotJSon) { TStr ResStr = ExecJSon(FldNmValPrV, RqEnv); BodySIn = TMIn::New(ResStr); ContTypeVal = THttp::AppJSonFldVal; } else { BodySIn = ExecSIn(FldNmValPrV, RqEnv, ContTypeVal); } if (ReportResponseSize) Notify->OnStatusFmt("Response size: %.1f KB", BodySIn->Len() / (double) TInt::Kilo); // log finish of the call if (NotifyOnRequest) Notify->OnStatus(TStr::Fmt("RequestFinish %s [request took %d ms]", FunNm.CStr(), StopWatch.GetMSecInt())); // prepare response HttpResp = THttpResp::New(THttp::OkStatusCd, ContTypeVal, false, BodySIn); } catch (PExcept Except) { // known internal error Notify->OnStatusFmt("Exception: %s", Except->GetMsgStr().CStr()); Notify->OnStatusFmt("Location: %s", Except->GetLocStr().CStr()); TStr ResStr, ContTypeVal = THttp::TextPlainFldVal; if (GetFunOutType() == saotXml) { PXmlTok TopTok = TXmlTok::New("error"); TopTok->AddSubTok(TXmlTok::New("message", Except->GetMsgStr())); TopTok->AddSubTok(TXmlTok::New("location", Except->GetLocStr())); PXmlDoc ErrorXmlDoc = TXmlDoc::New(TopTok); ResStr = XmlHdStr + ErrorXmlDoc->SaveStr(); ContTypeVal = THttp::TextXmlFldVal; } else if (GetFunOutType() == saotJSon) { PJsonVal ResVal = TJsonVal::NewObj(); ResVal->AddToObj("message", Except->GetMsgStr()); ResVal->AddToObj("location", Except->GetLocStr()); ResStr = TJsonVal::NewObj("error", ResVal)->SaveStr(); ContTypeVal = THttp::AppJSonFldVal; } // prepare response HttpResp = THttpResp::New(THttp::InternalErrStatusCd, ContTypeVal, false, TMIn::New(ResStr)); } catch (...) { // unknown internal error TStr ResStr, ContTypeVal = THttp::TextPlainFldVal; if (GetFunOutType() == saotXml) { PXmlDoc ErrorXmlDoc = TXmlDoc::New(TXmlTok::New("error")); ResStr = XmlHdStr + ErrorXmlDoc->SaveStr(); ContTypeVal = THttp::TextXmlFldVal; } else if (GetFunOutType() == saotJSon) { ResStr = TJsonVal::NewObj("error", "Unknown")->SaveStr(); ContTypeVal = THttp::AppJSonFldVal; } // prepare response HttpResp = THttpResp::New(THttp::InternalErrStatusCd, ContTypeVal, false, TMIn::New(ResStr)); } if (LogRqToFile) LogReqRes(FldNmValPrV, HttpResp); // send response RqEnv->GetWebSrv()->SendHttpResp(RqEnv->GetSockId(), HttpResp); }
int Len(){return EofChPrS.Len()+SIn->Len();}