update comm logic

main
syneffort 2 years ago
parent 758c943301
commit cc844168fc
  1. 20
      SocketStudy/Client/Client.csproj
  2. 20
      SocketStudy/ClientForm/ClientForm.csproj
  3. 13
      SocketStudy/ClientForm/MainForm.Designer.cs
  4. 64
      SocketStudy/ClientForm/MainForm.cs
  5. 121
      SocketStudy/PComm/PClient.cs
  6. 24
      SocketStudy/PComm/PComm.csproj
  7. 6
      SocketStudy/PComm/PDataType.cs
  8. 16
      SocketStudy/PObject/PFileData.cs
  9. 19
      SocketStudy/PObject/PObject.csproj
  10. 47
      SocketStudy/PUtil/PFileManager.cs
  11. 113
      SocketStudy/PUtil/PUtil.cs
  12. 23
      SocketStudy/PUtil/PUtility.csproj
  13. 20
      SocketStudy/Server/Server_Single.csproj
  14. 15
      SocketStudy/ServerForm/DataManager.cs
  15. 20
      SocketStudy/ServerForm/ServerForm.csproj
  16. 17
      SocketStudy/ServerForm/mainForm.cs
  17. 20
      SocketStudy/Server_Async/Server_Async.csproj
  18. 20
      SocketStudy/Server_Listener/Server_Client_Listener.csproj
  19. 38
      SocketStudy/SocketStudy.sln

@ -32,6 +32,26 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />

@ -32,6 +32,26 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />

@ -33,6 +33,7 @@ namespace ClientForm
this.stringButton = new System.Windows.Forms.Button();
this.objectButton = new System.Windows.Forms.Button();
this.imageButton = new System.Windows.Forms.Button();
this.fileButton = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// logConsole
@ -77,11 +78,22 @@ namespace ClientForm
this.imageButton.UseVisualStyleBackColor = true;
this.imageButton.Click += new System.EventHandler(this.imageButton_Click);
//
// fileButton
//
this.fileButton.Location = new System.Drawing.Point(379, 39);
this.fileButton.Name = "fileButton";
this.fileButton.Size = new System.Drawing.Size(75, 23);
this.fileButton.TabIndex = 2;
this.fileButton.Text = "file";
this.fileButton.UseVisualStyleBackColor = true;
this.fileButton.Click += new System.EventHandler(this.fileButton_Click);
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(498, 450);
this.Controls.Add(this.fileButton);
this.Controls.Add(this.imageButton);
this.Controls.Add(this.objectButton);
this.Controls.Add(this.stringButton);
@ -100,6 +112,7 @@ namespace ClientForm
private System.Windows.Forms.Button stringButton;
private System.Windows.Forms.Button objectButton;
private System.Windows.Forms.Button imageButton;
private System.Windows.Forms.Button fileButton;
}
}

