main
syneffort 2 years ago
parent 044ace6552
commit d4dc58c609
  1. 10
      MySolution/HttpStreamingApp/HttpStreamingApp.csproj
  2. 46
      MySolution/HttpStreamingApp/Program.cs
  3. 18
      MySolution/MySolution.sln
  4. 18
      MySolution/SObject/Coyote.cs
  5. 20
      MySolution/SObject/DogFamily.cs
  6. 9
      MySolution/SObject/SObject.csproj
  7. 18
      MySolution/SObject/Wolf.cs
  8. 39
      MySolution/SObject/Zoo.cs
  9. 2
      MySolution/STcpHelper/Packet/PacketType.cs
  10. 1
      MySolution/STcpHelper/Packet/STcpClientListReqPacket.cs
  11. 13
      MySolution/STcpHelper/Packet/STcpClientListResPacket.cs
  12. 1
      MySolution/STcpHelper/Packet/STcpFileReqPacket.cs
  13. 1
      MySolution/STcpHelper/Packet/STcpFileResPacket.cs
  14. 1
      MySolution/STcpHelper/Packet/STcpPacketHeader.cs
  15. 1
      MySolution/STcpHelper/Packet/STcpTextPacket.cs
  16. 24
      MySolution/STcpHelper/Packet/STcpZooReqPacket.cs
  17. 45
      MySolution/STcpHelper/Packet/STcpZooResPacket.cs
  18. 24
      MySolution/STcpHelper/STcpClient.cs
  19. 8
      MySolution/STcpHelper/STcpHelper.csproj
  20. 22
      MySolution/STcpHelper/STcpServer.cs
  21. 14
      MySolution/STcpHelper/Utility/ISSerializer.cs
  22. 3
      MySolution/STcpHelper/Utility/SBufferHelper.cs
  23. 2
      MySolution/STcpHelper/Utility/SEncoding.cs
  24. 22
      MySolution/STcpHelper/Utility/SJsonSerializerHelper.cs
  25. 8
      MySolution/STcpHelper/Utility/SXmlSerializerHelper.cs
  26. 3
      MySolution/TAPClient/Program.cs
  27. 1
      MySolution/TAPClient/TAPClient.csproj
  28. 1
      MySolution/TAPServer/TAPServer.csproj

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

@ -0,0 +1,46 @@
using System.Net;
namespace HttpStreamingApp
{
internal class Program
{
static void Main(string[] args)
{
Thread serverThread = new Thread(new ThreadStart(StartServer));
serverThread.Start();
Console.WriteLine("HTTP Server start.");
Console.ReadKey();
serverThread.Abort();
}
static void StartServer()
{
using (var listener = new HttpListener())
{
listener.Prefixes.Add("http://127.0.0.1:9879/");
listener.Start();
while (true)
{
var context = listener.GetContext();
var response = context.Response;
response.ContentType = "audio/mpeg";
using (var stream = new FileStream("./audio.mp3", FileMode.Open))
{
byte[] buffer = new byte[64 *1024];
int bytesRead = 0;
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
{
response.OutputStream.Write(buffer, 0, bytesRead);
response.OutputStream.Flush();
}
}
response.Close();
}
}
}
}
}

