-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.cs
115 lines (91 loc) · 3.79 KB
/
Server.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
using FLORENCE;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using Valve.Sockets;
namespace FLORENCE
{
public class Server
{
public Server()
{
NetworkingSockets server = new NetworkingSockets();
uint pollGroup = server.CreatePollGroup();
StatusCallback status = (ref StatusInfo info) =>
{
switch (info.connectionInfo.state)
{
case ConnectionState.None:
break;
case ConnectionState.Connecting:
server.AcceptConnection(info.connection);
server.SetConnectionPollGroup(pollGroup, info.connection);
break;
case ConnectionState.Connected:
Console.WriteLine("Client connected - ID: " + info.connection + ", IP: " + info.connectionInfo.address.GetIP());
break;
case ConnectionState.ClosedByPeer:
case ConnectionState.ProblemDetectedLocally:
server.CloseConnection(info.connection);
Console.WriteLine("Client disconnected - ID: " + info.connection + ", IP: " + info.connectionInfo.address.GetIP());
break;
}
};
NetworkingUtils utils = new NetworkingUtils();
utils.SetStatusCallback(status);
Address address = new Address();
address.SetAddress("::0", port);
uint listenSocket = server.CreateListenSocket(ref address);
#if VALVESOCKETS_SPAN
MessageCallback message = (in NetworkingMessage netMessage) => {
Console.WriteLine("Message received from - ID: " + netMessage.connection + ", Channel ID: " + netMessage.channel + ", Data length: " + netMessage.length);
};
#else
const int maxMessages = 20;
NetworkingMessage[] netMessages = new NetworkingMessage[maxMessages];
#endif
while (!Console.KeyAvailable)
{
server.RunCallbacks();
#if VALVESOCKETS_SPAN
server.ReceiveMessagesOnPollGroup(pollGroup, message, 20);
#else
int netMessagesCount = server.ReceiveMessagesOnPollGroup(pollGroup, netMessages, maxMessages);
if (netMessagesCount > 0)
{
for (int i = 0; i < netMessagesCount; i++)
{
ref NetworkingMessage netMessage = ref netMessages[i];
Console.WriteLine("Message received from - ID: " + netMessage.connection + ", Channel ID: " + netMessage.channel + ", Data length: " + netMessage.length);
netMessage.Destroy();
}
}
#endif
Thread.Sleep(15);
}
server.DestroyPollGroup(pollGroup);
}
public static void CreateAndSendNewMessage()
{
byte[] data = new byte[64];
sockets.SendMessageToConnection(connection, data);
}
public static void CopyPayloadFromMessage()
{
byte[] buffer = new byte[1024];
netMessage.CopyTo(buffer);
}
public static void SetA_HookForDebugInformation()
{
DebugCallback debug = (type, message) => {
Console.WriteLine("Debug - Type: " + type + ", Message: " + message);
};
NetworkingUtils utils = new NetworkingUtils();
utils.SetDebugCallback(DebugType.Everything, debug);
}
}
}