예제 #1
0
        private void Send(object st, Type sttype, EnumProtocolType protocoltype)
        {
            IntPtr plogin = Marshal.AllocHGlobal(Marshal.SizeOf(st));

            Marshal.StructureToPtr(st, plogin, false);
            byte[] body = new byte[Marshal.SizeOf(st)];
            Marshal.Copy(plogin, body, 0, Marshal.SizeOf(st));
            Marshal.FreeHGlobal(plogin);

            HEAD hd = CreateHeaderByProtocolType(protocoltype);

            hd.len = (short)(8 + body.Length);
            short crc = (short)((hd.len & 0xff) + ((hd.len >> 8) & 0xff) + hd.messagetype + hd.recvaddr + hd.recvtype + hd.sendaddr + hd.sendtype + hd.version + hd.workflow);

            for (int i = 0; i < body.Length; i++)
            {
                crc += (short)body[i];
            }
            //hd.len = System.Net.IPAddress.HostToNetworkOrder(hd.len);
            IntPtr phd = Marshal.AllocHGlobal(Marshal.SizeOf(hd));

            Marshal.StructureToPtr(hd, phd, false);
            byte[] protocol = new byte[Marshal.SizeOf(hd) + body.Length + 2];
            Marshal.Copy(phd, protocol, 0, Marshal.SizeOf(hd));
            Array.Copy(body, 0, protocol, Marshal.SizeOf(hd), body.Length);
            crc = System.Net.IPAddress.HostToNetworkOrder(crc);
            protocol[protocol.Length - 2] = (byte)((crc >> 8) & 0xff);
            protocol[protocol.Length - 1] = (byte)(crc & 0xff);
            Marshal.FreeHGlobal(phd);
            m_tcpManager.SendData(protocol);
        }
예제 #2
0
        /// <summary>
        /// 摘要:依据协议类型,创建包头
        /// </summary>
        /// <param name="ptype">协议类型</param>
        /// <returns>协议包包头</returns>
        private static HEAD CreateHeaderByProtocolType(EnumProtocolType ptype)
        {
            HEAD h = new HEAD();

            h.flag1       = 0x58;
            h.flag2       = 0x44;
            h.messagetype = (byte)ptype;
            h.version     = 0x01;
            h.workflow    = unchecked (workflow++);
            h.recvaddr    = 0;
            h.recvtype    = 0;
            h.sendaddr    = 0;
            h.sendtype    = 0;

            return(h);
        }
예제 #3
0
        private void Send(object st, Type sttype, EnumProtocolType protocoltype)
        {
            IntPtr plogin = Marshal.AllocHGlobal(Marshal.SizeOf(st));

            Marshal.StructureToPtr(st, plogin, false);
            byte[] body = new byte[Marshal.SizeOf(st)];
            Marshal.Copy(plogin, body, 0, Marshal.SizeOf(st));
            Marshal.FreeHGlobal(plogin);

            HEAD hd = CreateHeaderByProtocolType(protocoltype);

            hd.len = (short)(8 + body.Length);
            short crc = (short)((hd.len & 0xff) + ((hd.len >> 8) & 0xff) + hd.messagetype + hd.recvaddr + hd.recvtype + hd.sendaddr + hd.sendtype + hd.version + hd.workflow);

            for (int i = 0; i < body.Length; i++)
            {
                crc += (short)body[i];
            }
            //hd.len = System.Net.IPAddress.HostToNetworkOrder(hd.len);
            IntPtr phd = Marshal.AllocHGlobal(Marshal.SizeOf(hd));

            Marshal.StructureToPtr(hd, phd, false);
            byte[] protocol = new byte[Marshal.SizeOf(hd) + body.Length + 2];
            Marshal.Copy(phd, protocol, 0, Marshal.SizeOf(hd));
            Array.Copy(body, 0, protocol, Marshal.SizeOf(hd), body.Length);
            crc = System.Net.IPAddress.HostToNetworkOrder(crc);
            protocol[protocol.Length - 2] = (byte)((crc >> 8) & 0xff);
            protocol[protocol.Length - 1] = (byte)(crc & 0xff);
            Marshal.FreeHGlobal(phd);

            StringBuilder sb = new StringBuilder();

            foreach (var item in protocol)
            {
                sb.Append(item.ToString("X2") + " ");
            }
            MyLog4Net.ILogExtension.DebugWithDebugView(MyLog4Net.Container.Instance.Log, "m_tcpManager_SendeData ret:" + sb.ToString());


            m_tcpManager.SendData(protocol);



            //System.Diagnostics.Trace.WriteLine(string.Format("发送数据:{0}", protocol));
        }