• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Frequently used words (click to add to your profile)

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

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


Commit MetaInfo

Révision366e1af6b066f7062325819443f3db7dc5a00213 (tree)
l'heure2015-08-30 09:20:58
Auteurtsntsumi <tsntsumi@tsnt...>
Commitertsntsumi

Message de Log

DataReceivedCallback メソッドのステップ数が長いため分割してリファクタリング

DataRecievedCallback メソッドの、受信したデータからパケットを取り出して
DataReceived イベントを発生させるコードを独立させ、 RetrievePackets メソ
ッドとした。

Change Summary

Modification

--- a/src/SocketNet/SocketNet/TcpConnection.cs
+++ b/src/SocketNet/SocketNet/TcpConnection.cs
@@ -78,7 +78,6 @@ namespace SocketNet
7878 private byte[] header;
7979 private byte[] packet;
8080 private int packetPayloadLength;
81- private int bytesToProcessRemaining;
8281
8382 private bool disposed = false;
8483
@@ -151,7 +150,6 @@ namespace SocketNet
151150 header = new byte[PacketSpec.HeaderLength];
152151 packet = null;
153152 packetPayloadLength = 0;
154- bytesToProcessRemaining = 0;
155153 }
156154
157155 /// <summary>
@@ -260,70 +258,8 @@ namespace SocketNet
260258 {
261259 completelyReceivedData.Add(partiallyReceivedData[i]);
262260 }
263- bytesToProcessRemaining = completelyReceivedData.Count;
264-
265- while (bytesToProcessRemaining > 0)
266- {
267- switch (receiveState)
268- {
269- case DataReceiveState.PacketHeader:
270- if (completelyReceivedData.Count >= PacketSpec.HeaderLength)
271- {
272- completelyReceivedData.CopyTo(0, header, 0, PacketSpec.HeaderLength);
273- packetPayloadLength = PacketSpec.ObtainPayloadLength(header);
274-
275- receiveState = DataReceiveState.PacketPayload;
276- completelyReceivedData.RemoveRange(0, PacketSpec.HeaderLength);
277- bytesToProcessRemaining -= PacketSpec.HeaderLength;
278- }
279- else
280- {
281- bytesToProcessRemaining = 0;
282- }
283- break;
284-
285- case DataReceiveState.PacketPayload:
286- if (completelyReceivedData.Count >= packetPayloadLength)
287- {
288- packet = new byte[PacketSpec.HeaderLength + packetPayloadLength + PacketSpec.FooterLength];
289- Array.Copy(header, packet, PacketSpec.HeaderLength);
290- completelyReceivedData.CopyTo(0, packet, PacketSpec.HeaderLength, packetPayloadLength);
291-
292- receiveState = DataReceiveState.PacketFooter;
293- completelyReceivedData.RemoveRange(0, packetPayloadLength);
294- bytesToProcessRemaining -= packetPayloadLength;
295-
296- if (PacketSpec.FooterLength == 0)
297- {
298- OnDataReceived(new TcpDataReceivedEventArgs(this, packet));
299- receiveState = DataReceiveState.PacketHeader;
300- }
301- }
302- else
303- {
304- bytesToProcessRemaining = 0;
305- }
306- break;
307-
308- case DataReceiveState.PacketFooter:
309- if (completelyReceivedData.Count >= PacketSpec.FooterLength)
310- {
311- completelyReceivedData.CopyTo(0, packet, PacketSpec.HeaderLength + packetPayloadLength, PacketSpec.FooterLength);
312-
313- OnDataReceived(new TcpDataReceivedEventArgs(this, packet));
314-
315- receiveState = DataReceiveState.PacketHeader;
316- completelyReceivedData.RemoveRange(0, PacketSpec.FooterLength);
317- bytesToProcessRemaining -= PacketSpec.FooterLength;
318- }
319- else
320- {
321- bytesToProcessRemaining = 0;
322- }
323- break;
324- }
325- }
326261
262+ RetrievePackets();
327263 BeginReceive();
328264 }
329265 else
@@ -341,6 +277,74 @@ namespace SocketNet
341277 }
342278
343279 /// <summary>
280+ /// 受信したデータからパケットを取り出します。
281+ /// </summary>
282+ /// <remarks>>
283+ /// パケットを取り出したら、 <see cref="DataReceived"/> イベントを発行します。
284+ /// </remarks>
285+ private void RetrievePackets()
286+ {
287+ int bytesToProcessRemaining = completelyReceivedData.Count;
288+
289+ while (bytesToProcessRemaining > 0)
290+ {
291+ switch (receiveState)
292+ {
293+ case DataReceiveState.PacketHeader:
294+ if (completelyReceivedData.Count < PacketSpec.HeaderLength)
295+ {
296+ return;
297+ }
298+ completelyReceivedData.CopyTo(0, header, 0, PacketSpec.HeaderLength);
299+ packetPayloadLength = PacketSpec.ObtainPayloadLength(header);
300+
301+ receiveState = DataReceiveState.PacketPayload;
302+ completelyReceivedData.RemoveRange(0, PacketSpec.HeaderLength);
303+ bytesToProcessRemaining -= PacketSpec.HeaderLength;
304+ break;
305+
306+ case DataReceiveState.PacketPayload:
307+ if (completelyReceivedData.Count < packetPayloadLength)
308+ {
309+ return;
310+ }
311+ packet = new byte[PacketSpec.HeaderLength + packetPayloadLength + PacketSpec.FooterLength];
312+ Array.Copy(header, packet, PacketSpec.HeaderLength);
313+ completelyReceivedData.CopyTo(0, packet, PacketSpec.HeaderLength, packetPayloadLength);
314+
315+ receiveState = DataReceiveState.PacketFooter;
316+ completelyReceivedData.RemoveRange(0, packetPayloadLength);
317+ bytesToProcessRemaining -= packetPayloadLength;
318+
319+ // フッタ長が 0 バイトの場合に 1 パケットだけ受信したとすると
320+ // bytesToProcessRemaining も 0 となってしまい、
321+ // while ループを抜けてしまって case DataReceiveState.PacketFooter: に
322+ // 到達しないため、ここでチェックしなければならない。
323+ if (PacketSpec.FooterLength == 0)
324+ {
325+ OnDataReceived(new TcpDataReceivedEventArgs(this, packet));
326+ receiveState = DataReceiveState.PacketHeader;
327+ }
328+ break;
329+
330+ case DataReceiveState.PacketFooter:
331+ if (completelyReceivedData.Count < PacketSpec.FooterLength)
332+ {
333+ return;
334+ }
335+ completelyReceivedData.CopyTo(0, packet, PacketSpec.HeaderLength + packetPayloadLength, PacketSpec.FooterLength);
336+
337+ OnDataReceived(new TcpDataReceivedEventArgs(this, packet));
338+
339+ receiveState = DataReceiveState.PacketHeader;
340+ completelyReceivedData.RemoveRange(0, PacketSpec.FooterLength);
341+ bytesToProcessRemaining -= PacketSpec.FooterLength;
342+ break;
343+ }
344+ }
345+ }
346+
347+ /// <summary>
344348 /// DataReceived イベントを発行します。
345349 /// </summary>
346350 /// <param name="e">イベントのデータを格納する <see cref="TcpDataReceivedEventArgs"/> オブジェクト。</param>