public void ThreadTask(UserReq reqobj) { switch (int.Parse(reqobj.FunctionID)) { case 6001: break; } }
private void WriteResponse(UserReq ureq, string resultcode, string resultmsg, UserSubRec urec, SqlDataReader dr) { string resbody = null; byte[] resmsg = null; NetworkStream ns = null; List <ResQrySubDetail> dlist = new List <ResQrySubDetail>(); switch (ureq.FunctionID) { case GD.constQuerySubRec: ResQrySubRec resqry = new ResQrySubRec(); resqry.ResultCode = resultcode; resqry.ResultMessage = resultmsg; resqry.ID = urec.ID; while (dr.Read()) { ResQrySubDetail item = new ResQrySubDetail(); item.NotifyMsgID = dr["Serial"].ToString(); item.Range = dr["Range"].ToString(); item.Symbol = dr["Symbol"].ToString(); item.Type = dr["Type"].ToString(); item.Condition = dr["Condition"].ToString(); item.Value = dr["Value"].ToString(); item.Times = dr["Times"].ToString(); item.Duration = dr["Duration"].ToString(); item.SymbolName = dr["SymbolName"].ToString(); item.Market = dr["Market"].ToString(); item.TempRefer = dr["TempRefer"].ToString(); item.Refer = dr["Refer"].ToString(); item.CouponDate = dr.GetDateTime(dr.GetOrdinal("CouponDate")).ToString("yyyy/MM/dd"); //Convert.ToDateTime(dr["CouponDate"]).ToString("yyyy/MM/dd") dlist.Add(item); } resqry.Detail = dlist.ToArray(); resbody = JsonConvert.SerializeObject(resqry); resmsg = ResponseBytes(ureq.FunctionID, DateTime.Now.ToString("yyyyMMdd HH:mm:ss.fff"), ureq.MsgID, ureq.Channel, resbody); ns = TCPConnection.GetStream(); if (ns.CanWrite) { ns.Write(resmsg, 0, resmsg.Length); } resmsg = null; GD.reqlogger.Info(string.Format("IP-{0} ; FunID-{1} ; MsgID-{2} ; Response-{3}", ureq.clientinfo.ClientIP, ureq.FunctionID, ureq.MsgID, resbody)); break; } }
private void WriteResponse(UserReq ureq, string resultcode, string resultmsg, UserSubRec urec) { string resbody = null; byte[] resmsg = null; NetworkStream ns = null; switch (ureq.FunctionID) { case GD.constAddSubRec: case GD.constDelSubRec: ResAddSubRec resadd = new ResAddSubRec(); resadd.ResultCode = resultcode; resadd.ResultMessage = resultmsg; resadd.NotifyMsgID = urec.NotifyMsgID; resbody = JsonConvert.SerializeObject(resadd); resmsg = ResponseBytes(ureq.FunctionID, DateTime.Now.ToString("yyyyMMdd HH:mm:ss.fff"), ureq.MsgID, ureq.Channel, resbody); ns = TCPConnection.GetStream(); if (ns.CanWrite) { ns.Write(resmsg, 0, resmsg.Length); } resmsg = null; GD.reqlogger.Info(string.Format("IP-{0} ; FunID-{1} ; MsgID-{2} ; Response-{3}", ureq.clientinfo.ClientIP, ureq.FunctionID, ureq.MsgID, resbody)); break; /*case GD.constDelSubRec: * ResAddSubRec resdel = new ResAddSubRec(); * resdel.ResultCode = resultcode; * resdel.ResultMessage = resultmsg; * resdel.NotifyMsgID = urec.NotifyMsgID; * resbody = JsonConvert.SerializeObject(resdel); * resmsg = ResponseBytes(ureq.FunctionID, DateTime.Now.ToString("yyyyMMdd HH:mm:ss.fff"), ureq.MsgID, resbody); * ns = TCPConnection.GetStream(); * if (ns.CanWrite) * { * ns.Write(resmsg, 0, resmsg.Length); * } * resmsg = null; * break;*/ } }
private void OnReadData(IAsyncResult ar) { ClientInfo cinfo = (ClientInfo)ar.AsyncState; int bytesRead = 0; byte[] testByte = new byte[1]; if (cinfo.TCPConnection == null || !cinfo.TCPConnection.Connected) { HandleForm.UISyncMsg(GD.Push_Exception, null, null, "Detect client disconnect"); HandleForm.UISyncMsg(GD.Push_RemoveClient, cinfo, null, null); return; } try { //使用Peek測試連線是否仍存在 if (cinfo.TCPConnection.Client.Poll(0, SelectMode.SelectRead) && cinfo.TCPConnection.Client.Receive(testByte, SocketFlags.Peek) == 0) { HandleForm.UISyncMsg(GD.Push_Exception, null, null, "Detect client disconnect"); HandleForm.UISyncMsg(GD.Push_RemoveClient, cinfo, null, null); //HandleForm.DisplayText("Detect client disconnect\n"); return; } } catch (SocketException ex) { if (ex.ErrorCode == 10054) { //HandleForm.UISyncMsg(GD.Push_Exception, ex, null, string.Format("{0} Disconnected!", cinfo.ClientIP)); HandleForm.UISyncMsg(GD.Push_RemoveClient, cinfo, null, null); } else { HandleForm.UISyncMsg(GD.Push_Exception, ex, null, string.Format("OnReadData Exception {0}", ex.Message)); } //HandleForm.DisplayText(string.Format("OnReadData Exception Happen {0}\n", ex.Message)); return; } NetworkStream stream = cinfo.TCPConnection.GetStream(); //int BodyLen = 0; //byte[] blenarray = new byte[4]; //block until data is available try { bytesRead = stream.EndRead(ar); } catch (SocketException ex) { HandleForm.UISyncMsg(GD.Push_Exception, ex, null, string.Format("stream.EndRead Exception {0}", ex.Message)); //HandleForm.DisplayText(string.Format("stream.EndRead Exception Happen {0}\n", ex.Message)); } if (bytesRead > 0) { //state.Data.Append(Encoding.UTF8.GetString(state.Buffer, 0, bytesRead)); try { Array.Copy(cinfo.buf, 0, cinfo.tmpbuf, cinfo.tailidx, bytesRead); } catch (Exception ex) { //MessageBox.Show(ex.Message); HandleForm.UISyncMsg(GD.Push_Exception, ex, null, string.Format("OnReadData Exception Happen {0}\n")); //HandleForm.DisplayText(string.Format("OnReadData Exception Happen {0}\n", ex.Message)); } cinfo.tailidx += bytesRead; try { while (true) { if ((cinfo.tailidx - cinfo.headidx) >= AppMsgHeader.HeaderLen) { Array.Clear(cinfo.MsgHeader.HeaderData, 0, AppMsgHeader.HeaderLen); Array.Copy(cinfo.tmpbuf, cinfo.headidx, cinfo.MsgHeader.HeaderData, 0, AppMsgHeader.HeaderLen); //BodyLen = GlobalData.TABodyLength(blenarray); //建名的訊息bodylength不含header,但大州的含header length,故資料PARSE邏輯不一樣 //cinfo.headidx += AppMsgHeader.HeaderLen; if (cinfo.MsgHeader.BodyLength == 0) { break; } if ((cinfo.tailidx - cinfo.headidx) >= (cinfo.MsgHeader.BodyLength + AppMsgHeader.HeaderLen)) { string jsonstr = Encoding.UTF8.GetString(cinfo.tmpbuf, cinfo.headidx + AppMsgHeader.HeaderLen, cinfo.MsgHeader.BodyLength); if (jsonstr.Length > 0) { UserReq req = new UserReq(); req.clientinfo = cinfo; req.FunctionID = Encoding.UTF8.GetString(cinfo.MsgHeader.FunctionID); req.DateTime = Encoding.UTF8.GetString(cinfo.MsgHeader.DateTime); req.DataLen = cinfo.MsgHeader.BodyLength; req.MsgID = Encoding.UTF8.GetString(cinfo.MsgHeader.MsgID).Trim(Convert.ToChar(0)); req.Channel = Encoding.UTF8.GetString(cinfo.MsgHeader.Channel).Trim(Convert.ToChar(0)); req.jsonreq = jsonstr; cinfo.ReqQueue.Enqueue(req); ThreadPool.QueueUserWorkItem(new WaitCallback(cinfo.ThreadReqTask)); //cinfo.mrevent.Set(); //req.FunctionID = //HandleForm.DisplayText(string.Format("BodyLength - {0}\r\n{1}", AppMsgHeader.BodyLength, jsonstr)); //Emps emps = JsonConvert.DeserializeObject<Emps>(jsonstr); //HandleForm.DisplayText(string.Format("\r\nfirstname : {0}\r\nlastname : {1}", emps.employees[0].firstname, emps.employees[0].lastname)); //HandleForm.DisplayText(string.Format("\r\nfirstname : {0}\r\nlastname : {1}", emps.employees[1].firstname, emps.employees[1].lastname)); } else { //HandleForm.DisplayText("Heart Beat!!"); } cinfo.headidx += cinfo.MsgHeader.BodyLength + AppMsgHeader.HeaderLen; } else { int leftsize = cinfo.tailidx - cinfo.headidx; Array.Copy(cinfo.tmpbuf, cinfo.headidx, cinfo.tmpbuf, 0, leftsize); cinfo.headidx = 0; cinfo.tailidx = leftsize; Array.Clear(cinfo.tmpbuf, cinfo.tailidx, cinfo.tmpbuf.Length - leftsize); break; } } else { int leftsize = cinfo.tailidx - cinfo.headidx; Array.Copy(cinfo.tmpbuf, cinfo.headidx, cinfo.tmpbuf, 0, leftsize); cinfo.headidx = 0; cinfo.tailidx = leftsize; Array.Clear(cinfo.tmpbuf, cinfo.tailidx, cinfo.tmpbuf.Length - leftsize); break; } }//while stream.BeginRead(cinfo.buf, 0, cinfo.bufsize, new AsyncCallback(OnReadData), cinfo); } catch (Exception ex) { HandleForm.UISyncMsg(GD.Push_Exception, ex, null, string.Format("EndReadCallback Exception {0}", ex.Message)); //HandleForm.DisplayText(string.Format("EndReadCallback Exception Happen {0}\n", ex.Message)); return; } } else { HandleForm.UISyncMsg(GD.Push_Exception, null, null, "Detect client disconnect read 0 byte\n"); //HandleForm.DisplayText("Detect client disconnect read 0 byte\n"); return; } }