-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathExternalCounterpart.cs
122 lines (111 loc) · 3.51 KB
/
ExternalCounterpart.cs
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
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace Hacknet
{
public class ExternalCounterpart
{
private static Dictionary<string, string> networkIPList;
private static ASCIIEncoding encoder;
private byte[] buffer;
private TcpClient connection;
public string connectionIP;
public string idName;
public bool isConnected;
public ExternalCounterpart(string idName, string ipEndpoint)
{
this.idName = idName;
connectionIP = ipEndpoint;
buffer = new byte[4096];
if (encoder != null)
return;
encoder = new ASCIIEncoding();
}
public static string getIPForServerName(string serverName)
{
if (networkIPList == null)
loadNetIPList();
if (networkIPList.ContainsKey(serverName))
return networkIPList[serverName];
throw new InvalidOperationException("Server Name Not Found");
}
private static void loadNetIPList()
{
networkIPList = new Dictionary<string, string>();
var str1 = Utils.readEntireFile("Content/Network/NetworkIPList.txt");
var separator = new string[2]
{
"\n\r",
"\r\n"
};
var num = 1;
foreach (var str2 in str1.Split(separator, (StringSplitOptions) num))
{
var strArray = str2.Split(Utils.spaceDelim);
networkIPList.Add(strArray[0], strArray[1]);
}
}
public void sendMessage(string message)
{
if (!isConnected)
return;
writeMessage(message);
}
public void disconnect()
{
if (!isConnected)
return;
writeMessage("Disconnecting");
connection.Close();
isConnected = false;
}
public void testConnection()
{
establishConnection();
while (!isConnected)
Thread.Sleep(5);
writeMessage("Test Message From " + idName);
}
public void establishConnection()
{
var tcpClient = new TcpClient();
tcpClient.BeginConnect(IPAddress.Parse(connectionIP), Multiplayer.PORT, DoTcpConnectionCallback, tcpClient);
}
private void DoTcpConnectionCallback(IAsyncResult ar)
{
var tcpClient = ar.AsyncState as TcpClient;
if (tcpClient == null || !tcpClient.Connected)
return;
isConnected = true;
connection = tcpClient;
tcpClient.EndConnect(ar);
}
public void writeMessage(string message)
{
if (isConnected)
{
var stream = connection.GetStream();
buffer = encoder.GetBytes(message);
stream.BeginWrite(buffer, 0, buffer.Length, TCPWriteMessageCallback, stream);
}
else
establishConnection();
}
private void TCPWriteMessageCallback(IAsyncResult ar)
{
var networkStream = ar.AsyncState as NetworkStream;
if (networkStream == null)
return;
try
{
networkStream.EndWrite(ar);
}
catch (Exception ex)
{
}
}
}
}