-
Notifications
You must be signed in to change notification settings - Fork 0
/
UDPPacket1IO.cs
169 lines (152 loc) · 3.9 KB
/
UDPPacket1IO.cs
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.IO;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using UnityEngine;
/// <summary>
/// UdpPacket provides packetIO over UDP
/// </summary>
public class UDPPacket1IO : MonoBehaviour
{
void Start()
{
//do nothing. init must be called
}
public void init(string hostIP, int remotePort, int localPort)
{
RemoteHostName = hostIP;
RemotePort = remotePort;
LocalPort = localPort;
socketsOpen = false;
}
// public UdpPacket()
// {
// }
~UDPPacket1IO()
{
if (IsOpen())
Close();
}
/// <summary>
/// Open a UDP socket and create a UDP sender.
///
/// The default values with which a UdpPacket is created are:
/// - Address of the board to send to - 192.168.0.200
/// - Remote port to send to - 10000
/// - Local port to listen on - 10000
/// </summary>
/// <returns>True on success, false on failure.</returns>
public bool Open()
{
try
{
Sender = new UdpClient();
Receiver = new UdpClient(localPort);
socketsOpen = true;
return true;
}
catch
{
}
return false;
}
/// <summary>
/// Close the socket currently listening, and destroy the UDP sender device.
/// </summary>
public void Close()
{
if (Sender != null)
Sender.Close();
if (Receiver != null)
Receiver.Close();
socketsOpen = false;
}
/// <summary>
/// Query the open state of the UDP socket.
/// </summary>
/// <returns>True if open, false if closed.</returns>
public bool IsOpen()
{
return socketsOpen;
}
/// <summary>
/// Send a packet of bytes out via UDP.
/// </summary>
/// <param name="packet">The packet of bytes to be sent.</param>
/// <param name="length">The length of the packet of bytes to be sent.</param>
public void SendPacket(byte[] packet, int length)
{
if (!IsOpen())
Open();
if (!IsOpen())
return;
Sender.Send(packet, length, remoteHostName, remotePort);
}
/// <summary>
/// Receive a packet of bytes over UDP.
/// </summary>
/// <param name="buffer">The buffer to be read into.</param>
/// <returns>The number of bytes read, or 0 on failure.</returns>
public int ReceivePacket(byte[] buffer)
{
if (!IsOpen())
Open();
if (!IsOpen())
return 0;
IPEndPoint iep = new IPEndPoint(IPAddress.Any, 0);
byte[] incoming = Receiver.Receive(ref iep);
int count = Math.Min(buffer.Length, incoming.Length);
System.Array.Copy(incoming, buffer, count);
return count;
}
private UdpClient Sender;
private UdpClient Receiver;
private bool socketsOpen;
private string remoteHostName;
private int remotePort;
private int localPort;
/// <summary>
/// The address of the board that you're sending to.
/// </summary>
public string RemoteHostName
{
get
{
return remoteHostName;
}
set
{
remoteHostName = value;
}
}
/// <summary>
/// The remote port that you're sending to.
/// </summary>
public int RemotePort
{
get
{
return remotePort;
}
set
{
remotePort = value;
}
}
/// <summary>
/// The local port you're listening on.
/// </summary>
public int LocalPort
{
get
{
return localPort;
}
set
{
localPort = value;
}
}
}