-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnection_Handler.cs
More file actions
217 lines (182 loc) · 6.99 KB
/
Copy pathConnection_Handler.cs
File metadata and controls
217 lines (182 loc) · 6.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System.Net.Sockets;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Windows;
using System.Windows.Threading;
using Main_Server.DTOs.Ezcade;
namespace EZcade_Client
{
public class Connection_Handler
{
#region Fields
//public string serverIp = "127.0.0.1";
//public string serverIp = "213.207.200.115";
public string serverIp = "192.168.1.122";
public int serverPort = 1001;
public bool server_connection_status { get; set; }
public TcpClient client;
private NetworkStream stream;
private DispatcherTimer timer;
#endregion
#region Constructor
public Connection_Handler()
{
try
{
client = new TcpClient();
client.Connect(serverIp, serverPort);
stream = client.GetStream();
server_connection_status = false;
}
catch (Exception ex)
{
MessageBox.Show($"Client error: {ex.Message}");
}
}
#endregion
#region Methods
public BatchListDto_Ezcade GetBatchList()
{
var Request = new JsonObject();
Request["requestType"] = "batchList";
var response = Request_Json(Request);
string json = response["data"]!.GetValue<string>();
return JsonSerializer.Deserialize<BatchListDto_Ezcade>(json);
}
#region Encryption Methods
public static string EncryptData(string data)
{
return data;
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = Convert.FromBase64String("5EjbJ1cMefXwTG8vqn5WPkpQbV5LZp89JaXbkGgNctM=");
aesAlg.IV = Convert.FromBase64String("n1QSgyiWu1Efo7N7EbggMA==");
aesAlg.Mode = CipherMode.CBC;
aesAlg.Padding = PaddingMode.PKCS7;
using (ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV))
{
byte[] dataBytes = Encoding.UTF8.GetBytes(data);
byte[] encryptedData = encryptor.TransformFinalBlock(dataBytes, 0, dataBytes.Length);
return Convert.ToBase64String(encryptedData);
}
}
}
public static string DecryptData(string encryptedData)
{
return encryptedData;
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = Convert.FromBase64String("5EjbJ1cMefXwTG8vqn5WPkpQbV5LZp89JaXbkGgNctM=");
aesAlg.IV = Convert.FromBase64String("n1QSgyiWu1Efo7N7EbggMA==");
aesAlg.Mode = CipherMode.CBC;
aesAlg.Padding = PaddingMode.PKCS7;
using (ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV))
{
byte[] encryptedBytes = Convert.FromBase64String(encryptedData);
byte[] decryptedData = decryptor.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);
return Encoding.UTF8.GetString(decryptedData);
}
}
}
#endregion
#region Connection Methods
public JsonObject Request_Json(JsonObject request)
{
string request_jsonString = JsonSerializer.Serialize(request);
request_jsonString = EncryptData(request_jsonString);
byte[] dataToSend = Encoding.UTF8.GetBytes(request_jsonString);
int dataLength_send = dataToSend.Length;
byte[] lengthBytes = BitConverter.GetBytes(dataLength_send);
stream.Write(lengthBytes, 0, lengthBytes.Length);
stream.Write(dataToSend, 0, dataToSend.Length);
byte[] lengthBuffer = new byte[4];
int bytesRead = 0;
while (bytesRead < 4)
{
bytesRead += stream.Read(lengthBuffer, bytesRead, 4 - bytesRead);
}
int dataLength = BitConverter.ToInt32(lengthBuffer, 0);
byte[] dataBuffer = new byte[dataLength];
int totalBytesRead = 0;
while (totalBytesRead < dataLength)
{
int chunkSize = stream.Read(dataBuffer, totalBytesRead, dataLength - totalBytesRead);
totalBytesRead += chunkSize;
}
string response_jsonString = Encoding.UTF8.GetString(dataBuffer);
response_jsonString = DecryptData(response_jsonString);
JsonObject response = JsonSerializer.Deserialize<JsonObject>(response_jsonString);
return response;
}
public bool Status_Check()
{
try
{
var Request = new JsonObject();
Request["requestType"] = "status";
//string jsonString = JsonSerializer.Serialize(Request);
//jsonString = EncryptData(jsonString);
//byte[] dataToSend = Encoding.UTF8.GetBytes(jsonString);
//if (stream != null)
//{
// stream.Write(dataToSend, 0, dataToSend.Length);
//}
//else
//{
// return false;
//}
//byte[] buffer = new byte[1024];
//int bytesRead = 0;
//bytesRead = stream.Read(buffer, 0, buffer.Length);
//if (bytesRead == 0)
//{
// server_connection_status = false;
// return false;
//}
//string response_jsonString = Encoding.ASCII.GetString(buffer, 0, bytesRead);
//response_jsonString = DecryptData(response_jsonString);
//var Response = JsonSerializer.Deserialize<JsonObject>(response_jsonString);
var Response = Request_Json(Request);
if (Response["status"].ToString() == "is_connected")
{
server_connection_status = true;
return true;
}
server_connection_status = false;
return false;
}
catch (NullReferenceException)
{
server_connection_status = false;
return false;
}
catch (System.IO.IOException)
{
server_connection_status = false;
return false;
}
}
public void CloseConnection()
{
try
{
if (client != null && client.Connected)
{
client.Close();
}
}
catch (Exception ex)
{
Console.WriteLine($"Error closing connection: {ex.Message}");
}
}
public void Cleanup()
{
CloseConnection();
}
#endregion
#endregion
}
}