This repository was archived by the owner on Nov 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathServerConnection.cs
More file actions
169 lines (135 loc) · 5.59 KB
/
ServerConnection.cs
File metadata and controls
169 lines (135 loc) · 5.59 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
using System.Diagnostics;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
namespace TXLauncher {
internal class ServerConnection {
private Launcher Launcher;
public string gamePath { get; set; }
public bool trace { get; set; }
private readonly string host = "main.txrevive.com";
private readonly string stateServerPart = ":8080";
private readonly ushort port = 5050;
private readonly string listenHost = "localhost";
private readonly ushort stateServerListenPort = 8081;
private readonly ushort listenPort = 5051;
private Process gameProcess;
private Socket serverConnection;
private Socket clientConnection;
private NetworkStream _stream;
private ushort failedAttempts;
private ushort retryTimeout = 5000;
private DateTime connectionTime;
private List<byte> initialChunks = new();
private byte[] _buffer;
private int _countOfBytes = 91;
private ConnectionState state = ConnectionState.DISCONNECTED;
public ServerConnection(Launcher launcher) {
Launcher = launcher;
}
void setState(ConnectionState newState) {
state = newState;
if (trace) {
Console.WriteLine(state.ToString());
}
}
void BufferServerData(List<byte> buffer) {
Console.WriteLine("got data from server");
string isOverloaded = Encoding.UTF8.GetString(buffer.ToArray());
if (isOverloaded == "OVERLOADED") {
Console.WriteLine("server overloaded");
setState(ConnectionState.DISCONNECTED);
serverConnection.Close();
} else {
if (state != ConnectionState.CONNECTED_TO_SERVER) {
connectionTime = DateTime.Now;
}
setState(ConnectionState.CONNECTED_TO_SERVER);
/*if (gamePath == "") {
MessageBox.Show("Укажите путь до файла игры");
} else if (!gamePath.Trim().ToLower().EndsWith("tankix.exe")) {
MessageBox.Show("Укажите файл с названием \"tankix.exe\"");
} else {
gameProcess ??= Process.Start(gamePath);
}*/
/*if (DateTime.Now > connectionTime.AddSeconds(20)) {
Console.WriteLine("connection timed out");
serverConnection.Close();
}*/
failedAttempts = 0;
}
initialChunks?.Clear();
initialChunks.AddRange(buffer);
}
void Reconnect() {
if (failedAttempts != null) {
ConnectToServer();
} else {
while (state != ConnectionState.WAITING_FOR_DATA) {
ConnectToServer();
Task.Delay(retryTimeout * 2);
}
}
}
public void ConnectToServer() {
_buffer = new byte[_countOfBytes];
IPAddress[] address = Dns.GetHostAddresses(host);
IPEndPoint server = new(address[0], port);
Socket proxyToServer = new(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
while (!proxyToServer.Connected) {
try {
Console.WriteLine("connecting to server...");
proxyToServer.Connect(server);
} catch {
Console.WriteLine("server not responding");
}
Task.Delay(1000);
}
NetworkStream Stream = new(proxyToServer);
_stream = Stream;
Console.WriteLine("connected to server");
serverConnection = proxyToServer;
setState(ConnectionState.WAITING_FOR_DATA);
connectionTime = DateTime.Now;
try {
_stream.BeginRead(_buffer, 0, _countOfBytes, ReceiveCallback, null);
} catch (SocketException) {
if (state == ConnectionState.CONNECTED_TO_SERVER ||
state == ConnectionState.READY ||
state == ConnectionState.CLOSED_BY_CLIENT) {
Console.WriteLine("disconnected from server after " + (DateTime.Now - connectionTime));
} else {
Console.WriteLine($"connection attempt {++failedAttempts} failed");
}
setState(state == ConnectionState.READY ?
ConnectionState.CLOSED_BY_SERVER :
ConnectionState.DISCONNECTED);
serverConnection = null;
initialChunks = null;
clientConnection?.Close();
Reconnect();
}
}
void ReceiveCallback(IAsyncResult result) {
BufferServerData(_buffer.ToList());
_stream.EndRead(result);
_stream.BeginRead(_buffer, 0, _countOfBytes, ReceiveCallback, null);
}
}
public static class CheckNetwork {
public static bool CheckNetworkAvailable() {
var ping = new Ping();
string host = "google.com";
byte[] buffer = new byte[32];
int timeout = 1000;
var options = new PingOptions();
try {
var reply = ping.Send(host, timeout, buffer, options);
return reply.Status == IPStatus.Success;
} catch (PingException) {
return false;
}
}
}
}