private void OnResponse(byte[] data) { bool recvSucc = false; byte[] respData = null; string method = string.Empty; string errMsg = string.Empty; if (data != null && data.Length > 0) { Packet.Response resp = DataTranslater.Deserialize <Packet.Response>(data); method = resp.method; respData = resp.return_data; errMsg = resp.error; recvSucc = !string.IsNullOrEmpty(resp.method) && resp.invoke; } Delegate callback = GetResponseCallback(method); if (callback != null) { ResponseHandlerDel respCompletion = callback as ResponseHandlerDel; if (respCompletion != null) { respCompletion(recvSucc, respData, errMsg); } } }
private void OnEstablishConnectionAck(byte[] data) { bool attachExisting = false; if (data != null && data.Length > 0) { Packet.ConnectAck connAck = DataTranslater.Deserialize <Packet.ConnectAck>(data); IsConnected = connAck.success; SessionId = connAck.session; attachExisting = connAck.attach_existing; } Delegate callback = GetResponseCallback(typeof(Packet.Connect).Name); if (callback != null) { string errMsg = !IsConnected ? "交易终端拒绝连接" : ""; Action <bool, string, bool> connCompletion = callback as Action <bool, string, bool>; if (connCompletion != null) { connCompletion(IsConnected, errMsg, attachExisting); } } if (IsConnected) { _heartbeatTimer.Change(0, HEARTBEAT_INTERVAL); } }
private void EstablishConnection(Action <bool, string, bool> connectCompletionHandler) { Packet.Connect connReq = new Packet.Connect(); connReq.userid = AuthClientId; connReq.password = AuthPwd; byte[] data = DataTranslater.Serialize(connReq); BookRequest(typeof(Packet.Connect).Name, connectCompletionHandler); _connection.SendAsync(MsgType.CONN, data, delegate(bool succ, string msg) { if (succ) { Trace.WriteLine("Request establish connection"); } else { RemoveBookedRequest(typeof(Packet.Connect).Name); connectCompletionHandler( false, string.Format("Attempt to establish connection failed due to {0}", msg), false); } }); }
public void RequestAsync(string method, byte[] reqData, ResponseHandlerDel responseHandler) { Packet.Request req = new Packet.Request() { session = SessionId, method = method, param_data = reqData }; byte[] data = DataTranslater.Serialize(req); BookRequest(method, responseHandler); _connection.SendAsync(MsgType.REQ, data, delegate(bool succ, string msg) { if (succ) { Trace.WriteLine(string.Format("Request {0} success.", method)); } else { RemoveBookedRequest(method); responseHandler(false, null, msg); } }); }
private void HeartbeatProc(object state) { try { Packet.Heartbeat hb = new Packet.Heartbeat(); hb.timestamp = (int)DateTime.Now.TimeOfDay.TotalSeconds; byte[] data = DataTranslater.Serialize(hb); _connection.SendAsync(MsgType.HEARTBEAT, data, null); } catch (System.Exception ex) { Debug.WriteLine("Sending heartbeat failed due to {0}", ex.Message); } }
private void OnCallbackReq(byte[] callbackReqData) { byte[] paramData = null; string method = string.Empty; if (callbackReqData != null && callbackReqData.Length > 0) { Packet.CallbackReq cbReq = DataTranslater.Deserialize <Packet.CallbackReq>(callbackReqData); method = cbReq.method; paramData = cbReq.param_data; DispatchCallback(method, paramData); } }