-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathudpClient.cs
More file actions
183 lines (164 loc) · 6.19 KB
/
udpClient.cs
File metadata and controls
183 lines (164 loc) · 6.19 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
namespace Flow_ClientWin
{
[Serializable]
public class udpPacket
{
public udpMessageType Type { get; set; }
public DateTime TimeStamp { get; set; }
public string Message { get; set; }
public udpPacket(udpMessageType type)
{ this.Type = type; }
public udpPacket(udpMessageType type, string message)
{
this.Type = type;
this.Message = message;
}
}
public enum udpMessageType
{
Announce = 1,
Greet = 2,
Goodbye = 3,
FindLeader = 4,
LeaderReply = 5,
Heartbeat = 6
}
public class udpPeer
{
public IPAddress Address { get; set; }
public DateTime LastHeartBeat { get; set; }
public string ClientID { get; set; }
}
public static class udpClient
{
private static List<udpPeer> peers;
private static List<IPAddress> myIPs;
private static Timer heartbeatTimer;
public static void Start()
{
myIPs = Dns.GetHostAddresses(Dns.GetHostName()).ToList();
peers = new List<udpPeer>();
Listen();
Broadcast(new udpPacket(udpMessageType.Announce, Properties.Settings.Default.ClientID));
RecevedSignal += udpClient_RecevedSignal;
FindLeader(0);
//AutoResetEvent autoEvent = new AutoResetEvent(false);
int timeout = Properties.Settings.Default.Heartbeat * 1000;
heartbeatTimer = new Timer(sendHeartbeat, null, timeout,timeout);
}
private static void sendHeartbeat(object stateInfo)
{
Broadcast(new udpPacket(udpMessageType.Heartbeat, Properties.Settings.Default.ClientID));
int timeout = Properties.Settings.Default.PeerTimeout;
peers.RemoveAll(p => DateTime.UtcNow.Subtract(p.LastHeartBeat).TotalSeconds > timeout);
}
private static IPEndPoint udpLeader;
private static bool isLeader = false;
static void udpClient_RecevedSignal(udpPacket message, IPEndPoint Sender)
{
if (myIPs.Contains(Sender.Address))
{ return; }
switch (message.Type)
{
case udpMessageType.LeaderReply:
udpLeader = Sender;
break;
case udpMessageType.FindLeader:
if (isLeader)
Broadcast(new udpPacket(udpMessageType.LeaderReply, Properties.Settings.Default.ClientID));
break;
case udpMessageType.Announce:
case udpMessageType.Greet:
case udpMessageType.Heartbeat:
udpPeer peer = (from p in peers
where p.Address.Equals(Sender.Address)
where p.ClientID == message.Message
select p).FirstOrDefault();
if (peer == null)
{
udpPeer p = new udpPeer();
p.Address = Sender.Address;
p.ClientID = message.Message;
p.LastHeartBeat = DateTime.UtcNow;
if (message.Type == udpMessageType.Announce)
{ Broadcast(new udpPacket(udpMessageType.Greet, Properties.Settings.Default.ClientID)); }
peers.Add(p);
}
else
{ peer.LastHeartBeat = DateTime.UtcNow; }
Console.Title = "Peer Count: " + peers.Count;
break;
}
}
private static void FindLeader(int attempt)
{
if (attempt < 5)
{
Broadcast(new udpPacket(udpMessageType.FindLeader));
Thread.Sleep(3000);
if (udpLeader == null)
{ FindLeader(++attempt); }
}
else
{
Broadcast(new udpPacket(udpMessageType.LeaderReply));
isLeader = true;
udpLeader = null;
}
}
public delegate void ReceivedSignalDelegate(udpPacket Message, IPEndPoint Sender);
public static event ReceivedSignalDelegate RecevedSignal;
private static readonly UdpClient udp = new UdpClient(Properties.Settings.Default.Port);
public static void Listen()
{ udp.BeginReceive(Receive, new object()); }
public static void Broadcast(udpPacket Message)
{
Message.TimeStamp = DateTime.UtcNow;
byte[] bytes = ObjectToByteArray(Message);
using (UdpClient client = new UdpClient())
{
IPEndPoint ip = new IPEndPoint(IPAddress.Broadcast, Properties.Settings.Default.Port);
client.Send(bytes, bytes.Length, ip);
client.Close();
}
}
private static void Receive(IAsyncResult ar)
{
IPEndPoint ip = new IPEndPoint(IPAddress.Any, 15000);
byte[] bytes = udp.EndReceive(ar, ref ip);
udpPacket message = (udpPacket)ByteArrayToObject(bytes);
Listen();
if (RecevedSignal != null)
{ RecevedSignal(message, ip); }
}
// Convert an object to a byte array
private static byte[] ObjectToByteArray(Object obj)
{
BinaryFormatter bf = new BinaryFormatter();
using (var ms = new MemoryStream())
{
bf.Serialize(ms, obj);
return ms.ToArray();
}
}
private static Object ByteArrayToObject(byte[] arrBytes)
{
using (var memStream = new MemoryStream())
{
var binForm = new BinaryFormatter();
memStream.Write(arrBytes, 0, arrBytes.Length);
memStream.Seek(0, SeekOrigin.Begin);
var obj = binForm.Deserialize(memStream);
return obj;
}
}
}
}