Skip to content

Commit

Permalink
1.2.1: パケットの構造化, サーバの修正
Browse files Browse the repository at this point in the history
  • Loading branch information
amedouhu committed Jul 24, 2023
1 parent 62c2703 commit 12204f1
Show file tree
Hide file tree
Showing 16 changed files with 11,511 additions and 43 deletions.
49 changes: 46 additions & 3 deletions Installer/Installer.vdproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
"SccProvider" = "8:"
"Hierarchy"
{
"Entry"
{
"MsmKey" = "8:_3C590723DFB1B4F2CD7FA45644973999"
"OwnerKey" = "8:_AA3A688182AA4C1F8BF63F4DE0B5BAEE"
"MsmSig" = "8:_UNDEFINED"
}
"Entry"
{
"MsmKey" = "8:_47D9EE2F38888078997C5DFBBF6758D5"
Expand Down Expand Up @@ -46,6 +52,12 @@
"Entry"
{
"MsmKey" = "8:_UNDEFINED"
"OwnerKey" = "8:_3C590723DFB1B4F2CD7FA45644973999"
"MsmSig" = "8:_UNDEFINED"
}
"Entry"
{
"MsmKey" = "8:_UNDEFINED"
"OwnerKey" = "8:_47D9EE2F38888078997C5DFBBF6758D5"
"MsmSig" = "8:_UNDEFINED"
}
Expand Down Expand Up @@ -144,6 +156,37 @@
}
"File"
{
"{9F6F8455-1EF1-4B85-886A-4223BCC8E7F7}:_3C590723DFB1B4F2CD7FA45644973999"
{
"AssemblyRegister" = "3:1"
"AssemblyIsInGAC" = "11:FALSE"
"AssemblyAsmDisplayName" = "8:Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"
"ScatterAssemblies"
{
"_3C590723DFB1B4F2CD7FA45644973999"
{
"Name" = "8:Newtonsoft.Json.dll"
"Attributes" = "3:512"
}
}
"SourcePath" = "8:Newtonsoft.Json.dll"
"TargetName" = "8:"
"Tag" = "8:"
"Folder" = "8:_AD2BDD9538E04F7FBAFD0FE949579B94"
"Condition" = "8:"
"Transitive" = "11:FALSE"
"Vital" = "11:TRUE"
"ReadOnly" = "11:FALSE"
"Hidden" = "11:FALSE"
"System" = "11:FALSE"
"Permanent" = "11:FALSE"
"SharedLegacy" = "11:FALSE"
"PackageAs" = "3:1"
"Register" = "3:1"
"Exclude" = "11:FALSE"
"IsDependency" = "11:TRUE"
"IsolateTo" = "8:"
}
"{9F6F8455-1EF1-4B85-886A-4223BCC8E7F7}:_47D9EE2F38888078997C5DFBBF6758D5"
{
"AssemblyRegister" = "3:1"
Expand Down Expand Up @@ -271,15 +314,15 @@
{
"Name" = "8:Microsoft Visual Studio"
"ProductName" = "8:Unit"
"ProductCode" = "8:{3D84E9AA-4734-4504-8FFB-E6719F863B4C}"
"PackageCode" = "8:{07467D2D-4DCB-4A43-AF32-9FEE1362A870}"
"ProductCode" = "8:{91FE8033-93D8-448C-AC8D-267ADE167AC9}"
"PackageCode" = "8:{DD7E90B5-A254-423E-A12A-34965D3374F5}"
"UpgradeCode" = "8:{491DC567-9600-4CD7-AF1B-710DE09FF8E3}"
"AspNetVersion" = "8:"
"RestartWWWService" = "11:FALSE"
"RemovePreviousVersions" = "11:FALSE"
"DetectNewerInstalledVersion" = "11:TRUE"
"InstallAllUsers" = "11:FALSE"
"ProductVersion" = "8:1.1.0"
"ProductVersion" = "8:1.2.1"
"Manufacturer" = "8:飴豆腐"
"ARPHELPTELEPHONE" = "8:"
"ARPHELPLINK" = "8:"
Expand Down
63 changes: 46 additions & 17 deletions Unit/Forms/.Unit.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Newtonsoft.Json.Linq;
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
Expand Down Expand Up @@ -28,7 +29,7 @@ public Unit()
public void print(string message)
{
/* チャットに出力する */
if (String.IsNullOrWhiteSpace(message))
if (string.IsNullOrWhiteSpace(message))
{
// メッセージが空白なら
return;
Expand All @@ -53,7 +54,7 @@ public void print(string message)
}
}

public void position(String position)
public void position(string position)
{
/* ポジションを設定する */
if (this.InvokeRequired)
Expand All @@ -71,7 +72,7 @@ public void position(String position)
}
}

public void connect(String address, int port)
public void connect(string address, int port)
{
/* サーバーとの接続処理 */
try
Expand All @@ -86,6 +87,7 @@ public void connect(String address, int port)
{
client = new TcpClient(address, port);
}
sendData("{\"protocol\": \"unit.client\", \"packet\": \"connect\"}");
position(address + ":" + port);
print("Connected to the server.");
}catch (Exception e)
Expand All @@ -106,6 +108,7 @@ public void disconnect()
// サーバーとの接続がないなら
return;
}
sendData("{\"protocol\": \"unit.client\", \"packet\": \"disconnect\"}");
client.Close();
client = null;
position("Home");
Expand Down Expand Up @@ -136,7 +139,7 @@ public void recvData()
{
var data = new byte[256];
int size = 0;
string resultMessage = "";
string packet = "";
var stream = client.GetStream();
while (true)
{
Expand All @@ -151,15 +154,41 @@ public void recvData()
// 全てのデータを読み取ったなら
break;
}
resultMessage += System.Text.Encoding.UTF8.GetString(data, 0, size);
packet += System.Text.Encoding.UTF8.GetString(data, 0, size);
System.Threading.Thread.Sleep(1);
}
if (resultMessage == "")
if (packet == "")
{
// データが空白なら
continue;
}
print(resultMessage);
JObject json = JObject.Parse(packet);
if ((string) json["protocol"] != "unit.server")
{
// プロトコルが不正なら
continue;
}
if ((string) json["packet"] == "message")
{
// メッセージパケットなら
print((string)json["message"]);
continue;
}
if ((string)json["packet"] == "end")
{
// エンドパケットなら
print("server closed.");
try
{
client.Close();
client = null;
}catch
{
print("Failed to client close.");
}
position("Home");
continue;
}
}
catch (Exception e)
{
Expand All @@ -184,9 +213,14 @@ public void recvData()
});
}

