forked from RevenantX/LiteNetLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetSocket.cs
More file actions
159 lines (145 loc) · 5.29 KB
/
NetSocket.cs
File metadata and controls
159 lines (145 loc) · 5.29 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
#if WINRT && !UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.IO;
using Windows.Networking;
using Windows.Networking.Sockets;
namespace LiteNetLib
{
internal sealed class NetSocket
{
private DatagramSocket _datagramSocket;
private readonly Dictionary<NetEndPoint, Stream> _peers = new Dictionary<NetEndPoint, Stream>();
private readonly NetBase.OnMessageReceived _onMessageReceived;
private readonly byte[] _buffer = new byte[NetConstants.PacketSizeLimit];
private NetEndPoint _bufferEndPoint;
private NetEndPoint _localEndPoint;
private static readonly HostName MulticastAddressV4 = new HostName(NetConstants.MulticastGroupIPv4);
private static readonly HostName MulticastAddressV6 = new HostName(NetConstants.MulticastGroupIPv6);
public NetEndPoint LocalEndPoint
{
get { return _localEndPoint; }
}
public NetSocket(NetBase.OnMessageReceived onMessageReceived)
{
_onMessageReceived = onMessageReceived;
}
private void OnMessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
{
var stream = args.GetDataStream().AsStreamForRead();
int count = stream.Read(_buffer, 0, _buffer.Length);
if (count > 0)
{
if (_bufferEndPoint == null ||
!_bufferEndPoint.HostName.IsEqual(args.RemoteAddress) ||
!_bufferEndPoint.PortStr.Equals(args.RemotePort))
{
_bufferEndPoint = new NetEndPoint(args.RemoteAddress, args.RemotePort);
}
_onMessageReceived(_buffer, count, 0, _bufferEndPoint);
}
}
public bool Bind(int port)
{
_datagramSocket = new DatagramSocket();
_datagramSocket.Control.InboundBufferSizeInBytes = NetConstants.SocketBufferSize;
_datagramSocket.Control.DontFragment = true;
_datagramSocket.Control.OutboundUnicastHopLimit = NetConstants.SocketTTL;
_datagramSocket.MessageReceived += OnMessageReceived;
try
{
_datagramSocket.BindServiceNameAsync(port.ToString()).AsTask().Wait();
_localEndPoint = new NetEndPoint(_datagramSocket.Information.LocalAddress, _datagramSocket.Information.LocalPort);
}
catch (Exception ex)
{
NetUtils.DebugWriteError("[B]Bind exception: {0}", ex.ToString());
return false;
}
return true;
}
public bool JoinMulticastGroup(string address)
{
try
{
_datagramSocket.JoinMulticastGroup(new HostName(address));
}
catch (Exception)
{
return false;
}
return true;
}
public bool SendMulticast(byte[] data, int offset, int size, int port)
{
var portString = port.ToString();
try
{
var outputStream =
_datagramSocket.GetOutputStreamAsync(MulticastAddressV4, portString)
.AsTask()
.Result;
var writer = outputStream.AsStreamForWrite();
writer.Write(data, offset, size);
writer.Flush();
outputStream =
_datagramSocket.GetOutputStreamAsync(MulticastAddressV6, portString)
.AsTask()
.Result;
writer = outputStream.AsStreamForWrite();
writer.Write(data, offset, size);
writer.Flush();
}
catch (Exception ex)
{
NetUtils.DebugWriteError("[S][MCAST]" + ex);
return false;
}
return true;
}
public int SendTo(byte[] data, int offset, int length, NetEndPoint remoteEndPoint, ref int errorCode)
{
try
{
Stream writer;
if (!_peers.TryGetValue(remoteEndPoint, out writer))
{
var outputStream =
_datagramSocket.GetOutputStreamAsync(remoteEndPoint.HostName, remoteEndPoint.PortStr)
.AsTask()
.Result;
writer = outputStream.AsStreamForWrite();
_peers.Add(remoteEndPoint, writer);
}
writer.Write(data, offset, length);
writer.Flush();
return length;
}
catch (Exception ex)
{
NetUtils.DebugWriteError("[S]" + ex);
errorCode = -1;
return -1;
}
}
internal void RemovePeer(NetEndPoint ep)
{
_peers.Remove(ep);
}
public void Close()
{
_datagramSocket.Dispose();
_datagramSocket = null;
ClearPeers();
}
internal void ClearPeers()
{
foreach (var dataWriter in _peers)
{
dataWriter.Value.Dispose();
}
_peers.Clear();
}
}
}
#endif