public void AddRequest(CheDaoInterface req) { switch (req.message_type) { case CheDaoInterface.data_validation: CheRequest c_item = (CheRequest)req; this.Invoke((MethodInvoker)delegate { this._AddRequest(c_item);// run in UI thread }); break; case CheDaoInterface.print_confirm: ChePActionRequest p_item = (ChePActionRequest)req; this.Invoke((MethodInvoker)delegate { this.MoveToDoneList(p_item.Order_Number,p_item.Printed_Time,p_item.message_type);// run in UI thread }); break; case CheDaoInterface.delete_cmd: CheDeleteActionRequest d_item = (CheDeleteActionRequest)req; this.Invoke((MethodInvoker)delegate { this.MoveToDoneList(d_item.Order_Number, d_item.Printed_Time,d_item.message_type);// run in UI thread }); break; default: Trace.WriteLineIf(Program.trace_sw.TraceError, "incorrect message request received " + req.message_type.ToString()); break; } }
public void AddRequest(CheDaoInterface req) { switch (req.message_type) { case CheDaoInterface.data_validation: CheRequest c_item = (CheRequest)req; this.Invoke((MethodInvoker) delegate { this._AddRequest(c_item); // run in UI thread }); break; case CheDaoInterface.print_confirm: ChePActionRequest p_item = (ChePActionRequest)req; this.Invoke((MethodInvoker) delegate { this.MoveToDoneList(p_item.Order_Number, p_item.Printed_Time, p_item.message_type); // run in UI thread }); break; case CheDaoInterface.delete_cmd: CheDeleteActionRequest d_item = (CheDeleteActionRequest)req; this.Invoke((MethodInvoker) delegate { this.MoveToDoneList(d_item.Order_Number, d_item.Printed_Time, d_item.message_type); // run in UI thread }); break; default: Trace.WriteLineIf(Program.trace_sw.TraceError, "incorrect message request received " + req.message_type.ToString()); break; } }
private byte[] GenerateBytes(short type, CheDaoInterface req) { XmlDocument doc = new XmlDocument(); XmlSerializer xmlSerializer = new XmlSerializer((req.GetType())); MemoryStream body_str = new MemoryStream(1024); xmlSerializer.Serialize(body_str, req); int length = (int)body_str.Position; MemoryStream pkg_str = new MemoryStream(length + 6); byte[] tmp = System.BitConverter.GetBytes((int)length); Array.Reverse(tmp); pkg_str.Write(tmp, 0, 4); tmp = System.BitConverter.GetBytes(type); Array.Reverse(tmp); pkg_str.Write(tmp, 0, 2); pkg_str.Write(body_str.GetBuffer(), 0, length); return pkg_str.GetBuffer(); }
/* handle the message from network side*/ internal static byte[] HandlePackage(short cur_type, byte[] msg_body, int buffer_point) { LocalChePackage p = new LocalChePackage(cur_type, msg_body, buffer_point); CheDaoInterface msg = _HandlePackage(p); if (msg != null) { string rsp_msg = msg.Response(); Trace.WriteLineIf(Program.trace_sw.TraceVerbose, "send response: " + rsp_msg, "message"); byte[] body_buffer = Encoding.UTF8.GetBytes(rsp_msg); MemoryStream rsp_str = new MemoryStream(body_buffer.Length + 6); byte[] tmp = System.BitConverter.GetBytes((int)rsp_msg.Length); Array.Reverse(tmp); rsp_str.Write(tmp, 0, sizeof(int)); tmp = System.BitConverter.GetBytes((short)p.msg_type); Array.Reverse(tmp); rsp_str.Write(tmp, 0, sizeof(short)); rsp_str.Write(body_buffer, 0, body_buffer.Length); return(rsp_str.GetBuffer()); } return(null); }
public static void NewRequest(CheDaoInterface req) { mainForm.AddRequest(req); }
/** * make sure the function is re-entriable. **/ static CheDaoInterface _HandlePackage(LocalChePackage p) { CheDaoInterface msg = create(p); if (msg != null) { switch (msg.message_type) { case CheDaoInterface.data_validation: lock (mPendingList) { CheRequest item = (CheRequest)msg; try { //put to pending list mPendingList.Add(item.Order_Number, item); } catch (Exception e) { Trace.WriteLineIf(Program.trace_sw.TraceError, "Insert CheRequest Failed, maybe the key already exist " + e.Message, "error"); msg.Result = 2; //2:其它不符; } } break; case CheDaoInterface.clean_cmd: //delete the item one hour later int bypass_counter = 0; lock (mPendingList) { Dictionary <String, CheRequest> newPendingList = new Dictionary <String, CheRequest>(); foreach (String str in mPendingList.Keys) { CheRequest c_item = mPendingList[str]; DateTime c_date = new DateTime(c_item.create_time); if (c_date.AddMinutes(AppConfig.GetLifeTimeOfRec()) > DateTime.Now) { newPendingList.Add(str, c_item); } else { bypass_counter++; } } mPendingList = newPendingList; } Trace.WriteLineIf(Program.trace_sw.TraceVerbose, "clean up the internal cache and bypass " + bypass_counter.ToString()); break; case CheDaoInterface.scan_print: case CheDaoInterface.manual_print: lock (mPendingList) { ChePrintRequest p_item = (ChePrintRequest)msg; if (mPendingList.ContainsKey(p_item.Order_Number)) { CheRequest w_item = mPendingList[p_item.Order_Number]; mPendingList.Remove(p_item.Order_Number); Program.NewRequest(w_item); // to be print } else { Trace.WriteLineIf(Program.trace_sw.TraceError, "invalide print request, no key exist", "error"); msg.Result = 2; //2:已开票; } } break; case CheDaoInterface.print_confirm: Program.NewRequest(msg); //internal message break; case CheDaoInterface.delete_cmd: Program.NewRequest(msg); //internal message break; default: msg = null; break; } try { lock (bk_stream) { if (msg != null) { p.save(bk_stream); // just save the valid msg } } } catch (Exception e) { save_error_count++; Trace.WriteLineIf(Program.trace_sw.TraceError, "Write backup stream failed " + e.Message, "error"); } } return(msg); }