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 string resjson(short restype, object obj) //{ // switch (restype) // { // case 1 : // break; // default: // break; // } //} public void ThreadReqTask(Object obj) { UserReq req; SqlCommand cmd = null; UserSubRec rec = null; SqlConnection conn = null; SqlDataReader dr = null; while (ReqQueue.TryDequeue(out req)) { GD.reqlogger.Info(string.Format("IP-{0} ; FunID-{1} ; MsgID-{2} ; Req-{3}", req.clientinfo.ClientIP, req.FunctionID, req.MsgID, req.jsonreq)); switch (req.FunctionID) { case GD.constAddSubRec: try { rec = JsonConvert.DeserializeObject <UserSubRec>(req.jsonreq); }catch (Exception ex) { WriteExpResponse(req, GD.constExpJsonCode, ex.Message, ex); rec = null; } string addstr = "insert into UserSubRec (Channel, ID, Range, Symbol, Type, Condition, Value, Times, " + "Duration, UpdateTime) values (@Channel, @ID, @Range, @Symbol, @Type, @Condition, @Value, @Times, @Duration, @UpdateTime);SELECT CAST(scope_identity() AS int)"; if (GD.SubAppDbTool == null || rec == null) { return; } conn = GD.SubAppDbTool.GetDbConnection(); cmd = new SqlCommand(); cmd.Connection = conn; try { conn.Open(); //cmd.Parameters.Clear(); cmd.CommandText = addstr; cmd.Parameters.AddWithValue("@Channel", (object)req.Channel ?? DBNull.Value); /*if (!string.IsNullOrEmpty(rec.Channel)) * cmd.Parameters.AddWithValue("@Channel", rec.Channel); * else * cmd.Parameters.AddWithValue("@Channel", DBNull.Value);*/ if (!string.IsNullOrEmpty(rec.ID)) { cmd.Parameters.AddWithValue("@ID", rec.ID); } else { cmd.Parameters.AddWithValue("@ID", DBNull.Value); } if (!string.IsNullOrEmpty(rec.Range)) { cmd.Parameters.AddWithValue("@Range", rec.Range); } else { cmd.Parameters.AddWithValue("@Range", DBNull.Value); } if (!string.IsNullOrEmpty(rec.Symbol)) { cmd.Parameters.AddWithValue("@Symbol", rec.Symbol); } else { cmd.Parameters.AddWithValue("@Symbol", DBNull.Value); } if (!string.IsNullOrEmpty(rec.Type)) { cmd.Parameters.AddWithValue("@Type", rec.Type); } else { cmd.Parameters.AddWithValue("@Type", DBNull.Value); } if (!string.IsNullOrEmpty(rec.Condition)) { cmd.Parameters.AddWithValue("@Condition", rec.Condition); } else { cmd.Parameters.AddWithValue("@Condition", DBNull.Value); } if (!string.IsNullOrEmpty(rec.Value)) { cmd.Parameters.AddWithValue("@Value", rec.Value); } else { cmd.Parameters.AddWithValue("@Value", DBNull.Value); } if (!string.IsNullOrEmpty(rec.Times)) { cmd.Parameters.AddWithValue("@Times", rec.Times); } else { cmd.Parameters.AddWithValue("@Times", DBNull.Value); } if (!string.IsNullOrEmpty(rec.Duration)) { cmd.Parameters.AddWithValue("@Duration", rec.Duration); } else { cmd.Parameters.AddWithValue("@Duration", DBNull.Value); } if (!string.IsNullOrEmpty(req.DateTime)) { cmd.Parameters.AddWithValue("@UpdateTime", req.DateTime); } else { cmd.Parameters.AddWithValue("@UpdateTime", DBNull.Value); } //cmd.ExecuteNonQuery(); rec.NotifyMsgID = ((int)cmd.ExecuteScalar()).ToString().Trim(); WriteResponse(req, GD.constOKCode, "", rec); //ResAddSubRec resadd = new ResAddSubRec(); //resadd.ResultCode = "0000"; //resadd.ResultMessage = ""; //resadd.NotifyMsgID = rec.Serial; //string resbody = JsonConvert.SerializeObject(resadd); //byte[] resmsg = ResponseBytes(req.FunctionID, DateTime.Now.ToString("yyyyMMdd HH:mm:ss.fff"), req.MsgID, resbody); //NetworkStream ns = TCPConnection.GetStream(); //if (ns.CanWrite) //{ // ns.Write(resmsg, 0, resmsg.Length); //} //cmd.Parameters.Clear(); } catch (SqlException sqlex) { WriteExpResponse(req, GD.constExpSqlCode, sqlex.Message, sqlex); //ResAddSubRec resadd = new ResAddSubRec(); //resadd.ResultCode = GD.constExceptionSqlCode; //resadd.ResultMessage = sqlex.Message; //resadd.NotifyMsgID = ""; //string resbody = JsonConvert.SerializeObject(resadd); //byte[] resmsg = ResponseBytes(req.FunctionID, DateTime.Now.ToString("yyyyMMdd HH:mm:ss.fff"), req.MsgID, resbody); //NetworkStream ns = TCPConnection.GetStream(); //if (ns.CanWrite) //{ // ns.Write(resmsg, 0, resmsg.Length); //} } catch (SocketException socketex) { WriteExpResponse(req, GD.constExpSocketCode, socketex.Message, socketex); //ResAddSubRec resadd = new ResAddSubRec(); //resadd.ResultCode = GD.constExceptionSqlCode; //resadd.ResultMessage = socketex.Message; //resadd.NotifyMsgID = ""; //string resbody = JsonConvert.SerializeObject(resadd); //byte[] resmsg = ResponseBytes(req.FunctionID, DateTime.Now.ToString("yyyyMMdd HH:mm:ss.fff"), req.MsgID, resbody); //NetworkStream ns = TCPConnection.GetStream(); //if (ns.CanWrite) //{ // ns.Write(resmsg, 0, resmsg.Length); //} } catch (Exception ex) { WriteExpResponse(req, GD.constExpDefalutCode, ex.Message, ex); //ResAddSubRec resadd = new ResAddSubRec(); //resadd.ResultCode = GD.constExceptionSqlCode; //resadd.ResultMessage = ex.Message; //resadd.NotifyMsgID = ""; //string resbody = JsonConvert.SerializeObject(resadd); //byte[] resmsg = ResponseBytes(req.FunctionID, DateTime.Now.ToString("yyyyMMdd HH:mm:ss.fff"), req.MsgID, resbody); //NetworkStream ns = TCPConnection.GetStream(); //if (ns.CanWrite) //{ // ns.Write(resmsg, 0, resmsg.Length); //} } finally { if (cmd != null) { cmd.Dispose(); } conn.Close(); } break; case GD.constDelSubRec: try { rec = JsonConvert.DeserializeObject <UserSubRec>(req.jsonreq); } catch (Exception ex) { WriteExpResponse(req, GD.constExpJsonCode, ex.Message, ex); rec = null; } string delstr = "delete from UserSubRec where Channel=@Channel and ID=@ID and Serial=@NotifyMsgID"; if (GD.SubAppDbTool == null) { return; } conn = GD.SubAppDbTool.GetDbConnection(); cmd = new SqlCommand(); cmd.Connection = conn; try { conn.Open(); //cmd.Parameters.Clear(); cmd.CommandText = delstr; if (!string.IsNullOrEmpty(req.Channel)) { cmd.Parameters.AddWithValue("@Channel", req.Channel); } else { cmd.Parameters.AddWithValue("@Channel", DBNull.Value); } if (!string.IsNullOrEmpty(rec.ID)) { cmd.Parameters.AddWithValue("@ID", rec.ID); } else { cmd.Parameters.AddWithValue("@ID", DBNull.Value); } if (!string.IsNullOrEmpty(rec.NotifyMsgID)) { cmd.Parameters.AddWithValue("@NotifyMsgID", rec.NotifyMsgID); } else { cmd.Parameters.AddWithValue("@NotifyMsgID", DBNull.Value); } cmd.ExecuteNonQuery(); WriteResponse(req, GD.constOKCode, "", rec); } catch (SqlException sqlex) { WriteExpResponse(req, GD.constExpSqlCode, sqlex.Message, sqlex); } catch (SocketException socketex) { WriteExpResponse(req, GD.constExpSocketCode, socketex.Message, socketex); } catch (Exception ex) { WriteExpResponse(req, GD.constExpDefalutCode, ex.Message, ex); } finally { if (cmd != null) { cmd.Dispose(); } conn.Close(); } break; case GD.constQuerySubRec: try { rec = JsonConvert.DeserializeObject <UserSubRec>(req.jsonreq); } catch (Exception ex) { WriteExpResponse(req, GD.constExpJsonCode, ex.Message, ex); rec = null; } string qrystr = "select a.*, b.SymbolName, b.Market, b.TempRefer, b.Refer, b.CouponDate from UserSubRec a, " + "SubStockInfo b where a.Symbol=b.Symbol and b.CouponDate>=@CouponDate and a.ID=@ID " + "and a.Channel=@Channel order by a.Serial desc"; if (GD.SubAppDbTool == null) { return; } conn = GD.SubAppDbTool.GetDbConnection(); cmd = new SqlCommand(); cmd.Connection = conn; try { conn.Open(); //cmd.Parameters.Clear(); cmd.CommandText = qrystr; if (!string.IsNullOrEmpty(req.Channel)) { cmd.Parameters.AddWithValue("@Channel", req.Channel); } else { cmd.Parameters.AddWithValue("@Channel", DBNull.Value); } if (!string.IsNullOrEmpty(rec.ID)) { cmd.Parameters.AddWithValue("@ID", rec.ID); } else { cmd.Parameters.AddWithValue("@ID", DBNull.Value); } cmd.Parameters.AddWithValue("@CouponDate", DateTime.Today.ToString("yyyy/MM/dd")); dr = cmd.ExecuteReader(); WriteResponse(req, GD.constOKCode, "", rec, dr); } catch (SqlException sqlex) { WriteExpResponse(req, GD.constExpSqlCode, sqlex.Message, sqlex); } catch (SocketException socketex) { WriteExpResponse(req, GD.constExpSocketCode, socketex.Message, socketex); } catch (Exception ex) { WriteExpResponse(req, GD.constExpDefalutCode, ex.Message, ex); } finally { if (dr != null && cmd != null) { cmd.Cancel(); dr.Close(); cmd.Dispose(); } if (conn.State == ConnectionState.Open) { conn.Close(); } } break; } } //mrevent.Reset(); // mrevent.WaitOne(); }
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;*/ } }