Skip to content

Commit 1c005ff

Browse files
committed
下发数据添加返回结果
1 parent eb0ddd9 commit 1c005ff

3 files changed

Lines changed: 150 additions & 31 deletions

File tree

Lines changed: 98 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
using JT808.Gateway.Abstractions.Enums;
2-
using System;
3-
using System.Collections.Generic;
1+
using System;
2+
using System.Diagnostics.CodeAnalysis;
43
using System.Net.Sockets;
5-
using System.Text;
64
using System.Threading.Tasks;
5+
using JT808.Gateway.Abstractions.Enums;
76

87
namespace JT808.Gateway.Abstractions
98
{
@@ -17,59 +16,143 @@ public static class JT808SessionExtensions
1716
/// </summary>
1817
/// <param name="session"></param>
1918
/// <param name="data"></param>
20-
public static async void SendAsync(this IJT808Session session,byte[] data)
19+
public static async Task<Result> SendAsync(this IJT808Session session, byte[] data)
2120
{
21+
var result = new Result();
2222
try
2323
{
24-
if (data == null) return;
25-
if (session.TransportProtocolType == JT808TransportProtocolType.tcp)
24+
if (data == null)
25+
{
26+
result.Reason = FailReason.EmptyData;
27+
}
28+
else if (session.TransportProtocolType == JT808TransportProtocolType.tcp)
2629
{
2730
if (session.Client.Connected)
31+
{
2832
await session.Client.SendAsync(data, SocketFlags.None);
33+
result.Success = true;
34+
}
35+
else
36+
{
37+
result.Reason = FailReason.SessionNotConnected;
38+
}
2939
}
3040
else
3141
{
3242
await session.Client.SendToAsync(data, SocketFlags.None, session.RemoteEndPoint);
43+
result.Success = true;
3344
}
3445
}
3546
catch (AggregateException ex)
3647
{
37-
48+
result.Reason = FailReason.SessionInvalidAggregate;
49+
result.Exception = ex;
3850
}
39-
catch (Exception)
51+
catch (Exception ex)
4052
{
41-
53+
result.Reason = FailReason.Exception;
54+
result.Exception = ex;
4255
}
56+
return result;
4357
}
4458

4559
/// <summary>
4660
/// 下发消息
4761
/// </summary>
4862
/// <param name="session"></param>
4963
/// <param name="data"></param>
50-
public static void Send(this IJT808Session session, byte[] data)
64+
public static Result Send(this IJT808Session session, byte[] data)
5165
{
66+
var result = new Result();
5267
try
5368
{
54-
if (data == null) return;
55-
if (session.TransportProtocolType == JT808TransportProtocolType.tcp)
69+
if (data == null)
70+
{
71+
result.Reason = FailReason.EmptyData;
72+
}
73+
else if (session.TransportProtocolType == JT808TransportProtocolType.tcp)
5674
{
5775
if (session.Client.Connected)
76+
{
5877
session.Client.Send(data, SocketFlags.None);
78+
result.Success = true;
79+
}
80+
else
81+
{
82+
result.Reason = FailReason.SessionNotConnected;
83+
}
5984
}
6085
else
6186
{
6287
session.Client.SendTo(data, SocketFlags.None, session.RemoteEndPoint);
88+
result.Success = true;
6389
}
6490
}
6591
catch (AggregateException ex)
6692
{
67-
93+
result.Reason = FailReason.SessionInvalidAggregate;
94+
result.Exception = ex;
6895
}
69-
catch (Exception)
96+
catch (Exception ex)
97+
{
98+
result.Reason = FailReason.Exception;
99+
result.Exception = ex;
100+
}
101+
return result;
102+
}
103+
104+
public class UnknownErrorException : Exception
105+
{
106+
public UnknownErrorException() : base("An unknown error occurred.")
70107
{
71108

72109
}
73110
}
111+
/// <summary>
112+
/// 结果
113+
/// </summary>
114+
public class Result
115+
{
116+
/// <summary>
117+
/// 是否成功
118+
/// </summary>
119+
public bool Success { get; set; }
120+
/// <summary>
121+
/// 失败原因
122+
/// </summary>
123+
public FailReason Reason { get; set; } = FailReason.None;
124+
/// <summary>
125+
/// 异常
126+
/// </summary>
127+
[MemberNotNullWhen(false, nameof(Success))]
128+
public Exception Exception { get; set; }
129+
}
130+
131+
/// <summary>
132+
/// 失败原因
133+
/// </summary>
134+
public enum FailReason
135+
{
136+
/// <summary>
137+
/// 未知
138+
/// </summary>
139+
None,
140+
/// <summary>
141+
/// 空数据
142+
/// </summary>
143+
EmptyData,
144+
/// <summary>
145+
/// 非已连接状态
146+
/// </summary>
147+
SessionNotConnected,
148+
/// <summary>
149+
/// 无效的发送参数
150+
/// </summary>
151+
SessionInvalidAggregate,
152+
/// <summary>
153+
/// 未知异常
154+
/// </summary>
155+
Exception
156+
}
74157
}
75158
}

src/JT808.Gateway.Abstractions/JT808.Gateway.Abstractions.xml

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/JT808.Gateway/Session/JT808SessionManager.cs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,7 @@ public async ValueTask<bool> TrySendByTerminalPhoneNoAsync(string terminalPhoneN
172172
{
173173
if (TerminalPhoneNoSessions.TryGetValue(terminalPhoneNo, out var session))
174174
{
175-
if (session.TransportProtocolType == JT808TransportProtocolType.tcp)
176-
{
177-
await session.Client.SendAsync(data, SocketFlags.None);
178-
}
179-
else
180-
{
181-
await session.Client.SendToAsync(data, SocketFlags.None, session.RemoteEndPoint);
182-
}
175+
await session.SendAsync(data);
183176
return true;
184177
}
185178
else
@@ -192,14 +185,7 @@ public async ValueTask<bool> TrySendBySessionIdAsync(string sessionId, byte[] da
192185
{
193186
if (Sessions.TryGetValue(sessionId, out var session))
194187
{
195-
if (session.TransportProtocolType == JT808TransportProtocolType.tcp)
196-
{
197-
await session.Client.SendAsync(data, SocketFlags.None);
198-
}
199-
else
200-
{
201-
await session.Client.SendToAsync(data, SocketFlags.None, session.RemoteEndPoint);
202-
}
188+
await session.SendAsync(data);
203189
return true;
204190
}
205191
else

0 commit comments

Comments
 (0)