/// <summary> /// 发送数据包 /// </summary> /// <param name="sm">发送消息数据对象</param> public void Send(SendMessage sm) { try { byte[] bytes = sm.getBytes(); if (bytes == null) { Log.n("empty data to {" + sm.smsg.e + "}"); return; } UdpSendState uss = new UdpSendState(); uss.u = udp; uss.sm = sm; udpSent.Reset(); if (sm.smsg.type != SendType.Callback) { if (sm.time.Equals(DateTime.Parse("0001/1/1 0:00:00"))) { sm.time = DateTime.Now; MessageQueue.Get().Add(sm); } else { MessageQueue.Get().Update(sm); } } udp.BeginSend(bytes, bytes.Length, sm.smsg.e, new AsyncCallback(SendCallback), uss); udpSent.WaitOne(); } catch (Exception ex) { Log.n(ex.Message); } }
/// <summary> /// 发送回调方法 /// </summary> /// <param name="ar">回调参数</param> private void SendCallback(IAsyncResult ar) { UdpSendState u = (UdpSendState)ar.AsyncState; try { u.u.EndSend(ar); } catch (Exception ex) { Log.n("Exception:{" + u.sm.smsg.type.ToString() + "}" + ex.Message); Net.Get().Send(SendMessage.creatRunError("Data Send ERROR", u.sm.smsg.e)); return; } finally { udpSent.Set(); } }