public bool Connect() { _connecting = true; _connect = false; SGIP_BIND connect = new SGIP_BIND((uint)LoginTypes.SpToSmg, Setting.LoginName, Setting.Password, Sequence.Instance.CreateID()); byte[] bytes = connect.ToBytes(); _client.Close(); while (_client.IsConnected) { Thread.Sleep(100); } if (_client.Connect()) { Thread.Sleep(1000); } else { _connecting = false; return(false); } try { Thread.Sleep(Setting.SendSpan); _client.Send(bytes); DebugLog.Instance.Write <SGIP_BIND>("Client Send -> SGIP_BIND", connect); } catch { _connecting = false; return(false); } autoConnectEvent.WaitOne(Setting.TimeOut * 1000, false); if (!_connect) { _client.Close(); OnSMSEvent(new SMSEventArgs(SMS_Event.SP_CONNECT_ERROR, "等待 CONNECT_RESP 超时")); _connecting = false; return(false); } _connecting = false; return(_connect); }
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); } }