@ -17,11 +17,13 @@ namespace ClientForm
{
public partial class MainForm : Form
{
private readonly string SERVER_IP = "124.56.13.173";
private PClient heartBeatClient;
private PClient client;
private System.Timers.Timer heartBeatTimer;
private System.Timers.Timer testTimer;
private System.Timers.Timer GarbageTimer;
public MainForm()
{
@ -34,21 +36,21 @@ namespace ClientForm
string clientId = PUtil.GetRandomString(5);
string heartBeatClientId = clientId + "_hb";
heartBeatClient = new PClient("127.0.0.1", 7778, heartBeatClientId);
heartBeatClient = new PClient(SERVER_IP, 37778, heartBeatClientId);
heartBeatClient.OnReceived += HeartBeatClient_OnReceived;
client = new PClient("127.0.0.1", 7777, clientId);
client = new PClient(SERVER_IP, 37777, clientId);
client.OnReceived += Client_OnReceived;
heartBeatTimer = new System.Timers.Timer();
heartBeatTimer.Interval = 5000;
heartBeatTimer.Interval = 1000 * 10;
heartBeatTimer.Elapsed += HeartBeatTimer_Elapsed;
heartBeatTimer.Start();
testTimer = new System.Timers.Timer();
testTimer.Interval = 500;
testTimer.Elapsed += TestTimer_Elapsed;
testTimer.Start();
GarbageTimer = new System.Timers.Timer();
GarbageTimer.Interval = 1000 * 10;
GarbageTimer.Elapsed += GarbageTimer_Elapsed;
GarbageTimer.Start();
}
private void WriteLog(string log)
@ -79,7 +81,12 @@ namespace ClientForm
string msg = Encoding.UTF8.GetString(data, 0, data.Length);
if (msg != PServer.HB_CHECK)
{
WriteLog("WARNING!!! BAD HEARTBEAT!!!");
client.Reconnect();
heartBeatClient.Reconnect();
}
}
private void Client_OnReceived(PClient sender, PDataType dataType, byte[] data)
@ -87,9 +94,9 @@ namespace ClientForm
throw new NotImplementedException();
}
private void TestTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
private void GarbageTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//button1_Click(sender, e);
GC.Collect();
}
private void stringButton_Click(object sender, EventArgs e)
@ -107,7 +114,7 @@ namespace ClientForm
};
byte[] data = PSerializer.Serialize(person);
client.Send(PDataType.Person, data);
client.Send(PDataType.PersonObject, data);
}
private void imageButton_Click(object sender, EventArgs e)
@ -118,8 +125,39 @@ namespace ClientForm
return;
Image img = Image.FromFile(dlg.FileName);
byte[] data = PUtil.ImageToByteArray(img);
client.Send(PDataType.Image, data);
byte[] data = PUtil.ImageToBytes(img);
client.Send(PDataType.ShowImage, data);
GC.Collect();
}
private void fileButton_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "All File|*.*";
if (dlg.ShowDialog() == DialogResult.Cancel)
return;
string filePath = dlg.FileName;
string fileName = dlg.SafeFileName;
PFileData fileData = new PFileData()
{
FileName = fileName,
Data = PUtil.FileToBytes(filePath)
};
if (fileData.Data.Length >= 1000000000)
{
MessageBox.Show("1GB를 초과하는 데이터는 전송할 수 없습니다.");
GC.Collect();
return;
}
byte[] data = PSerializer.Serialize(fileData);
client.Send(PDataType.SaveFile, data);
GC.Collect();
}