public void sendData(String message)
public void sendData(string packet)
{
/* データの送信処理 */
if (string.IsNullOrWhiteSpace(packet))
{
// メッセージが空白なら
return;
}
try
{
// データの送信を試みる
Expand All @@ -198,12 +232,7 @@ public void sendData(String message)
position("Home");
return;
}
if (String.IsNullOrWhiteSpace(message))
{
// メッセージが空白なら
return;
}
var data = System.Text.Encoding.UTF8.GetBytes(message);
var data = System.Text.Encoding.UTF8.GetBytes(packet);
var stream = client.GetStream();
stream.Write(data, 0, data.Length);
}
Expand All @@ -217,7 +246,7 @@ public void sendData(String message)
private void bt_send_Click(object sender, EventArgs e)
{
/* bt_sendがクリックされたとき */
if (String.IsNullOrWhiteSpace(tb_message.Text))
if (string.IsNullOrWhiteSpace(tb_message.Text))
{
// メッセージが空白なら
return;
Expand All @@ -230,7 +259,7 @@ private void bt_send_Click(object sender, EventArgs e)
}else
{
// サーバーとの接続があるなら
sendData(tb_message.Text);
sendData("{\"protocol\": \"unit.client\", \"packet\": \"message\", \"message\": \"" + tb_message.Text + "\"}");
}
tb_message.Text = "";
}
Expand Down
66 changes: 44 additions & 22 deletions Unit/Forms/Server.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
Expand Down Expand Up @@ -60,8 +62,6 @@ public void beggin(int port)
{
TcpClient client = listener.AcceptTcpClient();
clients.Add(client);
print(client.Client.RemoteEndPoint + " connected.");
sendData(client.Client.RemoteEndPoint + " connected.");
}
catch
{
Expand All @@ -74,7 +74,7 @@ public void beggin(int port)
public void end()
{
/* サーバーを停止する */
sendData("Server closed.");
sendData("{\"protocol\": \"unit.server\", \"packet\": \"end\"}");
lock (obj)
{
if (listener == null)
Expand Down Expand Up @@ -119,7 +119,7 @@ public void recvData()
{
var data = new byte[256];
int size = 0;
string resultMessage = "";
string packet = "";
var stream = client.GetStream();
while (true)
{
Expand All @@ -134,15 +134,47 @@ public void recvData()
// 読み取りが終わっているなら
break;
}
resultMessage += System.Text.Encoding.UTF8.GetString(data, 0, data.Length);
if (resultMessage == "")
packet += System.Text.Encoding.UTF8.GetString(data, 0, data.Length);
if (packet == "")
{
// データが空白なら
break;
}
resultMessage = DateTime.Now.ToString("HH:mm ") + client.Client.RemoteEndPoint + ": " + resultMessage;
sendData(resultMessage);
print(resultMessage);
JObject json = JObject.Parse(packet);
if ((string)json["protocol"] != "unit.client")
{
// プロトコルが不正なら
continue;
}
if ((string)json["packet"] == "message")
{
// メッセージパケットなら
sendData("{\"protocol\": \"unit.server\", \"packet\": \"message\", \"message\": \"" + client.Client.RemoteEndPoint + ": " + (string)json["message"] + "\"}");
print(client.Client.RemoteEndPoint + ": " + (string) json["message"]);
continue;
}
if ((string)json["packet"] == "connect")
{
// コネクトパケットなら
sendData("{\"protocol\": \"unit.server\", \"packet\": \"message\", \"message\": \"" + client.Client.RemoteEndPoint + " connected.\"}");
print(client.Client.RemoteEndPoint + " connected.");
continue;
}
if ((string)json["packet"] == "disconnect")
{
// ディスコネクトパケットなら
for (int i = clients.Count - 1; i >= 0; i--)
{
if (clients[i].Equals(client))
{
clients.RemoveAt(i);
}
}
sendData("{\"protocol\": \"unit.server\", \"packet\": \"message\", \"message\": \"" + client.Client.RemoteEndPoint + " disconnected.\"}");
print(client.Client.RemoteEndPoint + " disconnected.");
continue;
}

System.Threading.Thread.Sleep(1);
}
}
Expand All @@ -151,23 +183,13 @@ public void recvData()
print("Failed to receive from client.: " + e.Message);
}
}
for (int i = clients.Count - 1; i >= 0; i--)
{
if (! clients[i].Connected)
{
// クライアントが接続されていないなら
TcpClient client = listener.AcceptTcpClient();
print(client + " disconnected.");
clients.RemoveAt(i);
}
}
}
System.Threading.Thread.Sleep(1);
}
});
}

public void sendData(string message)
public void sendData(string packet)
{
/* クライアントにデータを送信する */
lock (obj)
Expand All @@ -181,7 +203,7 @@ public void sendData(string message)
{
try
{
var data = System.Text.Encoding.UTF8.GetBytes(message);
var data = System.Text.Encoding.UTF8.GetBytes(packet);
var stream = client.GetStream();
stream.Write(data, 0, data.Length);
}catch (Exception e)
Expand Down
4 changes: 4 additions & 0 deletions Unit/Unit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
Expand Down Expand Up @@ -113,6 +116,7 @@
<EmbeddedResource Include="Forms\.Unit.resx">
<DependentUpon>.Unit.cs</DependentUpon>
</EmbeddedResource>
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand Down
Binary file added Unit/bin/Debug/Newtonsoft.Json.dll
Binary file not shown.
Loading

0 comments on commit 12204f1

Please sign in to comment.