1- using JT808 . Gateway . Abstractions . Enums ;
2- using System ;
3- using System . Collections . Generic ;
1+ using System ;
2+ using System . Diagnostics . CodeAnalysis ;
43using System . Net . Sockets ;
5- using System . Text ;
64using System . Threading . Tasks ;
5+ using JT808 . Gateway . Abstractions . Enums ;
76
87namespace 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}
0 commit comments