@ -1,7 +1,9 @@
using System;
using PUtility;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Sockets;
@ -36,7 +38,7 @@ namespace PComm
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
this.ID = id;
this.EndPoint = new IPEndPoint(IPAddress.Parse(ip), port);
socket.Connect(EndPoint);
socket.Connect(this.EndPoint);
// send ID
this.Send(PDataType.ClientID, Encoding.UTF8.GetBytes(this.ID));
@ -54,23 +56,61 @@ namespace PComm
socket.BeginReceive(new byte[] { 0 }, 0, 0, 0, AcceptCallback, null);
}
public void Reconnect()
{
if (socket.Connected)
return;
socket.Connect(this.EndPoint);
this.Send(PDataType.ClientID, Encoding.UTF8.GetBytes(this.ID));
socket.BeginReceive(new byte[] { 0 }, 0, 0, 0, AcceptCallback, null);
}
private int SendData(byte[] data)
{
byte[] toSendData;
// 데이터 크기 100B 이하 또는 50MB 이상 압축하지 않음 (CPU 부하시간 더 큼)
// 압축한 경우 1 전송, 아닌경우 0 전송
if (data.Length <= 100 || data.Length >= 50000000)
{
byte[] isCompressed = BitConverter.GetBytes(false);
socket.Send(isCompressed, SocketFlags.None);
toSendData = data;
}
else
{
byte[] isCompressed = BitConverter.GetBytes(true);
socket.Send(isCompressed, SocketFlags.None);
toSendData = PUtil.CompressBytes(data, CompressionLevel.Optimal);
}
byte[] length = BitConverter.GetBytes(toSendData.Length);
socket.Send(length, 0, 4, SocketFlags.None);
return socket.Send(toSendData, SocketFlags.None);
}
public int Send(string msg)
{
socket.Send(BitConverter.GetBytes((int)PDataType.SimpleString), 0, 4, 0);
byte[] data = Encoding.UTF8.GetBytes(msg);
byte[] length = BitConverter.GetBytes(data.Length);
socket.Send(length, 0, 4, 0);
//byte[] length = BitConverter.GetBytes(data.Length);
//socket.Send(length, 0, 4, 0);
return socket.Send(data, SocketFlags.None);
//return socket.Send(data, SocketFlags.None);
return SendData(data);
}
public int Send(PDataType dataType, byte[] data)
{
socket.Send(BitConverter.GetBytes((int)dataType), 0, 4, 0);
byte[] length = BitConverter.GetBytes(data.Length);
socket.Send(length, 0, 4, 0);
//byte[] length = BitConverter.GetBytes(data.Length);
//socket.Send(length, 0, 4, 0);
return socket.Send(data, SocketFlags.None);
//return socket.Send(data, SocketFlags.None);
return SendData(data);
}
private void AcceptCallback(IAsyncResult ar)
@ -84,10 +124,16 @@ namespace PComm
socket.Receive(dataTypeBuff, dataTypeBuff.Length, SocketFlags.None);
PDataType dataType = (PDataType)BitConverter.ToInt32(dataTypeBuff, 0);
// check compressed
byte[] isCompressedBuff = new byte[1];
socket.Receive(isCompressedBuff, isCompressedBuff.Length, SocketFlags.None);
bool isCompressed = BitConverter.ToBoolean(isCompressedBuff, 0);
// get data size
byte[] sizeBuff = new byte[4];
socket.Receive(sizeBuff, sizeBuff.Length, SocketFlags.None);
int dataSize = BitConverter.ToInt32(sizeBuff, 0);
using (MemoryStream ms = new MemoryStream())
{
@ -105,44 +151,51 @@ namespace PComm
dataSize -= receiveSize;
}
byte[] data = ms.ToArray();
byte[] receivedData = ms.ToArray();
byte[] data;
if (isCompressed)
data = PUtil.DecompressBytes(receivedData);
else
data = receivedData;
if (OnReceived != null)
OnReceived(this, dataType, data);
}
socket.BeginReceive(new byte[] { 0 }, 0, 0, 0, AcceptCallback, null);
//byte[] buff = new byte[BUFF_SIZE];
//int receiveSize = socket.Receive(buff, buff.Length, SocketFlags.None);
//if (receiveSize <= 0)
//{
// Close();
// if (Disconnected != null)
// Disconnected(this);
//}
//else if (receiveSize < buff.Length)
//{
// Array.Resize<byte>(ref buff, receiveSize);
//}
//if (OnReceived != null)
// OnReceived(this, buff);
//socket.BeginReceive(new byte[] { 0 }, 0, 0, 0, AcceptCallback, null);
socket.BeginReceive(new byte[] { 0 }, 0, 0, 0, AcceptCallback, null);
}
}
catch (Exception ex)
{
this.Close();
Debug.WriteLine($"[ERROR] CleintAcceptCallback: {ex.Message}");
PFileManager.Instance.WriteLog($"[ERROR] CleintAcceptCallback: {ex.Message}");
if (CheckSocketConnection(socket))
{
socket.BeginReceive(new byte[] { 0 }, 0, 0, 0, AcceptCallback, null);
}
else
{
Debug.WriteLine($"[ERROR] Close socket {socket.RemoteEndPoint.ToString()}");
PFileManager.Instance.WriteLog($"[ERROR] Close socket {socket.RemoteEndPoint.ToString()}");
if (Disconnected != null)
Disconnected(this);
Close();
Debug.WriteLine($"[ERROR] CleintAcceptCallback: {ex.Message}");
if (Disconnected != null)
Disconnected(this);
}
}
}
private bool CheckSocketConnection(Socket socket)
{
bool availability = socket.Available == 0;
bool poll = socket.Poll(1000, SelectMode.SelectRead);
if (availability && poll)
return false;
else
return true;
}
public void Close()
{
socket.Close();

@ -30,6 +30,24 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
@ -49,5 +67,11 @@
<Compile Include="PServer.cs" />
<Compile Include="SocketAcceptedEventArgs.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\PUtil\PUtility.csproj">
<Project>{b6e7f933-eb73-4d83-94fa-fb17774f1d52}</Project>
<Name>PUtility</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

@ -12,8 +12,10 @@ namespace PComm
SimpleString = 10,
Person = 100,
PersonObject = 100,
Image = 200,
ShowImage = 200,
SaveFile = 300,
}
}

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PObject
{
[Serializable]
public class PFileData
{
public string FileName { get; set;}
public byte[] Data { get; set; }
}
}

