NetworkUdpBase.cs
2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using HslCommunication.BasicFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace HslCommunication.Core.Net
{
/// <summary>
/// 基础的Udp的通信对象
/// </summary>
public class NetworkUdpBase : NetworkBase
{
/// <summary>
/// 实例化一个默认的方法
/// </summary>
public NetworkUdpBase( )
{
hybirdLock = new SimpleHybirdLock( );
ReceiveTimeout = 5000;
}
/// <summary>
/// Ip地址
/// </summary>
public virtual string IpAddress { get; set; }
/// <summary>
/// 端口号信息
/// </summary>
public virtual int Port { get; set; }
/// <summary>
/// 接收反馈的超时时间
/// </summary>
public int ReceiveTimeout { get; set; }
/// <summary>
/// 获取或设置一次接收时的数据长度,默认2KB数据长度,特殊情况的时候需要调整
/// </summary>
public int ReceiveCacheLength { get; set; } = 2048;
/// <summary>
/// 核心的数据交互读取
/// </summary>
/// <param name="value">完整的报文内容</param>
/// <returns>是否成功的结果对象</returns>
public virtual OperateResult<byte[]> ReadFromCoreServer( byte[] value )
{
hybirdLock.Enter( );
try
{
IPEndPoint endPoint = new IPEndPoint( IPAddress.Parse( IpAddress ), Port );
Socket server = new Socket( AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp );
server.SendTo( value, value.Length, SocketFlags.None, endPoint );
IPEndPoint sender = new IPEndPoint( IPAddress.Any, 0 );
EndPoint Remote = (EndPoint)sender;
// 对于不存在的IP地址,加入此行代码后,可以在指定时间内解除阻塞模式限制
server.SetSocketOption( SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, ReceiveTimeout );
byte[] buffer = new byte[ReceiveCacheLength];
int recv = server.ReceiveFrom( buffer, ref Remote );
hybirdLock.Leave( );
return OperateResult.CreateSuccessResult( buffer.Take( recv ).ToArray( ) );
}
catch (Exception ex)
{
hybirdLock.Leave( );
return new OperateResult<byte[]>( ex.Message );
}
}
private SimpleHybirdLock hybirdLock = null; // 数据锁
}
}