forked from GlaireDaggers/Netcode.IO.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
159 lines (131 loc) · 3.62 KB
/
Copy pathProgram.cs
File metadata and controls
159 lines (131 loc) · 3.62 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
using System;
using System.IO;
using System.Net;
using System.Threading;
using NetcodeIO.NET;
using NetcodeIO.NET.Utils.IO;
namespace Test
{
class Program
{
static readonly byte[] _privateKey = new byte[]
{
0x60, 0x6a, 0xbe, 0x6e, 0xc9, 0x19, 0x10, 0xea,
0x9a, 0x65, 0x62, 0xf6, 0x6f, 0x2b, 0x30, 0xe4,
0x43, 0x71, 0xd6, 0x2c, 0xd1, 0x99, 0x27, 0x26,
0x6b, 0x3c, 0x60, 0xf4, 0xb7, 0x15, 0xab, 0xa1,
};
private static Client client;
static void Main(string[] args)
{
startClient();
//startServer();
}
private static void startClient()
{
// get token
var webRequest = WebRequest.Create("http://127.0.0.1:8080/token");
webRequest.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response;
try
{
response = (HttpWebResponse)webRequest.GetResponse();
}
catch (Exception e)
{
Console.WriteLine("Failed to get token: " + e.Message);
Console.ReadLine();
return;
}
if (response.StatusCode != HttpStatusCode.OK)
{
Console.WriteLine("Failed to get token: " + response.StatusDescription);
Console.ReadLine();
return;
}
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseStr = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
byte[] connectToken = System.Convert.FromBase64String(responseStr);
testPacket = new byte[256];
using (var testPacketWriter = ByteArrayReaderWriter.Get(testPacket))
{
testPacketWriter.Write((uint)0xAABBCCDD);
}
client = new Client();
client.OnStateChanged += Client_OnStateChanged;
client.OnMessageReceived += Client_OnMessageReceived;
Console.WriteLine("Connecting...");
client.Connect(connectToken);
Console.ReadLine();
client.Disconnect();
}
private static void startServer()
{
Server server = new Server(
256,
"127.0.0.1", 40000,
0x1122334455667788L,
_privateKey
);
server.LogLevel = NetcodeLogLevel.Debug;
server.Start();
Console.WriteLine("Server started");
server.OnClientConnected += Server_OnClientConnected;
server.OnClientDisconnected += Server_OnClientDisconnected;
server.OnClientMessageReceived += Server_OnClientMessageReceived;
Console.ReadLine();
server.Stop();
}
private static byte[] testPacket;
private static bool isRunningClient = false;
private static void doClientSendStuff()
{
while (isRunningClient)
{
Console.WriteLine("Sent packet");
client.Send(testPacket, 256);
Thread.Sleep(100);
}
}
private static void Client_OnMessageReceived(byte[] payload, int payloadSize)
{
Console.WriteLine("Got packet!");
}
private static void Client_OnStateChanged(ClientState state)
{
Console.WriteLine("Client state changed: " + state.ToString());
if (state == ClientState.Connected)
{
// connected! start sending stuff.
isRunningClient = true;
var workThread = new Thread(doClientSendStuff);
workThread.Start();
}
else
{
if (isRunningClient)
{
isRunningClient = false;
}
}
}
private static void Server_OnClientMessageReceived(RemoteClient sender, byte[] payload, int payloadSize)
{
Console.WriteLine("Received message from " + sender.ClientID);
// just send it back to them
sender.SendPayload(payload, payloadSize);
}
private static void Server_OnClientDisconnected(RemoteClient client)
{
Console.WriteLine("Client " + client.ClientID + " disconnected");
}
private static void Server_OnClientConnected(RemoteClient client)
{
Console.WriteLine("Client " + client.ClientID + " connected");
}
}
}