123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- using System;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- namespace NetCore31BACNetTransfor
- {
- public class AsyncUdpClient : AsyncClientBase
- {
- private IPEndPoint localEP;
- private IPEndPoint remoteEP;
- private bool isAsync;
- private ILogger logger;
- private UdpClient udpClient;
- public bool shouldStop;
- public bool receiveStopped;
- public bool sendStopped;
- private bool WaitOne_Send;
- private bool WaitOne_Rece;
- public bool V { get; }
- public AsyncUdpClient(IPEndPoint localEP, IPEndPoint remoteEP, bool isAsync, ILogger logger) : base(1000)
- {
- this.localEP = localEP;
- this.remoteEP = remoteEP;
- this.isAsync = isAsync;
- this.logger = logger;
- }
- public AsyncUdpClient(IPEndPoint localEP, IPEndPoint remoteEP, bool isAsync, ILogger logger, int packetSize) : base(packetSize)
- {
- this.localEP = localEP;
- this.remoteEP = remoteEP;
- this.isAsync = isAsync;
- this.logger = logger;
- }
- public override bool IsConnected()
- {
- return false;
- }
- public override void Start()
- {
- this.udpClient = new UdpClient(this.localEP);
- this.udpClient.Connect(this.remoteEP);
- this.logger.Info("Started " + this.localEP.ToString() + " " + this.remoteEP.ToString());
- Thread thread = new Thread(new ThreadStart(this.ReceiveLoop));
- thread.Start();
- Thread thread2 = new Thread(new ThreadStart(this.SendLoop));
- thread2.Start();
- }
- public override void Stop()
- {
- this.shouldStop = true;
- while (!this.sendStopped || !this.receiveStopped)
- {
- Thread.Sleep(1);
- }
- if (this.udpClient != null)
- {
- this.udpClient.Close();
- }
- }
- private void ReceiveLoop()
- {
- this.receiveStopped = false;
- while (!this.shouldStop)
- {
- if (this.isAsync)
- {
- this.Receive();
- }
- else
- {
- this.ReceiveSync();
- }
- Thread.Sleep(1);
- }
- this.receiveStopped = true;
- }
- private void SendLoop()
- {
- this.sendStopped = false;
- while (!this.shouldStop)
- {
- string text = base.PopSend();
- if (text != null)
- {
- if (this.isAsync)
- {
- this.Send(text);
- }
- else
- {
- this.SendSync(text);
- }
- }
- Thread.Sleep(1);
- }
- this.sendStopped = true;
- }
- private void ReceiveSync()
- {
- try
- {
- IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Any, 0);
- byte[] bytes = this.udpClient.Receive(ref iPEndPoint);
- string str = Encoding.UTF8.GetString(bytes);
- base.PushReceive(str);
- this.logger.Info("rece:" + str);
- }
- catch (Exception ex)
- {
- this.logger.Error(ex.Message);
- }
- }
- private void SendSync(string bag)
- {
- try
- {
- byte[] bytes = Encoding.UTF8.GetBytes(bag);
- this.udpClient.Send(bytes, bytes.Length);
- this.logger.Info("send:" + bag);
- }
- catch (Exception ex)
- {
- this.logger.Error(ex.Message);
- }
- }
- private void Send(string msg)
- {
- byte[] bytes = Encoding.UTF8.GetBytes(msg);
- this.WaitOne_Send = false;
- this.udpClient.BeginSend(bytes, bytes.Length, new AsyncCallback(this.SendCallback), this.udpClient);
- while (!this.shouldStop)
- {
- if (this.WaitOne_Send)
- {
- this.logger.Info("send:" + msg);
- return;
- }
- Thread.Sleep(1);
- }
- }
- public void SendCallback(IAsyncResult iar)
- {
- UdpClient udpClient = iar.AsyncState as UdpClient;
- try
- {
- udpClient.EndSend(iar);
- this.WaitOne_Send = true;
- }
- catch (Exception)
- {
- }
- }
- public void Receive()
- {
- this.WaitOne_Rece = false;
- this.udpClient.BeginReceive(new AsyncCallback(this.ReceiveCallback), this.udpClient);
- while (!this.shouldStop)
- {
- if (this.WaitOne_Rece)
- {
- return;
- }
- Thread.Sleep(1);
- }
- }
- public void ReceiveCallback(IAsyncResult iar)
- {
- UdpClient udpClient = iar.AsyncState as UdpClient;
- try
- {
- IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Any, 0);
- byte[] bytes = udpClient.EndReceive(iar, ref iPEndPoint);
- this.WaitOne_Rece = true;
- string str = Encoding.UTF8.GetString(bytes);
- base.PushReceive(str);
- this.logger.Info("rece:" + str);
- }
- catch (Exception)
- {
- }
- }
- }
- }
|