@ -30,6 +30,24 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
@ -42,6 +60,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Person.cs" />
<Compile Include="PFileData.cs" />
<Compile Include="PSerializer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>

@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PUtility
{
public class PFileManager
{
public static PFileManager Instance { get; set; } = new PFileManager();
public string DirPath { get; set; } = Application.StartupPath + @"\Files";
public string LogDirPath { get; set; } = Application.StartupPath + @"\Logs";
private void CheckFolder(string dirPath)
{
DirectoryInfo directoryInfo = new DirectoryInfo(dirPath);
if (!directoryInfo.Exists)
directoryInfo.Create();
}
public void SaveFile(byte[] data, string fileName)
{
CheckFolder(this.DirPath);
if (string.IsNullOrEmpty(fileName))
fileName = Guid.NewGuid().ToString();
string fileSaveName = PUtil.CheckFileDuplication(this.DirPath, fileName);
string filePath = Path.Combine(this.DirPath, fileSaveName);
PUtil.BytesToFile(filePath, data);
}
public void WriteLog(string log)
{
CheckFolder(this.LogDirPath);
DateTime now = DateTime.Now;
string logPath = Path.Combine(this.LogDirPath, $"[{now.ToString("yyyyMMdd")}]log.txt");
File.AppendAllText(logPath, $"[{now.ToString("yyyyMMdd HH:mm:ss")}] {log + Environment.NewLine}");
}
}
}

@ -7,6 +7,8 @@ using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.IO.Compression;
using System.Diagnostics;
namespace PUtility
{
@ -35,22 +37,69 @@ namespace PUtility
target.ScrollToCaret();
}
public static byte[] ImageToByteArray(Image image)
public static byte[] CompressBytes(byte[] data, CompressionLevel level = CompressionLevel.Fastest)
{
byte[] compressedData;
using (MemoryStream ms = new MemoryStream())
{
using (GZipStream gs = new GZipStream(ms, level))
{
gs.Write(data, 0, data.Length);
}
compressedData = ms.ToArray();
}
Debug.WriteLine($"Compressed{data.Length} to {compressedData.Length} ({(float)compressedData.Length / (float)data.Length * 100f}%)");
return compressedData;
}
public static byte[] DecompressBytes(byte[] data)
{
byte[] decompressedData;
using (MemoryStream resultStream = new MemoryStream())
{
using (MemoryStream ms = new MemoryStream(data))
{
using (GZipStream gs = new GZipStream(ms, CompressionMode.Decompress))
{
gs.CopyTo(resultStream);
gs.Close();
}
}
decompressedData = resultStream.ToArray();
}
//Debug.WriteLine($"Decompressed{data.Length} to {decompressedData.Length} ({(float)decompressedData.Length / (float)data.Length * 100f}%)");
return decompressedData;
}
public static byte[] ImageToBytes(Image image)
{
byte[] data;
using (MemoryStream ms = new MemoryStream())
{
image.Save(ms, image.RawFormat);
return ms.ToArray();
data = ms.ToArray();
}
return data;
}
public static Bitmap ByteToImage(byte[] imgByteArray)
public static Bitmap BytesToImage(byte[] imgByteArray)
{
Bitmap bitmap;
using (MemoryStream ms = new MemoryStream(imgByteArray))
{
Bitmap img = Image.FromStream(ms) as Bitmap;
return img;
bitmap = Image.FromStream(ms) as Bitmap;
}
return bitmap;
}
public static void SaveImage(string fileName, Image img, ImageFormat format)
@ -60,5 +109,59 @@ namespace PUtility
saveImage.Save(fileName, format);
}
}
public static byte[] FileToBytes(string filePath)
{
return File.ReadAllBytes(filePath);
//byte[] data;
//using (FileStream fs = new FileStream(filePath, FileMode.Open))
//{
// data = new byte[fs.Length];
// fs.Read(data, 0, data.Length);
//}
//return data;
}
public static void BytesToFile(string filePath, byte[] data)
{
File.WriteAllBytes(filePath, data);
//using (FileStream fs = new FileStream(filePath, FileMode.Create))
//{
// fs.Write(data, 0, data.Length);
//}
}
public static string CheckFileDuplication(string dirPath, string fileName)
{
if (string.IsNullOrEmpty(dirPath) || string.IsNullOrEmpty(fileName))
return null;
string resultFileName = fileName;
int dotIdx = fileName.LastIndexOf(".");
string name = fileName.Substring(0, dotIdx);
string ext = fileName.Substring(dotIdx);
bool fileExist = true;
int fileCount = 0;
string dirMapPath = "";
while (fileExist)
{
dirMapPath = dirPath;
string combinedPath = Path.Combine(dirMapPath, resultFileName);
if (!File.Exists(combinedPath))
{
fileExist = false;
break;
}
fileCount++;
resultFileName = $"{name}({fileCount}){ext}";
}
return resultFileName;
}
}
}

