• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Frequently used words (click to add to your profile)

javaandroidc++linuxc#objective-ccocoa誰得qtrubypythonbathyscaphephpgamewindowsguic翻訳omegattwitterframeworktestbtronarduinovb.net計画中(planning stage)directxpreviewerゲームエンジンdom

ソケットを使ってクライアントサーバプログラムを作成するための C# ライブラリ


Commit MetaInfo

Révisiondf69d3815421a23092bb97cc514e2b26590f2542 (tree)
l'heure2015-08-22 22:05:35
Auteurtsntsumi <tsntsumi@tsnt...>
Commitertsntsumi

Message de Log

UdpServer クラスとその導出クラスを追加

UdpServer とその導出クラスは、UDPで接続と受信データを待ち受けるサーバ。

Change Summary

Modification

--- /dev/null
+++ b/src/SocketNet/SocketNet/BroadcastUdpServer.cs
@@ -0,0 +1,51 @@
1+//
2+// BroadcastUdpServer.cs
3+//
4+// Author:
5+// tsntsumi <tsntsumi at tsntsumi.com>
6+//
7+// Copyright (c) 2015 tsntsumi
8+//
9+// This program is free software: you can redistribute it and/or modify
10+// it under the terms of the GNU Lesser General Public License as published by
11+// the Free Software Foundation, either version 3 of the License, or
12+// (at your option) any later version.
13+//
14+// This program is distributed in the hope that it will be useful,
15+// but WITHOUT ANY WARRANTY; without even the implied warranty of
16+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+// GNU Lesser General Public License for more details.
18+//
19+// You should have received a copy of the GNU Lesser General Public License
20+// along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
22+/// @file
23+/// <summary>
24+/// Broadcast UDP server.
25+/// </summary>
26+/// @since 2015.8.20
27+using System;
28+using System.Net;
29+using System.Net.Sockets;
30+
31+namespace SocketNet
32+{
33+ /// <summary>
34+ /// Broadcast UDP server.
35+ /// </summary>
36+ public class BroadcastUdpServer: UdpServer
37+ {
38+ /// <summary>
39+ /// コンストラクタ。
40+ /// </summary>
41+ /// <param name="port">バインドするポート番号。</param>
42+ public BroadcastUdpServer(int port)
43+ {
44+ IPAddress = IPAddress.Any;
45+ Port = port;
46+ TransmissionType = TransmissionType.Broadcast;
47+ asyncCallback = new AsyncCallback(EndReceive);
48+ }
49+ }
50+}
51+
--- /dev/null
+++ b/src/SocketNet/SocketNet/LocalBroadcastUdpServer.cs
@@ -0,0 +1,51 @@
1+//
2+// LocalBroadcastUdpServer.cs
3+//
4+// Author:
5+// tsntsumi <tsntsumi at tsntsumi.com>
6+//
7+// Copyright (c) 2015 tsntsumi
8+//
9+// This program is free software: you can redistribute it and/or modify
10+// it under the terms of the GNU Lesser General Public License as published by
11+// the Free Software Foundation, either version 3 of the License, or
12+// (at your option) any later version.
13+//
14+// This program is distributed in the hope that it will be useful,
15+// but WITHOUT ANY WARRANTY; without even the implied warranty of
16+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+// GNU Lesser General Public License for more details.
18+//
19+// You should have received a copy of the GNU Lesser General Public License
20+// along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
22+/// @file
23+/// <summary>
24+/// Local broadcast UDP server.
25+/// </summary>
26+/// @since 2015.8.20
27+using System;
28+using System.Net;
29+using System.Net.Sockets;
30+
31+namespace SocketNet
32+{
33+ /// <summary>
34+ /// Local broadcast UDP server.
35+ /// </summary>
36+ public class LocalBroadcastUdpServer: UdpServer
37+ {
38+ /// <summary>
39+ /// コンストラクタ。
40+ /// </summary>
41+ /// <param name="port">バインドするポート番号。</param>
42+ public LocalBroadcastUdpServer (int port)
43+ {
44+ IPAddress = IPAddress.Any;
45+ Port = port;
46+ TransmissionType = TransmissionType.LocalBroadcast;
47+ asyncCallback = new AsyncCallback(EndReceive);
48+ }
49+ }
50+}
51+
--- /dev/null
+++ b/src/SocketNet/SocketNet/MulticastUdpServer.cs
@@ -0,0 +1,85 @@
1+//
2+// MulticastUdpServer.cs
3+//
4+// Author:
5+// tsntsumi <tsntsumi at tsntsumi.com>
6+//
7+// Copyright (c) 2015 tsntsumi
8+//
9+// This program is free software: you can redistribute it and/or modify
10+// it under the terms of the GNU Lesser General Public License as published by
11+// the Free Software Foundation, either version 3 of the License, or
12+// (at your option) any later version.
13+//
14+// This program is distributed in the hope that it will be useful,
15+// but WITHOUT ANY WARRANTY; without even the implied warranty of
16+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+// GNU Lesser General Public License for more details.
18+//
19+// You should have received a copy of the GNU Lesser General Public License
20+// along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
22+/// @file
23+/// <summary>
24+/// Multicast UDP server.
25+/// </summary>
26+/// @since 2015.8.20
27+using System;
28+using System.Net;
29+using System.Net.Sockets;
30+
31+namespace SocketNet
32+{
33+ /// <summary>
34+ /// Multicast UDP server.
35+ /// </summary>
36+ public class MulticastUdpServer: UdpServer
37+ {
38+ /// <summary>
39+ /// コンストラクタ。
40+ /// </summary>
41+ /// <param name="ipAddress">ジョインするマルチキャストIPアドレス。</param>
42+ /// <param name="port">バインドするポート番号。</param>
43+ public MulticastUdpServer (IPAddress ipAddress, int port)
44+ {
45+ IPAddress = ipAddress;
46+ Port = port;
47+ TransmissionType = TransmissionType.Multicast;
48+ asyncCallback = new AsyncCallback(EndReceive);
49+ }
50+
51+ /// <summary>
52+ /// Udpサーバを開始してデータ受信を開始します。
53+ /// </summary>
54+ public override void Start()
55+ {
56+ IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, Port);
57+
58+ udpClient = new UdpClient();
59+ udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
60+ udpClient.Client.Bind(ipEndPoint);
61+ udpClient.JoinMulticastGroup(IPAddress);
62+
63+ UdpState udpState = new UdpState(udpClient, ipEndPoint);
64+
65+ acceptingConnections = true;
66+ udpClient.BeginReceive(asyncCallback, udpState);
67+ }
68+
69+ /// <summary>
70+ /// Udpサーバを停止します。
71+ /// </summary>
72+ public override void Stop()
73+ {
74+ acceptingConnections = false;
75+
76+ if (udpClient != null)
77+ {
78+ udpClient.DropMulticastGroup(IPAddress);
79+
80+ udpClient.Close();
81+ }
82+ }
83+ }
84+}
85+
--- a/src/SocketNet/SocketNet/SocketNet.csproj
+++ b/src/SocketNet/SocketNet/SocketNet.csproj
@@ -38,6 +38,13 @@
3838 <Compile Include="TcpDataReceivedEventArgs.cs" />
3939 <Compile Include="TcpServer.cs" />
4040 <Compile Include="Packet.cs" />
41+ <Compile Include="UdpServer.cs" />
42+ <Compile Include="TransmissionType.cs" />
43+ <Compile Include="UdpDataReceivedEventArgs.cs" />
44+ <Compile Include="UnicastUdpServer.cs" />
45+ <Compile Include="MulticastUdpServer.cs" />
46+ <Compile Include="BroadcastUdpServer.cs" />
47+ <Compile Include="LocalBroadcastUdpServer.cs" />
4148 </ItemGroup>
4249 <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
4350 </Project>
\ No newline at end of file
--- /dev/null
+++ b/src/SocketNet/SocketNet/TransmissionType.cs
@@ -0,0 +1,56 @@
1+//
2+// TransmissionType.cs
3+//
4+// Author:
5+// tsntsumi <tsntsumi at tsntsumi.com>
6+//
7+// Copyright (c) 2015 tsntsumi
8+//
9+// This program is free software: you can redistribute it and/or modify
10+// it under the terms of the GNU Lesser General Public License as published by
11+// the Free Software Foundation, either version 3 of the License, or
12+// (at your option) any later version.
13+//
14+// This program is distributed in the hope that it will be useful,
15+// but WITHOUT ANY WARRANTY; without even the implied warranty of
16+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+// GNU Lesser General Public License for more details.
18+//
19+// You should have received a copy of the GNU Lesser General Public License
20+// along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
22+/// @file
23+/// <summary>
24+/// 転送タイプを実装します。
25+/// </summary>
26+/// @since 2015.8.15
27+using System;
28+
29+namespace SocketNet
30+{
31+ /// <summary>
32+ /// 転送のタイプ。
33+ /// </summary>
34+ public enum TransmissionType
35+ {
36+ /// <summary>
37+ /// 登録されたクライアントだけに転送します。ハートビートイクスチェンジを含みます。
38+ /// </summary>
39+ Unicast,
40+
41+ /// <summary>
42+ /// Udp マルチキャストにより転送します。ハートビートイクスチェンジを含みません。
43+ /// </summary>
44+ Multicast,
45+
46+ /// <summary>
47+ /// Udp ブロードキャストにより転送します。ハートビートイクスチェンジを含みません。
48+ /// </summary>
49+ Broadcast,
50+
51+ /// <summary>
52+ /// ローカルな登録されていないユニキャストもしくはハートビートイクスチェンジです。
53+ /// </summary>
54+ LocalBroadcast
55+ }
56+}
--- /dev/null
+++ b/src/SocketNet/SocketNet/UdpDataReceivedEventArgs.cs
@@ -0,0 +1,59 @@
1+//
2+// UdpDataReceivedEventArgs.cs
3+//
4+// Author:
5+// tsntsumi <tsntsumi at tsutsumi.com>
6+//
7+// Copyright (c) 2015 tsntsumi
8+//
9+// This program is free software: you can redistribute it and/or modify
10+// it under the terms of the GNU Lesser General Public License as published by
11+// the Free Software Foundation, either version 3 of the License, or
12+// (at your option) any later version.
13+//
14+// This program is distributed in the hope that it will be useful,
15+// but WITHOUT ANY WARRANTY; without even the implied warranty of
16+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+// GNU Lesser General Public License for more details.
18+//
19+// You should have received a copy of the GNU Lesser General Public License
20+// along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
22+/// @file
23+/// <summary>
24+/// Udp データ受信イベント引数クラスを実装します。
25+/// </summary>
26+/// @since 2015.8.15
27+using System;
28+using System.Net;
29+
30+namespace SocketNet
31+{
32+ /// <summary>
33+ /// データ受信イベントのデータ。
34+ /// </summary>
35+ public class UdpDataReceivedEventArgs : EventArgs
36+ {
37+ /// <summary>
38+ /// 関連付けられたソースエンドポイントを取得します。
39+ /// </summary>
40+ public IPEndPoint SourceEndPoint { get; private set; }
41+
42+ /// <summary>
43+ /// 関連付けられたデータを取得します。
44+ /// </summary>
45+ public byte[] Data { get; private set; }
46+
47+ /// <summary>
48+ /// コンストラクタ。
49+ /// </summary>
50+ /// <param name="sourceEndPoint">関連付けられたソースエンドポイント。</param>
51+ /// <param name="data">関連付けられたデータ。</param>
52+ public UdpDataReceivedEventArgs(IPEndPoint sourceEndPoint, byte[] data)
53+ {
54+ SourceEndPoint = sourceEndPoint;
55+ Data = data;
56+ }
57+ }
58+}
59+
--- /dev/null
+++ b/src/SocketNet/SocketNet/UdpServer.cs
@@ -0,0 +1,216 @@
1+//
2+// UdpServer.cs
3+//
4+// Author:
5+// tsntsumi <tsntsumi at tsntsumi.com>
6+//
7+// Copyright (c) 2015 tsntsumi
8+//
9+// This program is free software: you can redistribute it and/or modify
10+// it under the terms of the GNU Lesser General Public License as published by
11+// the Free Software Foundation, either version 3 of the License, or
12+// (at your option) any later version.
13+//
14+// This program is distributed in the hope that it will be useful,
15+// but WITHOUT ANY WARRANTY; without even the implied warranty of
16+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+// GNU Lesser General Public License for more details.
18+//
19+// You should have received a copy of the GNU Lesser General Public License
20+// along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
22+/// @file
23+/// <summary>
24+/// UDP サーバを実装します。
25+/// </summary>
26+/// @since 2015.8.15
27+using System;
28+using System.Collections.Generic;
29+using System.Net;
30+using System.Net.Sockets;
31+
32+namespace SocketNet
33+{
34+ /// <summary>
35+ /// UDP サーバ。
36+ /// </summary>
37+ public abstract class UdpServer
38+ {
39+ protected UdpClient udpClient;
40+ protected AsyncCallback asyncCallback;
41+
42+ protected volatile bool acceptingConnections;
43+
44+ /// <summary>
45+ /// 非同期メソッドの BeginReceive/EndReceive 呼び出しで渡される
46+ /// オブジェクトをインスタンシエイトするためのクラスです。
47+ /// </summary>
48+ protected class UdpState
49+ {
50+ /// <summary>
51+ /// 関連付けられたクライアントを取得します。
52+ /// </summary>
53+ public UdpClient Client { get; private set; }
54+
55+ /// <summary>
56+ /// 関連付けられた IP エンドポイントを取得します。
57+ /// </summary>
58+ public IPEndPoint IPEndPoint { get; private set; }
59+
60+ /// <summary>
61+ /// コンストラクタ。
62+ /// </summary>
63+ /// <param name="client">関連付けられたクライアント。</param>
64+ /// <param name="ipEndPoint">関連付けられたIPエンドポイント。</param>
65+ public UdpState(UdpClient client, IPEndPoint ipEndPoint)
66+ {
67+ Client = client;
68+ IPEndPoint = ipEndPoint;
69+ }
70+ }
71+
72+ /// <summary>
73+ /// データを受信したときに発行されます。
74+ /// </summary>
75+ public event EventHandler<UdpDataReceivedEventArgs> DataReceived;
76+
77+ /// <summary>
78+ /// Udp サーバがバインドされる IP アドレスを取得します。
79+ /// </summary>
80+ public IPAddress IPAddress { get; protected set; }
81+
82+ /// <summary>
83+ /// Udp サーバがバインドされるマルチキャストアドレスを取得します。
84+ /// </summary>
85+ public IPAddress MulticastAddress { get; protected set; }
86+
87+ /// <summary>
88+ /// Udp サーバがバインドされるポート番号を取得します。
89+ /// </summary>
90+ public int Port { get; protected set; }
91+
92+ /// <summary>
93+ /// サーバの状態を取得します。
94+ /// </summary>
95+ public bool IsRunning
96+ {
97+ get
98+ {
99+ return acceptingConnections;
100+ }
101+ }
102+
103+ /// <summary>
104+ /// 関連付けられた転送タイプを取得します。
105+ /// </summary>
106+ public TransmissionType TransmissionType { get; protected set; }
107+
108+ /// <summary>
109+ /// ブロードキャストUDPサーバを作成します。
110+ /// </summary>
111+ /// <returns>ブロードキャストサーバ。</returns>
112+ /// <param name="port">ポート番号。</param>
113+ /// <param name="local">ローカルブロードキャストサーバを作成するなら <c>true</c>。</param>
114+ public static UdpServer CreateServer(int port, bool local = false)
115+ {
116+ if (local)
117+ {
118+ return new LocalBroadcastUdpServer(port);
119+ }
120+ return new BroadcastUdpServer(port);
121+ }
122+
123+ /// <summary>
124+ /// ユニキャストUDPサーバを作成します。
125+ /// </summary>
126+ /// <returns>ユニキャストUDPサーバ。</returns>
127+ /// <param name="unicastAddress">ユニキャストアドレス。</param>
128+ /// <param name="port">ポート番号。</param>
129+ public static UdpServer CreateServer(IPAddress unicastAddress, int port)
130+ {
131+ return new UnicastUdpServer(unicastAddress, port);
132+ }
133+
134+ /// <summary>
135+ /// マルチキャストUDPサーバを作成します。
136+ /// </summary>
137+ /// <returns>マルチキャストUDPサーバ。</returns>
138+ /// <param name="port">ポート番号。</param>
139+ /// <param name="multicastAddress">マルチキャストアドレス。</param>
140+ public static UdpServer CreateServer(int port, IPAddress multicastAddress)
141+ {
142+ return new MulticastUdpServer(multicastAddress, port);
143+ }
144+
145+ /// <summary>
146+ /// Udpサーバを開始してデータ受信を開始します。
147+ /// </summary>
148+ public virtual void Start()
149+ {
150+ IPEndPoint ipEndPoint = new IPEndPoint(IPAddress, Port);
151+
152+ udpClient = new UdpClient();
153+ udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
154+ udpClient.Client.Bind(ipEndPoint);
155+
156+ UdpState udpState = new UdpState(udpClient, ipEndPoint);
157+
158+ acceptingConnections = true;
159+ udpClient.BeginReceive(asyncCallback, udpState);
160+ }
161+
162+ /// <summary>
163+ /// Udpサーバを停止します。
164+ /// </summary>
165+ public virtual void Stop()
166+ {
167+ acceptingConnections = false;
168+
169+ if (udpClient != null)
170+ {
171+ udpClient.Close();
172+ }
173+ }
174+
175+ /// <summary>
176+ /// UdpClient.EndReceive を呼び出します。
177+ /// </summary>
178+ /// <param name="asyncResult">BeginReceive 呼び出しから送られる結果オブジェクト。</param>
179+ protected void EndReceive(IAsyncResult asyncResult)
180+ {
181+ try
182+ {
183+ UdpState udpState = (UdpState)asyncResult.AsyncState;
184+ UdpClient udpClient = udpState.Client;
185+ IPEndPoint ipEndPoint = udpState.IPEndPoint;
186+
187+ byte[] data = udpClient.EndReceive(asyncResult, ref ipEndPoint);
188+ if (data != null && data.Length > 0)
189+ {
190+ OnDataReceived(new UdpDataReceivedEventArgs(ipEndPoint, data));
191+ }
192+
193+ if (acceptingConnections)
194+ {
195+ udpClient.BeginReceive(asyncCallback, udpState);
196+ }
197+ }
198+ catch (ObjectDisposedException)
199+ {
200+ // Suppress error
201+ }
202+ }
203+
204+ /// <summary>
205+ /// DetaReceived イベントを発行します。
206+ /// </summary>
207+ /// <param name="e">イベントデータを格納するオブジェクト。</param>
208+ protected void OnDataReceived(UdpDataReceivedEventArgs e)
209+ {
210+ if (DataReceived != null)
211+ {
212+ DataReceived(this, e);
213+ }
214+ }
215+ }
216+}
--- /dev/null
+++ b/src/SocketNet/SocketNet/UnicastUdpServer.cs
@@ -0,0 +1,52 @@
1+//
2+// UnicastUdpServer.cs
3+//
4+// Author:
5+// tsntsumi <tsntsumi at tsntsumi.com>
6+//
7+// Copyright (c) 2015 tsntsumi
8+//
9+// This program is free software: you can redistribute it and/or modify
10+// it under the terms of the GNU Lesser General Public License as published by
11+// the Free Software Foundation, either version 3 of the License, or
12+// (at your option) any later version.
13+//
14+// This program is distributed in the hope that it will be useful,
15+// but WITHOUT ANY WARRANTY; without even the implied warranty of
16+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+// GNU Lesser General Public License for more details.
18+//
19+// You should have received a copy of the GNU Lesser General Public License
20+// along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
22+/// @file
23+/// <summary>
24+/// Unicast UDP server.
25+/// </summary>
26+/// @since 2015.8.20
27+using System;
28+using System.Net;
29+using System.Net.Sockets;
30+
31+namespace SocketNet
32+{
33+ /// <summary>
34+ /// Unicast UDP server.
35+ /// </summary>
36+ public class UnicastUdpServer: UdpServer
37+ {
38+ /// <summary>
39+ /// コンストラクタ。
40+ /// </summary>
41+ /// <param name="ipAddress">バインドするIPアドレス。</param>
42+ /// <param name="port">バインドするポート番号。</param>
43+ public UnicastUdpServer (IPAddress ipAddress, int port)
44+ {
45+ IPAddress = ipAddress;
46+ Port = port;
47+ TransmissionType = TransmissionType.Unicast;
48+ asyncCallback = new AsyncCallback(EndReceive);
49+ }
50+ }
51+}
52+