コード例 #1
0
ファイル: Program.cs プロジェクト: TimYun95/TcpMsgServer
        /// <summary>
        /// TCP协议打包数据
        /// </summary>
        /// <param name="dataContent">待打包的数据</param>
        /// <param name="functionalNum">功能码</param>
        /// <returns>打包后的数据</returns>
        static byte[] EnpackTCP(byte[] dataContent, TCPProtocolKey functionalNum)
        {
            List <byte> sendBytes = new List <byte>(8 + dataContent.Length);

            sendBytes.Add(header1);
            sendBytes.Add(header2);
            sendBytes.Add(remoteDeviceIndex.HasValue ? remoteDeviceIndex.Value : byte.MinValue);
            sendBytes.Add((byte)functionalNum);
            sendBytes.AddRange(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(dataContent.Length)));
            sendBytes.AddRange(dataContent);

            return(sendBytes.ToArray());
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: TimYun95/TcpMsgServer
        /// <summary>
        /// 处理TCP接收的数据
        /// </summary>
        /// <param name="datas">所收数据</param>
        static void DealWithTcpTransferRecieveDatas(byte[] datas)
        {
            int dataLength = 0;

            if (datas.Length > 8)
            {
                dataLength = Convert.ToInt32(IPAddress.NetworkToHostOrder(
                                                 BitConverter.ToInt32(datas, (byte)TCPProtocol.DataLength)));
                if (dataLength != datas.Length - 8)
                {
                    return;                                 // 数据段长度不匹配
                }
            }
            else
            {
                if (datas.Length != 4)
                {
                    return;                     // 总长度不匹配
                }
            }

            if (datas[(byte)TCPProtocol.Header1] != header1 ||
                datas[(byte)TCPProtocol.Header2] != header2)
            {
                return;                                                                        // 协议头不匹配
            }
            byte           deviceIndex = datas[(byte)TCPProtocol.DeviceID];                    // 设备号
            TCPProtocolKey protocolKey = (TCPProtocolKey)datas[(byte)TCPProtocol.ProtocolKey]; // 标志位

            if (Object.Equals(remoteDeviceIndex, null) && !protocolKey.Equals(TCPProtocolKey.RSAPublicKey))
            {
                return;                                                                                             // 尚未获得设备号 不允许接收RSA公钥以外的消息
            }
            switch (protocolKey)
            {
            case TCPProtocolKey.RSAPublicKey:
                if (dataLength != (int)SecurityKeyLength.RSAKeyLength)
                {
                    return;                          // RSA公钥长度不匹配
                }
                remoteDeviceIndex     = deviceIndex; // RSA传送时发送设备号
                remoteDevicePublicKey = Encoding.UTF8.GetString(datas, (byte)TCPProtocol.DataContent, dataLength);

                // 发送AES公钥
                SendAESKey();

                Logger.HistoryPrinting(Logger.Level.INFO, MethodBase.GetCurrentMethod().DeclaringType.FullName, "RSAKey saved, AES sent, TCP can be used to transfer.");
                break;

            case TCPProtocolKey.NormalData:
                if (remoteDeviceIndex != deviceIndex)
                {
                    return;                                       // 设备号不匹配
                }
                // 入队等待Pipe发送
                PushNormalData(datas.Skip((byte)TCPProtocol.DataContent).ToArray());

                Logger.HistoryPrinting(Logger.Level.INFO, MethodBase.GetCurrentMethod().DeclaringType.FullName, "Data enqueue for pipe.");
                break;

            case TCPProtocolKey.PingSignal:
                if (remoteDeviceIndex != deviceIndex)
                {
                    return;                // 设备号不匹配
                }
                tcpBeatClocker.Stop();     // 重开计时器
                tcpBeatClocker.Start();
                break;

            case TCPProtocolKey.EndRemoteControl:
                if (remoteDeviceIndex != deviceIndex)
                {
                    return;                  // 设备号不匹配
                }
                NormalEndTcpOperation();     // 收到指令进行一般TCP中断操作
                Logger.HistoryPrinting(Logger.Level.INFO, MethodBase.GetCurrentMethod().DeclaringType.FullName, "TCP transfer is stopped.");
                break;

            case TCPProtocolKey.EndBothControl:
                if (remoteDeviceIndex != deviceIndex)
                {
                    return;                                // 设备号不匹配
                }
                EndTcpOperationWithStopPipeTransfer();     // 收到指令中断TCP操作,同时停止Pipe传输
                Logger.HistoryPrinting(Logger.Level.INFO, MethodBase.GetCurrentMethod().DeclaringType.FullName, "TCP transfer is stopped and cut down pipe transfer at the same time.");
                break;

            case TCPProtocolKey.AESCommonKeyAndIV:
                break;

            default:
                Logger.HistoryPrinting(Logger.Level.INFO, MethodBase.GetCurrentMethod().DeclaringType.FullName, "No such control command.");
                break;
            }
        }