コード例 #1
0
ファイル: UdpServer.cs プロジェクト: youngbeangeek/X
        /// <summary>发送数据包到目的地址</summary>
        /// <param name="pk"></param>
        /// <param name="remote"></param>
        /// <param name="wait"></param>
        /// <returns></returns>
        internal async Task <Packet> SendAsync(Packet pk, IPEndPoint remote, Boolean wait)
        {
            if (pk.Count > 0)
            {
                if (remote != null && remote.Address == IPAddress.Broadcast && !Client.EnableBroadcast)
                {
                    Client.EnableBroadcast = true;
                    // 广播匹配任意响应
                    remote = null;
                }
            }

            if (Packet == null)
            {
                Packet = new PacketProvider();
            }

            var task = !wait ? null : Packet.Add(pk, remote, Timeout);

            // 这里先发送,基类的SendAsync注定发给Remote而不是remote
            if (!SendByQueue(pk, remote))
            {
                return(null);
            }

            if (!wait)
            {
                return(null);
            }

            return(await task.ConfigureAwait(false));
        }
コード例 #2
0
        /// <summary>异步发送数据并等待响应</summary>
        /// <param name="pk"></param>
        /// <returns></returns>
        public virtual async Task <Packet> SendAsync(Packet pk)
        {
            if (!Open())
            {
                return(null);
            }

            if (Packet == null)
            {
                Packet = new PacketProvider();
            }

            var task = Packet.Add(pk, null, Timeout);

            if (pk != null)
            {
                WriteLog("SendAsync:{0}", pk.ToHex());

                // 发送数据
                Serial.Write(pk.Data, pk.Offset, pk.Count);
            }

            return(await task);
        }