@ -7,8 +7,8 @@
<ProjectGuid>{B6E7F933-EB73-4D83-94FA-FB17774F1D52}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>PUtil</RootNamespace>
<AssemblyName>PUtil</AssemblyName>
<RootNamespace>PUtility</RootNamespace>
<AssemblyName>PUtility</AssemblyName>
<TargetFrameworkVersion>v4.8.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
@ -30,6 +30,24 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
@ -43,6 +61,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="PFileManager.cs" />
<Compile Include="PUtil.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>

@ -32,6 +32,26 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />

@ -32,23 +32,26 @@ namespace ServerForm
log = Encoding.UTF8.GetString(data, 0, data.Length);
logHandler(clientInfo.Client, log);
break;
case PDataType.Person:
case PDataType.PersonObject:
Person person = PSerializer.Deserialize<Person>(data);
log = $"Name: {person.Name}, Age: {person.Age}";
logHandler(clientInfo.Client, log);
break;
case PDataType.Image:
Image image = PUtil.ByteToImage(data);
//string fileName = $@"{System.IO.Path.GetTempPath()}{Guid.NewGuid()}.bmp";
//PUtil.SaveImage(fileName, image, image.RawFormat);
case PDataType.ShowImage:
Image image = PUtil.BytesToImage(data);
log = $"Receive {data.Length.ToString("#,###")}bytes image";
logHandler(clientInfo.Client, log);
ImageForm imgForm = new ImageForm(image);
formHandler(imgForm);
break;
case PDataType.SaveFile:
PFileData fileData = PSerializer.Deserialize<PFileData>(data);
PFileManager.Instance.SaveFile(fileData.Data, fileData.FileName);
GC.Collect();
log = $"Receive {data.Length.ToString("#,###")}bytes file and save it";
logHandler(clientInfo.Client, log);
break;
default:
logHandler(clientInfo.Client, "ERROR!!! UNEXPECTED DATA TYPE!!!");

@ -32,6 +32,26 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />

@ -19,8 +19,8 @@ namespace ServerForm
public partial class MainForm : Form
{
private readonly int PORT = 7777;
private readonly int HB_PORT = 7778;
private readonly int PORT = 37777;
private readonly int HB_PORT = 37778;
private PServer mainServer;
private PServer heartBeatServer;
@ -29,6 +29,7 @@ namespace ServerForm
private FormHandler formHandler;
private System.Timers.Timer clientCheckTimer;
private System.Timers.Timer GarbageTimer;
public MainForm()
{
@ -38,6 +39,8 @@ namespace ServerForm
private void InitInstance()
{
string path = Application.StartupPath;
heartBeatServer = new PServer(HB_PORT);
mainServer = new PServer(PORT);
@ -54,6 +57,11 @@ namespace ServerForm
clientCheckTimer.Interval = 1000;
clientCheckTimer.Elapsed += ClientCheckTimer_Elapsed;
clientCheckTimer.Start();
GarbageTimer = new System.Timers.Timer();
GarbageTimer.Interval = 1000 * 10;
GarbageTimer.Elapsed += GarbageTimer_Elapsed;
GarbageTimer.Start();
}
private void WriteLog(PClient client, string log)
@ -129,5 +137,10 @@ namespace ServerForm
CheckClientInfo(clientInfoList);
}
private void GarbageTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
GC.Collect();
}
}
}

