internal unsafe void SendRequest(string[] cmds, uint cmdidx) { // ReqName times f9oms.Layout *pReqLayout = GetRequestLayout(cmds, cmdidx++); if (pReqLayout == null) { return; } Int32 times = 0; if (cmdidx < cmds.Length) { Int32.TryParse(cmds[cmdidx], out times); } RequestRec req = this.RequestRecs_[pReqLayout->LayoutId_ - 1]; System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); Int32 count = 0; sw.Start(); do // 最少送一次. { ++count; f9oms.Api.SendRequestString(this, ref *pReqLayout, req.RequestPacket); } while (--times > 0); sw.Stop(); double us = 1000L * 1000L * sw.ElapsedTicks / (double)System.Diagnostics.Stopwatch.Frequency; Console.WriteLine($"Elapsed={us} us / {count}; Avg={us / count} us"); }
internal unsafe void SetRequest(string[] cmds, uint cmdidx) { // ReqName tag=value... f9oms.Layout *pReqLayout = GetRequestLayout(cmds, cmdidx++); if (pReqLayout == null) { return; } RequestRec req = this.RequestRecs_[pReqLayout->LayoutId_ - 1]; if (cmdidx < cmds.Length) { string tvstr = cmds[cmdidx]; int iend; for (int ifrom = 0; 0 <= ifrom && ifrom < tvstr.Length;) { int iFld = GetFieldIndex(tvstr, ref ifrom, pReqLayout); if (iFld < 0) { break; } string val; char chbr = (ifrom < tvstr.Length ? tvstr[ifrom] : '\0'); switch (chbr) { case '\'': case '"': iend = tvstr.IndexOf(chbr, ++ifrom); if (iend < ifrom) { Console.WriteLine($"Cannot find matching [{chbr}].\n"); goto __BREAK_PUT_FIELDS; } val = tvstr.Substring(ifrom, iend - ifrom); ifrom = tvstr.IndexOf('|', iend + 1); break; default: iend = tvstr.IndexOf('|', ifrom); if (iend < ifrom) { val = tvstr.Substring(ifrom); ifrom = -1; } else { val = tvstr.Substring(ifrom, iend - ifrom); ifrom = iend + 1; } break; } req.Fields_[iFld] = val; } __BREAK_PUT_FIELDS :; MakeRequestStr(pReqLayout, req); } PrintRequest(pReqLayout, req); Console.WriteLine($"RequestStr = [{req.RequestStr}]"); }
static unsafe void MakeRequestStr(f9oms.Layout *pReqLayout, RequestRec req) { StringBuilder res = new StringBuilder(); for (uint iFld = 0; iFld < pReqLayout->FieldCount_; ++iFld) { if (iFld != 0) { res.Append('\x01'); } res.Append(req.Fields_[iFld]); } req.RequestStr = res.ToString(); }
// --------------------------------------------------------------------- static unsafe void PrintRequest(f9oms.Layout *pReqLayout, RequestRec req) { Console.WriteLine($"[{pReqLayout->LayoutId_}] {pReqLayout->LayoutName_}"); StringBuilder typeId = new StringBuilder(); for (uint iFld = 0; iFld < pReqLayout->FieldCount_; ++iFld) { f9sv.Field *fld = &pReqLayout->FieldArray_[iFld]; typeId.Clear(); byte *p = fld->TypeId_; while (*p != 0) { typeId.Append((char)*p++); } Console.WriteLine($"\t[{iFld,2}] {typeId,-6} {fld->Named_.Name_,-10} = '{req.Fields_[iFld]}'"); } }
static unsafe int GetFieldIndex(string tvstr, ref int ifrom, f9oms.Layout *pReqLayout) { UInt32 retval; if (Char.IsDigit(tvstr, ifrom)) { retval = UIntTryParse(tvstr, ref ifrom); if (retval >= pReqLayout->FieldCount_) { Console.WriteLine($"Unknwon field index = {retval}"); return(-1); } } else { int ispl = tvstr.IndexOf('=', ifrom); string fldName = (ifrom <= ispl ? tvstr.Substring(ifrom, ispl - ifrom) : string.Empty); for (retval = 0; retval < pReqLayout->FieldCount_; ++retval) { if (fldName == pReqLayout->FieldArray_[retval].Named_.Name_.ToString()) { goto __FOUND_FIELD; } } Console.WriteLine($"Field not found: {fldName}"); return(-1); __FOUND_FIELD :; ifrom = ispl; } if (tvstr[ifrom] != '=') { Console.WriteLine("Loss '=' for field."); return(-1); } ++ifrom; return((int)retval); }
unsafe f9oms.Layout *GetRequestLayout(string[] cmds, uint cmdidx) { if (this.Config_ == null) { Console.WriteLine("Config not ready."); return(null); } if (cmdidx >= cmds.Length) { Console.WriteLine("Require: ReqName"); return(null); } // Req(Id or Name) string reqName = cmds[cmdidx]; if (!string.IsNullOrEmpty(reqName)) { if (Char.IsDigit(reqName, 0)) { UInt32 id = 0; UInt32.TryParse(reqName, out id); if (id <= 0 || this.Config_->RequestLayoutCount_ < id) { Console.WriteLine($"Unknown request id = {reqName}"); return(null); } return(this.Config_->RequestLayoutArray_[id - 1]); } f9oms.Layout *pReqLayout = f9oms.Api.GetRequestLayout(this, ref *this.Config_, reqName); if (pReqLayout != null) { return(pReqLayout); } } Console.WriteLine($"Unknown request name = {reqName}"); return(null); }