public static NETADDR ConvertAddress(string AddressString) { NETADDR NetAddr = new NETADDR(); byte b; ushort u; if (!string.IsNullOrEmpty(AddressString)) { string[] strs = AddressString.Split(','); if (strs.Length == 5) { if (byte.TryParse(strs[0], out b)) { NetAddr.bureauCode = b; } if (byte.TryParse(strs[1], out b)) { NetAddr.unitType = b; } if (ushort.TryParse(strs[2], out u)) { NetAddr.unitId = u; } if (byte.TryParse(strs[3], out b)) { NetAddr.devType = b; } if (ushort.TryParse(strs[4], out u)) { NetAddr.devId = u; } } } return(NetAddr); }
public void SetLocalAddress(string LocalAddressString, bool p_ifSendHeartBeat, bool p_ifReplyHeartBeat) { this.m_ifSendHeartBeat = p_ifSendHeartBeat; this.m_ifReplyHeartBeat = p_ifReplyHeartBeat; NETADDR NetAddr = NetRouterClientHelper.ConvertAddress(LocalAddressString); this.m_LocalAddr = NetAddr; }
/// <summary> /// 将需要保持连接的远程地址添加到地址表 /// </summary> /// <param name="p_Address"></param> public void AddRemoteAddress(string p_Address) { NETADDR t_NetAddr = NetRouterClientHelper.ConvertAddress(p_Address); EndPointRecord t_EndPoint = new EndPointRecord(); t_EndPoint.m_NetAddr = t_NetAddr; t_EndPoint.m_AddressStr = p_Address; this.m_RemoteAddressMap.TryAdd(p_Address, t_EndPoint); }
//public void Send(string p_DstAddressStr, string p_Message) //{ // if (this.m_NetRouterClient == null) // { // this.m_StatusStrAction("尚未与网络路由服务器建立连接"); // return; // } // NETADDR NetAddr = NetRouterClientHelper.ConvertAddress(p_DstAddressStr); // List<NETADDR> adrlist = new List<NETADDR>(); // adrlist.Add(NetAddr); // //SENDMSG sMessage = new SENDMSG(ref p_Message, ref adrlist); // //for (int i = 0; i < 10; i++) // { // byte[] t_Data = Encoding.UTF8.GetBytes(p_Message); // int length = t_Data.Length; // byte[] t_LengthBytes = Int32ToByte(length); // byte[] t_SendData = new byte[4 + length]; // t_LengthBytes.CopyTo(t_SendData, 0); // t_Data.CopyTo(t_SendData, 4); // SENDMSG tMessage = new SENDMSG(ref t_SendData, ref adrlist); // if (!m_NetRouterClient.sendMessage(ref tMessage)) // { // if (this.m_StatusStrAction != null) // { // this.m_StatusStrAction("发送错误!"); // } // } // else // { // if (this.m_StatusStrAction != null) // { // this.m_StatusStrAction("发送成功!"); // } // } // } //} /// <summary> /// 添加头部,并发送 /// </summary> /// <param name="p_NetAddr"></param> /// <param name="p_Data"></param> /// <param name="p_ifNeedHeader"></param> public void Send(NETADDR p_NetAddr, byte[] p_Data, bool p_ifNeedHeader = true) { if (!m_Connected) { if (this.m_StatusStrAction != null) { this.m_StatusStrAction("尚未与网络路由服务器建立连接"); return; } } try { List <NETADDR> adrlist = new List <NETADDR>(); adrlist.Add(p_NetAddr); byte[] t_SendData; int length = p_Data.Length; if (p_ifNeedHeader) { byte[] t_LengthBytes = Int32ToByte(length); t_SendData = new byte[4 + length]; t_LengthBytes.CopyTo(t_SendData, 0); p_Data.CopyTo(t_SendData, 4); } else { t_SendData = p_Data; } SENDMSG tMessage = new SENDMSG(ref t_SendData, ref adrlist); if (!m_NetRouterClient.sendMessage(ref tMessage)) { if (this.m_StatusStrAction != null) { this.m_StatusStrAction("发送错误!"); } } else { if (p_Data != this.m_HeartBeatData && p_Data != this.m_HeartBeatReply) { if (this.m_StatusStrAction != null) { this.m_StatusStrAction("成功发送" + length + "字节的数据"); } } } } catch (Exception e) { if (this.m_StatusStrAction != null) { this.m_StatusStrAction(e.Message); return; } } }
public void SetLocalAddress(byte BureauCode, byte UnitType, ushort UnitId, byte DevType, ushort DevId) { this.m_LocalAddr = new NETADDR(); this.m_LocalAddr.bureauCode = BureauCode; this.m_LocalAddr.unitType = UnitType; this.m_LocalAddr.unitId = UnitId; this.m_LocalAddr.devType = DevType; this.m_LocalAddr.devId = DevId; }
public static string ConvertAddress(NETADDR p_NetAddr) { StringBuilder sb = new StringBuilder(); sb.Append(p_NetAddr.bureauCode); sb.Append(','); sb.Append(p_NetAddr.unitType); sb.Append(','); sb.Append(p_NetAddr.unitId); sb.Append(','); sb.Append(p_NetAddr.devType); sb.Append(','); sb.Append(p_NetAddr.devId); return(sb.ToString()); }
public void Send(string p_DstAddressStr, byte[] p_Data) { try { NETADDR NetAddr = NetRouterClientHelper.ConvertAddress(p_DstAddressStr); Send(NetAddr, p_Data); } catch (Exception e) { if (this.m_StatusStrAction != null) { this.m_StatusStrAction(e.Message); return; } } }
static void Main(string[] args) { NETADDR localaddr = new NETADDR(); localaddr.bureauCode = 8; localaddr.unitType = 2; localaddr.unitId = 0; localaddr.devType = 1; localaddr.devId = 6; INetRouterClient netRouterClient = NetRouterClientFactory.CreateNetRouterClient("Test", "172.168.0.1", 9003, "172.168.0.1", 9005, ref localaddr, ""); while (!netRouterClient.start()) { Console.Write("Start fails.\n"); Thread.Sleep(10); } Console.Write("Start succeeds.\n"); REVMSG recvMsg = new REVMSG(); while (true) { while (netRouterClient.receiveMessage(ref recvMsg)) { string revmsgTemp = System.Text.Encoding.Default.GetString(recvMsg.msg, 0, 5); Console.WriteLine(revmsgTemp); } } Console.ReadKey(); }
static void Main(string[] args) { NETADDR localaddr = new NETADDR(); localaddr.bureauCode = 8; localaddr.unitType = 2; localaddr.unitId = 0; localaddr.devType = 1; localaddr.devId = 7; NETADDR remote1 = new NETADDR(); remote1.bureauCode = 8; remote1.unitType = 2; remote1.unitId = 0; remote1.devType = 1; remote1.devId = 5; NETADDR remote2 = new NETADDR(); remote2.bureauCode = 8; remote2.unitType = 2; remote2.unitId = 0; remote2.devType = 1; remote2.devId = 6; Console.WriteLine("Sender!!!!!!"); INetRouterClient netRouterClient = NetRouterClientFactory.CreateNetRouterClient("Test", "172.168.0.1", 9003, "172.168.0.1", 9005, ref localaddr, ""); while (!netRouterClient.start()) { Console.Write("Start fails.\n"); Thread.Sleep(10); } Console.Write("Start succeeds.\n"); List <NETADDR> adrlist = new List <NETADDR>(); adrlist.Add(remote1); adrlist.Add(remote2); string s = "Hello"; byte[] data = new byte[65000]; for (int i = 0; i < data.Length; i++) { data[i] = 1; } SENDMSG sMessage = new SENDMSG(ref data, ref adrlist); while (true) { if (!netRouterClient.sendMessage(ref sMessage)) { Console.Write("Send Wrong!\n"); } Thread.Sleep(2000); } Console.ReadKey(); }
private void ReceiveThreadMethod() { while (!m_ThreadClose) { try { if (this.m_NetRouterClient != null) { if (this.m_ConnectStatus1 || this.m_ConnectStatus2) { REVMSG recvMsg = new REVMSG(); while (m_NetRouterClient.receiveMessage(ref recvMsg)) { byte[] t_ReceiveData = recvMsg.msg; if (t_ReceiveData != null) { int t_DataLength = ByteToInt32(t_ReceiveData); bool t_NeedOperate = true; if (t_DataLength == 4) { if (t_ReceiveData[4] == m_HeartBeatData[0] && t_ReceiveData[5] == m_HeartBeatData[1] && t_ReceiveData[6] == m_HeartBeatData[2] && t_ReceiveData[7] == m_HeartBeatData[3]) { t_NeedOperate = false; DateTime dt = DateTime.Now; if (this.m_StatusStrAction != null) { this.m_StatusStrAction(dt.ToLongTimeString() + " 收到心跳信息包"); } if (m_ifReplyHeartBeat) { SendHeartBeatReply(recvMsg.srcAddr); } } else if (t_ReceiveData[4] == m_HeartBeatReply[0] && t_ReceiveData[5] == m_HeartBeatReply[1] && t_ReceiveData[6] == m_HeartBeatReply[2] && t_ReceiveData[7] == m_HeartBeatReply[3]) { t_NeedOperate = false; DateTime dt = DateTime.Now; if (this.m_StatusStrAction != null) { this.m_StatusStrAction(dt.ToLongTimeString() + " 收到心跳信息应答包"); } NETADDR t_SrcAddr = recvMsg.srcAddr; string t_AddrStr = NetRouterClientHelper.ConvertAddress(t_SrcAddr); EndPointRecord t_EndPoint; if (this.m_RemoteAddressMap.TryGetValue(t_AddrStr, out t_EndPoint)) { t_EndPoint.m_HeartBeatInterval = 0; } } } if (t_NeedOperate && this.m_ReceiveAction != null) { this.m_ReceiveAction(t_ReceiveData, 4, t_DataLength); } } } } } Thread.Sleep(10); } catch (ThreadAbortException e) { System.Console.WriteLine(Thread.CurrentThread.Name + "已被要求退出"); Thread.ResetAbort(); } catch (Exception e) { if (this.m_StatusStrAction != null) { this.m_StatusStrAction(e.Message); } } } }
private void SendHeartBeatReply(NETADDR p_NetAddr) { this.Send(p_NetAddr, m_HeartBeatReply); }
private void SendHeartBeatData(NETADDR p_NetAddr) { this.Send(p_NetAddr, m_HeartBeatData); }
private void ConnectionThreadMethod() { int count = 0; while (true) { try { INetRouterClient t_Client = this.m_NetRouterClient; if (t_Client != null) { bool isNet1Connected = t_Client.isNet1Connected(); if (this.m_ConnectStatus1 != isNet1Connected) { this.m_ConnectStatus1 = isNet1Connected; if (this.m_Connection1Action != null) { this.m_Connection1Action("网络连接1状态: " + isNet1Connected); } } bool isNet2Connected = t_Client.isNet2Connected(); if (this.m_ConnectStatus2 != isNet2Connected) { this.m_ConnectStatus2 = isNet2Connected; if (this.m_Connection2Action != null) { this.m_Connection2Action("网络连接2状态: " + isNet2Connected); } } if (!isNet1Connected && !isNet2Connected) { if (m_Connected) { m_Connected = false; if (this.m_ConnectingAction != null) { this.m_ConnectingAction(m_Connected); } } } else { if (!m_Connected) { m_Connected = true; if (this.m_ConnectingAction != null) { this.m_ConnectingAction(m_Connected); } } if (m_ifSendHeartBeat && count % 2 == 0) { foreach (EndPointRecord t_EndPoint in this.m_RemoteAddressMap.Values) { NETADDR t_Addr = t_EndPoint.m_NetAddr; t_EndPoint.m_HeartBeatInterval++; SendHeartBeatData(t_Addr); if (t_EndPoint.m_HeartBeatInterval > 5) { if (this.m_StatusStrAction != null) { this.m_StatusStrAction("[" + t_EndPoint.m_AddressStr + "]已断开"); } } } } } } count++; Thread.Sleep(1000); } catch (ThreadAbortException e) { System.Console.WriteLine(Thread.CurrentThread.Name + "已被要求退出"); Thread.ResetAbort(); } catch (Exception e) { if (this.m_StatusStrAction != null) { this.m_StatusStrAction(e.Message); } } } }
static void Main(string[] args) { NETADDR localaddr = new NETADDR(); localaddr.bureauCode = 8; localaddr.unitType = 2; localaddr.unitId = 0; localaddr.devType = 1; localaddr.devId = 6; INetRouterClient netRouterClient = NetRouterClientFactory.CreateNetRouterClient("Test", "191.168.99.98", 9003, "10.2.48.123", 9005, ref localaddr, ""); while (!netRouterClient.start()) { Console.Write("Start fails.\n"); Thread.Sleep(10); } Console.Write("Start succeeds.\n"); while (true) { if (netRouterClient.isNet1Connected() || netRouterClient.isNet2Connected()) { if (SENDNUM + 1 < SENDNUM) { SENDNUM = 0; } REVMSG recvMsg = new REVMSG(); List <SENDMSG> sendlist = new List <SENDMSG>(); while (netRouterClient.receiveMessage(ref recvMsg)) { string revmsgTemp = System.Text.Encoding.Default.GetString(recvMsg.msg, 0, recvMsg.msg.Length); Console.WriteLine("Rev len : " + recvMsg.msg.Length); Console.WriteLine("Rev (" + recvMsg.srcAddr.unitId + ":" + recvMsg.srcAddr.devId + ")---" + dataNum(revmsgTemp)); //Console.WriteLine("Rev (" + recvMsg.srcAddr.unitId + ":" // + recvMsg.srcAddr.devId + ")---" // + recvMsg.msg); String ack = (++SENDNUM).ToString(); ack += " Hello!"; //byte[] tempByte = Encoding.Default.GetBytes(ack); List <NETADDR> adrlist = new List <NETADDR>(); adrlist.Add(recvMsg.srcAddr); SENDMSG sMessage = new SENDMSG(ref ack, ref adrlist); sendlist.Add(sMessage); if (!netRouterClient.sendMessage(ref sMessage)) { Console.Write("Send Wrong!\n"); } else { Console.WriteLine("Send -------- " + ack + " : " + ack.ToString().Length + "\n"); } } } try { Thread.Sleep(10); } catch (Exception e) { // TODO Auto-generated catch block Console.Write(e.Message + "\n"); } } }