forked from chronoxor/NetCoreServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
118 lines (95 loc) · 3.55 KB
/
Copy pathProgram.cs
File metadata and controls
118 lines (95 loc) · 3.55 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
using System;
using System.Net;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using NetCoreServer;
namespace SslChatServer
{
class ChatSession : SslSession
{
public ChatSession(SslServer server) : base(server) {}
protected override void OnConnected()
{
Console.WriteLine($"Chat SSL session with Id {Id} connected!");
}
protected override void OnHandshaked()
{
Console.WriteLine($"Chat SSL session with Id {Id} handshaked!");
// Send invite message
string message = "Hello from SSL chat! Please send a message or '!' to disconnect the client!";
Send(message);
}
protected override void OnDisconnected()
{
Console.WriteLine($"Chat SSL session with Id {Id} disconnected!");
}
protected override void OnReceived(byte[] buffer, long offset, long size)
{
string message = Encoding.UTF8.GetString(buffer, (int)offset, (int)size);
Console.WriteLine("Incoming: " + message);
// Multicast message to all connected sessions
Server.Multicast(message);
// If the buffer starts with '!' the disconnect the current session
if (message == "!")
Disconnect();
}
protected override void OnError(SocketError error)
{
Console.WriteLine($"Chat SSL session caught an error with code {error}");
}
}
class ChatServer : SslServer
{
public ChatServer(SslContext context, IPAddress address, int port) : base(context, address, port) {}
protected override SslSession CreateSession() { return new ChatSession(this); }
protected override void OnError(SocketError error)
{
Console.WriteLine($"Chat SSL server caught an error with code {error}");
}
}
class Program
{
static void Main(string[] args)
{
// SSL server port
int port = 2222;
if (args.Length > 0)
port = int.Parse(args[0]);
Console.WriteLine($"SSL server port: {port}");
Console.WriteLine();
// Create and prepare a new SSL server context
var context = new SslContext(SslProtocols.Tls13, new X509Certificate2("server.pfx", "qwerty"));
// Create a new SSL chat server
var server = new ChatServer(context, IPAddress.Any, port);
// Start the server
Console.Write("Server starting...");
server.Start();
Console.WriteLine("Done!");
Console.WriteLine("Press Enter to stop the server or '!' to restart the server...");
// Perform text input
for (;;)
{
string line = Console.ReadLine();
if (string.IsNullOrEmpty(line))
break;
// Restart the server
if (line == "!")
{
Console.Write("Server restarting...");
server.Restart();
Console.WriteLine("Done!");
continue;
}
// Multicast admin message to all sessions
line = "(admin) " + line;
server.Multicast(line);
}
// Stop the server
Console.Write("Server stopping...");
server.Stop();
Console.WriteLine("Done!");
}
}
}