@ -7,11 +7,15 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp", "ConsoleApp\Co
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SerialCommApp", "SerialComApp\SerialCommApp.csproj", "{21CB97AA-1550-4C56-894D-76E6B75B91DC}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SerialCommApp", "SerialComApp\SerialCommApp.csproj", "{21CB97AA-1550-4C56-894D-76E6B75B91DC}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TAPServer", "TAPServer\TAPServer.csproj", "{4CA4FF89-FA3A-42C0-8A66-D84F9C4F8EBC}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TAPServer", "TAPServer\TAPServer.csproj", "{4CA4FF89-FA3A-42C0-8A66-D84F9C4F8EBC}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "STcpHelper", "STcpHelper\STcpHelper.csproj", "{86B2F7FF-8D76-4C46-91E4-E83F7EED4A68}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "STcpHelper", "STcpHelper\STcpHelper.csproj", "{86B2F7FF-8D76-4C46-91E4-E83F7EED4A68}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TAPClient", "TAPClient\TAPClient.csproj", "{6F0DC5AC-D046-4506-A339-EC50C18C4CDB}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TAPClient", "TAPClient\TAPClient.csproj", "{6F0DC5AC-D046-4506-A339-EC50C18C4CDB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SObject", "SObject\SObject.csproj", "{98E66698-89A2-49DF-A808-279C3FEFFABF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HttpStreamingApp", "HttpStreamingApp\HttpStreamingApp.csproj", "{3D549B90-D7ED-4771-8595-9147BBF221C6}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -39,6 +43,14 @@ Global
{6F0DC5AC-D046-4506-A339-EC50C18C4CDB}.Debug|Any CPU.Build.0 = Debug|Any CPU {6F0DC5AC-D046-4506-A339-EC50C18C4CDB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6F0DC5AC-D046-4506-A339-EC50C18C4CDB}.Release|Any CPU.ActiveCfg = Release|Any CPU {6F0DC5AC-D046-4506-A339-EC50C18C4CDB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6F0DC5AC-D046-4506-A339-EC50C18C4CDB}.Release|Any CPU.Build.0 = Release|Any CPU {6F0DC5AC-D046-4506-A339-EC50C18C4CDB}.Release|Any CPU.Build.0 = Release|Any CPU
{98E66698-89A2-49DF-A808-279C3FEFFABF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{98E66698-89A2-49DF-A808-279C3FEFFABF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{98E66698-89A2-49DF-A808-279C3FEFFABF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{98E66698-89A2-49DF-A808-279C3FEFFABF}.Release|Any CPU.Build.0 = Release|Any CPU
{3D549B90-D7ED-4771-8595-9147BBF221C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3D549B90-D7ED-4771-8595-9147BBF221C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3D549B90-D7ED-4771-8595-9147BBF221C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3D549B90-D7ED-4771-8595-9147BBF221C6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SObject
{
public class Coyote : DogFamily
{
public Coyote(string name, int age)
{
this.Name = name;
this.Age = age;
this.Crying = "Yip";
}
}
}

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SObject
{
public class DogFamily
{
public string Name { get; set; }
public int Age { get; set; }
public string Crying { get; set; }
public string Cry()
{
return this.Crying;
}
}
}

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SObject
{
public class Wolf : DogFamily
{
public Wolf(string name, int age)
{
this.Name = name;
this.Age = age;
this.Crying = "Howl";
}
}
}

@ -0,0 +1,39 @@
namespace SObject
{
public class Zoo
{
private Random _rand;
public List<DogFamily> Dogs { get; set; }
public Zoo()
{
_rand = new Random();
this.Dogs = new List<DogFamily>()
{
GetDog(),
GetDog(),
GetDog(),
GetDog(),
GetDog(),
GetDog(),
GetDog(),
GetDog(),
GetDog(),
GetDog(),
};
}
public DogFamily GetDog()
{
DogFamily dog;
int seed = _rand.Next(0, 20);
if (seed % 2 == 0)
dog = new Wolf($"W{seed % 7}", seed);
else
dog = new Coyote($"C{seed % 7}", seed);
return dog;
}
}
}

@ -13,5 +13,7 @@ namespace STcpHelper.Packet
RES_CLIENT_LIST, RES_CLIENT_LIST,
REQ_FILE, REQ_FILE,
RES_FILE, RES_FILE,
REQ_ZOO,
RES_ZOO,
} }
} }

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using STcpHelper.Utility;
namespace STcpHelper.Packet namespace STcpHelper.Packet
{ {

@ -1,4 +1,4 @@
using STcpHelper.Packet; using STcpHelper.Utility;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -12,6 +12,8 @@ namespace STcpHelper.Packet
{ {
public class STcpClientListResPacket : ISTcpPacket public class STcpClientListResPacket : ISTcpPacket
{ {
private ISSerializer _serializer = new SJsonSerializerHelper();
public List<string> Clients { get; private set; } public List<string> Clients { get; private set; }
public STcpClientListResPacket(List<TcpClient> clinets) public STcpClientListResPacket(List<TcpClient> clinets)
@ -29,16 +31,15 @@ namespace STcpHelper.Packet
int length = SBufferHelper.ConvertBufferToDataLength(dataBuffer, cursor); int length = SBufferHelper.ConvertBufferToDataLength(dataBuffer, cursor);
cursor += sizeof(int); cursor += sizeof(int);
string jsonString = SEncoding.GetString(dataBuffer, cursor, length); string serialized = SEncoding.GetString(dataBuffer, cursor, length);
List<string> clients = JsonSerializer.Deserialize<List<string>>(jsonString);
this.Clients = clients; this.Clients = _serializer.Deserialize<List<string>>(serialized);
} }
public byte[] Serialize() public byte[] Serialize()
{ {
string jsonString = JsonSerializer.Serialize(this.Clients); string serialized = _serializer.Serialize(this.Clients);
byte[] data = SEncoding.GetBytes(jsonString); byte[] data = SEncoding.GetBytes(serialized);
byte[] dataLength = SBufferHelper.ConvertDataLengthToBuffer(data.Length); byte[] dataLength = SBufferHelper.ConvertDataLengthToBuffer(data.Length);
int dataSize = STcpPacketHeader.GetDataSize(data, dataLength); int dataSize = STcpPacketHeader.GetDataSize(data, dataLength);

@ -5,6 +5,7 @@ using System.Net;
using System.Text; using System.Text;
using System.Text.Json; using System.Text.Json;
using System.Threading.Tasks; using System.Threading.Tasks;
using STcpHelper.Utility;
namespace STcpHelper.Packet namespace STcpHelper.Packet
{ {

@ -4,6 +4,7 @@ using System.Linq;
using System.Net; using System.Net;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using STcpHelper.Utility;
namespace STcpHelper.Packet namespace STcpHelper.Packet
{ {

@ -4,6 +4,7 @@ using System.Linq;
using System.Net; using System.Net;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using STcpHelper.Utility;
namespace STcpHelper.Packet namespace STcpHelper.Packet
{ {

@ -4,6 +4,7 @@ using System.Linq;
using System.Net; using System.Net;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using STcpHelper.Utility;
namespace STcpHelper.Packet namespace STcpHelper.Packet
{ {

@ -0,0 +1,24 @@
using STcpHelper.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace STcpHelper.Packet
{
public class STcpZooReqPacket : ISTcpPacket
{
public STcpZooReqPacket()
{
}
public byte[] Serialize()
{
STcpPacketHeader header = new STcpPacketHeader(PacketType.REQ_ZOO, 0);
return SBufferHelper.GetBuffer(STcpPacketHeader.HEADER_LENGTH, header.Serialize());
}
}
}

@ -0,0 +1,45 @@
using SObject;
using STcpHelper.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace STcpHelper.Packet
{
public class STcpZooResPacket : ISTcpPacket
{
private ISSerializer _serializer = new SJsonSerializerHelper();
public Zoo Zoo { get; private set; }
public STcpZooResPacket(Zoo zoo)
{
this.Zoo = zoo;
}
public STcpZooResPacket(byte[] dataBuffer)
{
int cursor = 0;
int length = SBufferHelper.ConvertBufferToDataLength(dataBuffer, cursor);
cursor += sizeof(int);
string serialized = SEncoding.GetString(dataBuffer, cursor, length);
this.Zoo = _serializer.Deserialize<Zoo>(serialized);
}
public byte[] Serialize()
{
string serialized = _serializer.Serialize(this.Zoo);
byte[] data = SEncoding.GetBytes(serialized);
byte[] dataLength = SBufferHelper.ConvertDataLengthToBuffer(data.Length);
int dataSize = STcpPacketHeader.GetDataSize(data, dataLength);
STcpPacketHeader header = new STcpPacketHeader(PacketType.RES_ZOO, dataSize);
return SBufferHelper.GetBuffer(STcpPacketHeader.HEADER_LENGTH + dataSize, header.Serialize(), dataLength, data);
}
}
}

@ -1,4 +1,5 @@
using STcpHelper.Packet; using SObject;
using STcpHelper.Packet;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
@ -80,6 +81,14 @@ namespace STcpHelper
STcpFileResPacket packet = new STcpFileResPacket(dataBuffer, FILE_SAVE_FOLDER); STcpFileResPacket packet = new STcpFileResPacket(dataBuffer, FILE_SAVE_FOLDER);
WriteMessage($"File({packet.FileName}) saved."); WriteMessage($"File({packet.FileName}) saved.");
} }
else if (header.Type == PacketType.RES_ZOO)
{
STcpZooResPacket packet = new STcpZooResPacket(dataBuffer);
foreach (var dog in packet.Zoo.Dogs)
{
WriteMessage($"Name:{dog.Name} \tAge:{dog.Age} \tCrying:{dog.Cry()}");
}
}
} }
}, _cts.Token); }, _cts.Token);
} }
@ -134,6 +143,19 @@ namespace STcpHelper
} }
} }
public async void RequireZoo()
{
try
{
STcpZooReqPacket packet = new STcpZooReqPacket();
await _stream.WriteAsync(packet.Serialize());
}
catch (Exception ex)
{
WriteError(ex);
}
}
public void Close() public void Close()
{ {
try try

@ -6,4 +6,12 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SObject\SObject.csproj" />
</ItemGroup>
</Project> </Project>

@ -1,4 +1,5 @@
using STcpHelper.Packet; using SObject;
using STcpHelper.Packet;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Text; using System.Text;
@ -96,6 +97,25 @@ namespace STcpHelper
// Send async // Send async
SendAsyncPacket(packet, client); SendAsyncPacket(packet, client);
} }
else if (header.Type == PacketType.REQ_ZOO)
{
//Zoo zoo = new Zoo();
//Random rand = new Random();
//zoo.Dogs = new List<DogFamily>()
//{
// new Wolf($"Wooolf-{rand.Next(1, 10)}", rand.Next(1, 10)),
// new Coyote($"Coyote-{rand.Next(1, 10)}", rand.Next(1, 10)),
// new Wolf($"Wooolf-{rand.Next(1, 10)}", rand.Next(1, 10)),
// new Coyote($"Coyote-{rand.Next(1, 10)}", rand.Next(1, 10)),
// new Coyote($"Coyote-{rand.Next(1, 10)}", rand.Next(1, 10)),
// new Wolf($"Wooolf-{rand.Next(1, 10)}", rand.Next(1, 10)),
// new Wolf($"Wooolf-{rand.Next(1, 10)}", rand.Next(1, 10)),
//};
STcpZooResPacket packet = new STcpZooResPacket(new Zoo());
SendPacket(packet, client);
}
if (!IsClientConnected(client)) if (!IsClientConnected(client))
break; break;

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace STcpHelper.Utility
{
internal interface ISSerializer
{
string Serialize<T>(T obj);
T Deserialize<T>(string str);
}
}

@ -4,8 +4,9 @@ using System.Linq;
using System.Net; using System.Net;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using STcpHelper.Packet;
namespace STcpHelper.Packet namespace STcpHelper.Utility
{ {
internal class SBufferHelper internal class SBufferHelper
{ {

@ -4,7 +4,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace STcpHelper.Packet namespace STcpHelper.Utility
{ {
internal class SEncoding internal class SEncoding
{ {

@ -0,0 +1,22 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace STcpHelper.Utility
{
public class SJsonSerializerHelper : ISSerializer
{
public string Serialize<T>(T obj)
{
return JsonConvert.SerializeObject(obj);
}
public T Deserialize<T>(string str)
{
return JsonConvert.DeserializeObject<T>(str);
}
}
}

@ -5,11 +5,11 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Xml.Serialization; using System.Xml.Serialization;
namespace STcpHelper.Packet namespace STcpHelper.Utility
{ {
static class SXmlSerializerHelper public class SXmlSerializerHelper : ISSerializer
{ {
public static string Serialize<T>(T obj) public string Serialize<T>(T obj)
{ {
XmlSerializer serializer = new XmlSerializer(typeof(T)); XmlSerializer serializer = new XmlSerializer(typeof(T));
using (StringWriter sw = new StringWriter()) using (StringWriter sw = new StringWriter())
@ -19,7 +19,7 @@ namespace STcpHelper.Packet
} }
} }
public static T Deserialize<T>(string str) public T Deserialize<T>(string str)
{ {
XmlSerializer serializer = new XmlSerializer(typeof(T)); XmlSerializer serializer = new XmlSerializer(typeof(T));
using (StringReader sr = new StringReader(str)) using (StringReader sr = new StringReader(str))

@ -26,6 +26,9 @@ namespace TAPClient
string path = Console.ReadLine(); string path = Console.ReadLine();
client.RequireFile(path); client.RequireFile(path);
break; break;
case "zoo":
client.RequireZoo();
break;
default: default:
client.SendMessage(input); client.SendMessage(input);
break; break;

@ -8,6 +8,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\SObject\SObject.csproj" />
<ProjectReference Include="..\STcpHelper\STcpHelper.csproj" /> <ProjectReference Include="..\STcpHelper\STcpHelper.csproj" />
</ItemGroup> </ItemGroup>

@ -8,6 +8,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\SObject\SObject.csproj" />
<ProjectReference Include="..\STcpHelper\STcpHelper.csproj" /> <ProjectReference Include="..\STcpHelper\STcpHelper.csproj" />
</ItemGroup> </ItemGroup>

Loading…
Cancel
Save