@ -32,6 +32,26 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />

@ -32,6 +32,26 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />

@ -24,45 +24,83 @@ EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5263D61C-F3A5-4EDA-AC6D-5F721CDBD2AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5263D61C-F3A5-4EDA-AC6D-5F721CDBD2AF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5263D61C-F3A5-4EDA-AC6D-5F721CDBD2AF}.Debug|x64.ActiveCfg = Debug|x64
{5263D61C-F3A5-4EDA-AC6D-5F721CDBD2AF}.Debug|x64.Build.0 = Debug|x64
{5263D61C-F3A5-4EDA-AC6D-5F721CDBD2AF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5263D61C-F3A5-4EDA-AC6D-5F721CDBD2AF}.Release|Any CPU.Build.0 = Release|Any CPU
{5263D61C-F3A5-4EDA-AC6D-5F721CDBD2AF}.Release|x64.ActiveCfg = Release|x64
{5263D61C-F3A5-4EDA-AC6D-5F721CDBD2AF}.Release|x64.Build.0 = Release|x64
{408C9CC8-81EB-4EC1-B2D6-55FA2F976718}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{408C9CC8-81EB-4EC1-B2D6-55FA2F976718}.Debug|Any CPU.Build.0 = Debug|Any CPU
{408C9CC8-81EB-4EC1-B2D6-55FA2F976718}.Debug|x64.ActiveCfg = Debug|x64
{408C9CC8-81EB-4EC1-B2D6-55FA2F976718}.Debug|x64.Build.0 = Debug|x64
{408C9CC8-81EB-4EC1-B2D6-55FA2F976718}.Release|Any CPU.ActiveCfg = Release|Any CPU
{408C9CC8-81EB-4EC1-B2D6-55FA2F976718}.Release|Any CPU.Build.0 = Release|Any CPU
{408C9CC8-81EB-4EC1-B2D6-55FA2F976718}.Release|x64.ActiveCfg = Release|x64
{408C9CC8-81EB-4EC1-B2D6-55FA2F976718}.Release|x64.Build.0 = Release|x64
{94E9926E-CD35-49F8-867E-2E8B76E7FB1B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{94E9926E-CD35-49F8-867E-2E8B76E7FB1B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{94E9926E-CD35-49F8-867E-2E8B76E7FB1B}.Debug|x64.ActiveCfg = Debug|x64
{94E9926E-CD35-49F8-867E-2E8B76E7FB1B}.Debug|x64.Build.0 = Debug|x64
{94E9926E-CD35-49F8-867E-2E8B76E7FB1B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{94E9926E-CD35-49F8-867E-2E8B76E7FB1B}.Release|Any CPU.Build.0 = Release|Any CPU
{94E9926E-CD35-49F8-867E-2E8B76E7FB1B}.Release|x64.ActiveCfg = Release|x64
{94E9926E-CD35-49F8-867E-2E8B76E7FB1B}.Release|x64.Build.0 = Release|x64
{420F59CE-1ED7-4194-9DCA-FCB6428AFCA3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{420F59CE-1ED7-4194-9DCA-FCB6428AFCA3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{420F59CE-1ED7-4194-9DCA-FCB6428AFCA3}.Debug|x64.ActiveCfg = Debug|x64
{420F59CE-1ED7-4194-9DCA-FCB6428AFCA3}.Debug|x64.Build.0 = Debug|x64
{420F59CE-1ED7-4194-9DCA-FCB6428AFCA3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{420F59CE-1ED7-4194-9DCA-FCB6428AFCA3}.Release|Any CPU.Build.0 = Release|Any CPU
{420F59CE-1ED7-4194-9DCA-FCB6428AFCA3}.Release|x64.ActiveCfg = Release|x64
{420F59CE-1ED7-4194-9DCA-FCB6428AFCA3}.Release|x64.Build.0 = Release|x64
{A88648B7-3EAE-4BFF-B5EF-2FED447C7B9C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A88648B7-3EAE-4BFF-B5EF-2FED447C7B9C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A88648B7-3EAE-4BFF-B5EF-2FED447C7B9C}.Debug|x64.ActiveCfg = Debug|x64
{A88648B7-3EAE-4BFF-B5EF-2FED447C7B9C}.Debug|x64.Build.0 = Debug|x64
{A88648B7-3EAE-4BFF-B5EF-2FED447C7B9C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A88648B7-3EAE-4BFF-B5EF-2FED447C7B9C}.Release|Any CPU.Build.0 = Release|Any CPU
{A88648B7-3EAE-4BFF-B5EF-2FED447C7B9C}.Release|x64.ActiveCfg = Release|x64
{A88648B7-3EAE-4BFF-B5EF-2FED447C7B9C}.Release|x64.Build.0 = Release|x64
{80B87BC0-61D3-410C-8FD1-9BF12493C511}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{80B87BC0-61D3-410C-8FD1-9BF12493C511}.Debug|Any CPU.Build.0 = Debug|Any CPU
{80B87BC0-61D3-410C-8FD1-9BF12493C511}.Debug|x64.ActiveCfg = Debug|x64
{80B87BC0-61D3-410C-8FD1-9BF12493C511}.Debug|x64.Build.0 = Debug|x64
{80B87BC0-61D3-410C-8FD1-9BF12493C511}.Release|Any CPU.ActiveCfg = Release|Any CPU
{80B87BC0-61D3-410C-8FD1-9BF12493C511}.Release|Any CPU.Build.0 = Release|Any CPU
{80B87BC0-61D3-410C-8FD1-9BF12493C511}.Release|x64.ActiveCfg = Release|x64
{80B87BC0-61D3-410C-8FD1-9BF12493C511}.Release|x64.Build.0 = Release|x64
{B6E7F933-EB73-4D83-94FA-FB17774F1D52}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B6E7F933-EB73-4D83-94FA-FB17774F1D52}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B6E7F933-EB73-4D83-94FA-FB17774F1D52}.Debug|x64.ActiveCfg = Debug|x64
{B6E7F933-EB73-4D83-94FA-FB17774F1D52}.Debug|x64.Build.0 = Debug|x64
{B6E7F933-EB73-4D83-94FA-FB17774F1D52}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B6E7F933-EB73-4D83-94FA-FB17774F1D52}.Release|Any CPU.Build.0 = Release|Any CPU
{B6E7F933-EB73-4D83-94FA-FB17774F1D52}.Release|x64.ActiveCfg = Release|x64
{B6E7F933-EB73-4D83-94FA-FB17774F1D52}.Release|x64.Build.0 = Release|x64
{BB2DA035-8C85-41C5-87EB-157C8C776633}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BB2DA035-8C85-41C5-87EB-157C8C776633}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BB2DA035-8C85-41C5-87EB-157C8C776633}.Debug|x64.ActiveCfg = Debug|x64
{BB2DA035-8C85-41C5-87EB-157C8C776633}.Debug|x64.Build.0 = Debug|x64
{BB2DA035-8C85-41C5-87EB-157C8C776633}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BB2DA035-8C85-41C5-87EB-157C8C776633}.Release|Any CPU.Build.0 = Release|Any CPU
{BB2DA035-8C85-41C5-87EB-157C8C776633}.Release|x64.ActiveCfg = Release|x64
{BB2DA035-8C85-41C5-87EB-157C8C776633}.Release|x64.Build.0 = Release|x64
{ABE747A1-0883-42B0-9A78-1D60720288CD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ABE747A1-0883-42B0-9A78-1D60720288CD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ABE747A1-0883-42B0-9A78-1D60720288CD}.Debug|x64.ActiveCfg = Debug|x64
{ABE747A1-0883-42B0-9A78-1D60720288CD}.Debug|x64.Build.0 = Debug|x64
{ABE747A1-0883-42B0-9A78-1D60720288CD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ABE747A1-0883-42B0-9A78-1D60720288CD}.Release|Any CPU.Build.0 = Release|Any CPU
{ABE747A1-0883-42B0-9A78-1D60720288CD}.Release|x64.ActiveCfg = Release|x64
{ABE747A1-0883-42B0-9A78-1D60720288CD}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

Loading…
Cancel
Save