/// <summary> /// 创建message包 /// </summary> /// <returns></returns> public static _TAG_NETPACK_MESSAGE CreateMessage(Int64 adress, Int64 identify) { _TAG_NETPACK_MESSAGE message = new _TAG_NETPACK_MESSAGE(); message._adressLong = adress; message._identifyLong = identify; return(message); }
private static void SendDgramMsg(string strFilePath, string strMachineID, Socket socket) { //数据包 _TAG_NETPACK_DISTINGUISHHEAD_VER1 pack = new _TAG_NETPACK_DISTINGUISHHEAD_VER1(); //基本包头 NETPACK_BASEHEAD_APPLICATION_VER1 baseHeadPack = new NETPACK_BASEHEAD_APPLICATION_VER1(); baseHeadPack._encrypt = DecDefine.NETPACK_ENCRYPT_NOTHING; baseHeadPack._format = DecDefine.NETPACK_BASEHEAD_VER1_FORMAT_JSON; JsonObject json = new JsonObject(); json["MachineID"] = new JsonProperty("\"" + strMachineID + "\""); json["ChangeID"] = new JsonProperty("\"" + System.DateTime.Now.Year.ToString() + string.Format("{0:00}", System.DateTime.Now.Month) + string.Format("{0:00}", System.DateTime.Now.Day) + string.Format("{0:00}", System.DateTime.Now.Hour) + string.Format("{0:00}", System.DateTime.Now.Minute) + string.Format("{0:00}", System.DateTime.Now.Second) + string.Format("{0:000}", System.DateTime.Now.Millisecond) + "\""); JsonObject jsonParam = new JsonObject(); jsonParam["ParamChange"] = new JsonProperty(json); baseHeadPack._validsize = (short)jsonParam.ToString().Length; baseHeadPack._datasize = (short)jsonParam.ToString().Length; byte[] byteData = Encoding.ASCII.GetBytes(jsonParam.ToString()); // string str = Encoding.ASCII.GetString(byteData); // UMPService00.IEventLog.WriteEntry("send message = "+str, EventLogEntryType.Warning); _TAG_NETPACK_ENCRYPT_CONTEXT encrypt = new _TAG_NETPACK_ENCRYPT_CONTEXT(); encrypt._encrypt = DecDefine.NETPACK_ENCRYPT_NOTHING; //识别头 _TAG_NETPACK_DISTINGUISHHEAD disHead = new _TAG_NETPACK_DISTINGUISHHEAD(); byte[] byteHeadPack = Common.StructToBytes(baseHeadPack); byte[] byteFollowData = new byte[byteHeadPack.Length + byteData.Length]; Array.Copy(byteHeadPack, byteFollowData, byteHeadPack.Length); Array.Copy(byteData, 0, byteFollowData, byteHeadPack.Length, byteData.Length); _TAG_NETPACK_MESSAGE message = Common.CreateMessage(0xffff, 0xffff, 0xffff, 0xffff, DecDefine.NETMSG_SMALLTYPE_COMMON_PARAM_NOTIFY, DecDefine.NETMSG_MIDTYPE_COMMON_PARAM, DecDefine.NETMSG_LARGTYPE_COMMON, DecDefine.NETMSG_NUMBER_COMMON_PARAM_NOTIFY_CHANGE); _TAG_NETPACK_DISTINGUISHHEAD_VER1 ReleasePack = Common.CreatePack(DecDefine.NETPACK_DISTHEAD_VER1, disHead, DecDefine.NETPACK_BASETYPE_APPLICATION_VER1, DecDefine.NETPACK_EXTTYPE_NOTHING, encrypt, (ushort)byteData.Length, message, 0xffff, 0xffff, DecDefine.NETPACK_PACKTYPE_APPLICATION, 0, 0, 0, -1); bool bIsSend = false; //UMPService00.IEventLog.WriteEntry("_TAG_NETPACK_DISTINGUISHHEAD_VER1 size = " + Marshal.SizeOf(typeof(_TAG_NETPACK_DISTINGUISHHEAD_VER1)) // +"mes length = "+byteFollowData.Length.ToString(), EventLogEntryType.Warning); for (int i = 0; i <= 3; i++) { if (bIsSend) { break; } try { SendMsg(ReleasePack, byteFollowData, socket); bIsSend = true; socket.Close(); } catch { bIsSend = false; Thread.Sleep(2 * 1000); } } }
public static _TAG_NETPACK_DISTINGUISHHEAD_VER1 CreatePack(byte strVersion, _TAG_NETPACK_DISTINGUISHHEAD DISTINGUISHHEAD, ushort BaseHead, ushort ExtHead, _TAG_NETPACK_ENCRYPT_CONTEXT encrypt, ushort validsize, _TAG_NETPACK_MESSAGE message, ushort module , ushort number, byte packtype, byte sequence, Int64 source, ushort state, Int64 target) { //计算识别头大小 short iDistHeadSize = (short)Marshal.SizeOf(DISTINGUISHHEAD); //计算基本包头大小 ushort baseheadsize = 0; switch (BaseHead) { case DecDefine.NETPACK_BASETYPE_NOTHING: baseheadsize = 0; break; case DecDefine.NETPACK_BASETYPE_CONNECT_ERROR: baseheadsize = (ushort)Marshal.SizeOf(typeof(_TAG_NETPACK_BASEHEAD_ERROR)); break; case DecDefine.NETPACK_BASETYPE_CONNECT_HELLO: baseheadsize = (ushort)Marshal.SizeOf(typeof(_TAG_NETPACK_BASEHEAD_CONNECT_HELLO)); break; case DecDefine.NETPACK_BASETYPE_CONNECT_WELCOME: baseheadsize = (ushort)Marshal.SizeOf(typeof(_TAG_NETPACK_BASEHEAD_CONNECT_WELCOME)); break; case DecDefine.NETPACK_BASETYPE_CONNECT_LOGON: baseheadsize = (ushort)Marshal.SizeOf(typeof(_TAG_NETPACK_BASEHEAD_CONNECT_LOGON)); break; case DecDefine.NETPACK_BASETYPE_CONNECT_AUTHEN: baseheadsize = (ushort)Marshal.SizeOf(typeof(_TAG_NETPACK_BASETYPE_CONNECT_AUTHEN)); break; case DecDefine.NETPACK_BASETYPE_REQ_ADDSUBSCRIBE: baseheadsize = (ushort)Marshal.SizeOf(typeof(_TAG_NETPACK_BASEHEAD_REQ_ADDSUBSCRIBE)); break; case DecDefine.NETPACK_BASETYPE_APPLICATION_VER1: baseheadsize = (ushort)Marshal.SizeOf(typeof(NETPACK_BASEHEAD_APPLICATION_VER1)); break; } //计算扩展包头大小 short extheadsize = (short)(ExtHead != DecDefine.NETPACK_EXTTYPE_NOTHING ? Marshal.SizeOf(ExtHead) : 0); // 计算包头部分大小(识别头+基本包头+扩展包头) int iHeadSize = iDistHeadSize + baseheadsize + extheadsize; //UMPService00.IEventLog.WriteEntry("iHeadSize = " + iHeadSize, EventLogEntryType.Warning); byte encrypeMethod = encrypt != null ? encrypt._encrypt : DecDefine.NETPACK_ENCRYPT_NOTHING; ushort dataSize = Common.get_netpack_encrypt_storesize(encrypeMethod, validsize); // UMPService00.IEventLog.WriteEntry("dataSize = " + dataSize, EventLogEntryType.Warning); short packsize = (short)(iHeadSize + dataSize); // UMPService00.IEventLog.WriteEntry("packsize = " + packsize, EventLogEntryType.Warning); _TAG_NETPACK_DISTINGUISHHEAD_VER1 netPack = new _TAG_NETPACK_DISTINGUISHHEAD_VER1(); netPack._basehead = BaseHead; netPack._basesize = baseheadsize; netPack._dist = DISTINGUISHHEAD; netPack._exthead = ExtHead; netPack._extsize = (ushort)extheadsize; netPack._followsize = (ushort)(packsize - iDistHeadSize); //UMPService00.IEventLog.WriteEntry("_followsize = " + netPack._followsize, EventLogEntryType.Warning); netPack._message = message; netPack._moduleid = module; netPack._number = number; netPack._packtype = packtype; netPack._sequence = sequence; netPack._source = source; netPack._datasize = dataSize; netPack._state = state; netPack._target = target; netPack._dist._flag = new char[2]; netPack._dist._flag[0] = DecDefine.NETPACK_FLAG[0]; netPack._dist._flag[1] = DecDefine.NETPACK_FLAG[1]; netPack._dist._version = strVersion; netPack._dist._cbsize = (byte)((char)Marshal.SizeOf(typeof(_TAG_NETPACK_DISTINGUISHHEAD_VER1))); netPack._timestamp = (long)(System.DateTime.Now.ToUniversalTime().Subtract(DateTime.Parse("1970-1-1")).TotalMilliseconds * 10); return(netPack); }