void _server_DataReceived(SocketAsyncEventArgs send, byte[] data) { try { SGIP_MESSAGE header = new SGIP_MESSAGE(data); switch (header.Command_Id) { case SGIP_COMMAND.SGIP_BIND: SGIP_BIND bind = new SGIP_BIND(data); DebugLog.Instance.Write <SGIP_BIND>("Server Received -> SGIP_BIND", bind); uint BindResult = 0; if (bind.LoginType == (uint)LoginTypes.SmgToSp || bind.LoginType == (uint)LoginTypes.SpToSmg) { if ((bind.LoginName == _setting.LocalLoginName) && (bind.LoginPassword == _setting.LocalLoginPassword)) { BindResult = 0; } else { BindResult = 1; } } else { BindResult = 4; } SGIP_BIND_RESP sgip_bind_resp = new SGIP_BIND_RESP(BindResult, header.SrcNodeSequence, header.DateSequence, header.Sequence_Id); ServerSend(send, sgip_bind_resp); DebugLog.Instance.Write <SGIP_BIND_RESP>("Server Send -> SGIP_BIND_RESP", sgip_bind_resp); break; case SGIP_COMMAND.SGIP_REPORT: // Thread.Sleep(100);//调试代码,上线去掉 SGIP_REPORT report = new SGIP_REPORT(data); DebugLog.Instance.Write <SGIP_REPORT>("Server Received -> SGIP_REPORT", report); SGIP_REPORT_RESP reportResp = new SGIP_REPORT_RESP(0, "", header.SrcNodeSequence, header.DateSequence, header.Sequence_Id); ServerSend(send, reportResp); DebugLog.Instance.Write <SGIP_REPORT_RESP>("Server Send -> SGIP_REPORT_RESP", reportResp); if (report.ReportType == 0) { bool result = false; ushort resultCode; string resultMsg = ""; string sTime = report.DateSequence.ToString().PadLeft(10, '0'); int mm = System.Convert.ToInt32(sTime.Substring(0, 2)); int dd = System.Convert.ToInt32(sTime.Substring(2, 2)); int hh = System.Convert.ToInt32(sTime.Substring(4, 2)); int m = System.Convert.ToInt32(sTime.Substring(6, 2)); int s = System.Convert.ToInt32(sTime.Substring(8, 2)); DateTime time = new DateTime(DateTime.Now.Year, mm, dd, hh, m, s); switch (report.State) { case 0: //成功 case 1: //状态报告返回,网关等待发送. result = true; resultCode = ((ushort)PlatformCode.SGIP + (ushort)SystemCode.ReportBack); break; default: resultCode = ((ushort)PlatformCode.SGIP + (ushort)SystemCode.ReportBack); resultMsg = report.ErrorCode.ToString(); break; } string serial = report.SrcNodeSequence.ToString() + report.DateSequence.ToString() + report.Sequence_Id.ToString(); ReportEventArgs args = new ReportEventArgs(serial, result, resultCode, resultMsg, time); ReportEvent(this, args); } break; case SGIP_COMMAND.SGIP_UNBIND: SGIP_UNBIND_RESP unbindResp = new SGIP_UNBIND_RESP(header.SrcNodeSequence, header.DateSequence, header.Sequence_Id); DebugLog.Instance.Write("Server Received -> SGIP_UNBIND"); ServerSend(send, unbindResp); DebugLog.Instance.Write <SGIP_UNBIND_RESP>("Server Send -> SGIP_UNBIND_RESP", unbindResp); break; case SGIP_COMMAND.SGIP_DELIVER: SGIP_DELIVER deliver = new SGIP_DELIVER(data); DebugLog.Instance.Write <SGIP_DELIVER>("Server Received -> SGIP_DELIVER", deliver); SGIP_DELIVER_RESP deliverResp = new SGIP_DELIVER_RESP(0, "", header.SrcNodeSequence, header.DateSequence, header.Sequence_Id); ServerSend(send, deliverResp); DebugLog.Instance.Write <SGIP_DELIVER_RESP>("Server Send -> SGIP_DELIVER_RESP", deliverResp); if (DeliverEvent != null) { string serial = header.SrcNodeSequence.ToString() + header.DateSequence.ToString() + header.Sequence_Id.ToString(); DeliverEvent(this, new DeliverEventArgs(serial, DateTime.Now, deliver.MessageContent, deliver.UserNumber, deliver.SPNumber, "")); } break; } } catch (Exception ex) { DebugLog.Instance.Write <Exception>("Server Received -> Exception", ex); } }
void _client_DataReceived(byte[] data) { SGIP_MESSAGE header; try { header = new SGIP_MESSAGE(data); } catch (Exception ex) { OnClientNetworkError(ex); return; } switch (header.Command_Id) { case SGIP_COMMAND.SGIP_BIND_RESP: SGIP_BIND_RESP sgip_bind_resp; try { sgip_bind_resp = new SGIP_BIND_RESP(data); } catch (Exception ex) { OnClientNetworkError(ex); return; } DebugLog.Instance.Write <SGIP_BIND_RESP>("Client Received -> SGIP_BIND_RESP", sgip_bind_resp); switch (sgip_bind_resp.Result) { case 0: _connect = true; autoConnectEvent.Set(); OnSMSEvent(new SMSEventArgs(SMS_Event.SP_CONNECT, "登录成功")); break; default: OnSMSEvent(new SMSEventArgs(SMS_Event.SP_CONNECT_ERROR, StateDictionary.StateRespDictionary(sgip_bind_resp.Result))); break; } break; case SGIP_COMMAND.SGIP_SUBMIT_RESP: SGIP_SUBMIT_RESP submit_resp; try { submit_resp = new SGIP_SUBMIT_RESP(data); } catch (Exception ex) { OnClientNetworkError(ex); return; } DebugLog.Instance.Write <SGIP_SUBMIT_RESP>("Client Received -> SGIP_SUBMIT_RESP", submit_resp); string serial = header.SrcNodeSequence.ToString() + header.DateSequence.ToString() + header.Sequence_Id.ToString(); SlidingWindow send = SWGet(serial); if (send.Status == WindowStatus.Idle) { return; } SWClr(serial); if (SendEvent != null) { send.Report.Succeed = false; switch (submit_resp.Result) { case 0: send.Report.Succeed = true; break; default: send.Report.Describe = StateDictionary.StateRespDictionary(submit_resp.Result); break; } send.Report.Serial = serial; SendEvent(this, send.Report); } break; case SGIP_COMMAND.SGIP_UNBIND: SGIP_UNBIND_RESP unbindResp; try { unbindResp = new SGIP_UNBIND_RESP(header.SrcNodeSequence, header.DateSequence, header.Sequence_Id); } catch (Exception ex) { OnClientNetworkError(ex); return; } DebugLog.Instance.Write("Client Received -> SGIP_UNBIND"); ClientSend(unbindResp); DebugLog.Instance.Write <SGIP_UNBIND_RESP>("Client Send -> SGIP_UNBIND_RESP", unbindResp); Thread.Sleep(1000); _connect = false; break; } }