|
1 | 1 | using Quick.Protocol.Utils; |
2 | 2 | using System; |
3 | 3 | using System.Collections.Generic; |
4 | | -using System.IO; |
5 | 4 | using System.Net; |
6 | 5 | using System.Net.Sockets; |
7 | 6 | using System.Text; |
8 | 7 | using System.Threading; |
9 | 8 | using System.Threading.Tasks; |
| 9 | +using UdpAsTcp; |
10 | 10 |
|
11 | 11 | namespace Quick.Protocol.Udp |
12 | 12 | { |
13 | 13 | public class QpUdpServer : QpServer |
14 | 14 | { |
15 | | - private UdpClient udpServer; |
| 15 | + private UdpAsTcpListener udpAsTcpListener; |
16 | 16 | private QpUdpServerOptions options; |
17 | | - public IPEndPoint ListenEndPoint { get; private set; } |
18 | 17 | public QpUdpServer(QpUdpServerOptions options) : base(options) |
19 | 18 | { |
20 | 19 | this.options = options; |
21 | 20 | } |
22 | 21 |
|
23 | 22 | public override void Start() |
24 | 23 | { |
25 | | - ListenEndPoint = new IPEndPoint(options.Address, options.Port); |
26 | | - udpServer = new UdpClient(ListenEndPoint); |
| 24 | + udpAsTcpListener = new UdpAsTcpListener(new IPEndPoint(options.Address, options.Port)); |
| 25 | + udpAsTcpListener.Start(); |
27 | 26 | base.Start(); |
28 | 27 | } |
29 | 28 |
|
30 | 29 | public override void Stop() |
31 | 30 | { |
32 | | - udpServer?.Close(); |
33 | | - udpServer?.Dispose(); |
34 | | - udpServer = null; |
| 31 | + udpAsTcpListener?.Stop(); |
| 32 | + udpAsTcpListener = null; |
35 | 33 | base.Stop(); |
36 | 34 | } |
37 | 35 |
|
38 | | - protected override Task InnerAcceptAsync(CancellationToken token) |
| 36 | + protected override async Task InnerAcceptAsync(CancellationToken token) |
39 | 37 | { |
40 | | - return udpServer.ReceiveAsync().ContinueWith(task => |
41 | | - { |
42 | | - if (task.IsCanceled) |
43 | | - return; |
44 | | - if (task.IsFaulted) |
45 | | - return; |
46 | | - var ret = task.Result; |
47 | | - var remoteEndPoint = ret.RemoteEndPoint; |
48 | | - var buffer = ret.Buffer; |
| 38 | + var udpAsTcpClient = await udpAsTcpListener.AcceptClientAsync(); |
49 | 39 |
|
50 | | - try |
51 | | - { |
52 | | - var remoteEndPointStr = "UDP:" + remoteEndPoint.ToString(); |
53 | | - if (LogUtils.LogConnection) |
54 | | - LogUtils.Log("[Connection]{0} connected.", remoteEndPointStr); |
55 | | - OnNewChannelConnected(new UdpClientStream(udpServer, remoteEndPoint, buffer), remoteEndPointStr, token); |
56 | | - } |
57 | | - catch (Exception ex) |
58 | | - { |
59 | | - if (LogUtils.LogConnection) |
60 | | - LogUtils.Log("[Connection]Init&Start Channel error,reason:{0}", ex.ToString()); |
61 | | - } |
62 | | - }); |
| 40 | + if (udpAsTcpClient == null) |
| 41 | + return; |
| 42 | + try |
| 43 | + { |
| 44 | + var remoteEndPointStr = "UDP:" + udpAsTcpClient.RemoteEndPoint.ToString(); |
| 45 | + if (LogUtils.LogConnection) |
| 46 | + LogUtils.Log("[Connection]{0} connected.", remoteEndPointStr); |
| 47 | + OnNewChannelConnected(udpAsTcpClient.GetStream(), remoteEndPointStr, token); |
| 48 | + } |
| 49 | + catch (Exception ex) |
| 50 | + { |
| 51 | + if (LogUtils.LogConnection) |
| 52 | + LogUtils.Log("[Connection]Init&Start Channel error,reason:{0}", ex.ToString()); |
| 53 | + try { udpAsTcpClient.Close(); } |
| 54 | + catch { } |
| 55 | + } |
63 | 56 | } |
64 | 57 | } |
65 | 58 | } |
0 commit comments