forked from AvilanceLtd/StarryboundServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListenerThread.cs
68 lines (62 loc) · 2.9 KB
/
ListenerThread.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
/*
* Starrybound Server
* Copyright 2013, Avilance Ltd
* Created by Zidonuke ([email protected]) and Crashdoom ([email protected])
*
* This file is a part of Starrybound Server.
* Starrybound Server is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* Starrybound Server is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with Starrybound Server. If not, see http://www.gnu.org/licenses/.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using com.avilance.Starrybound.Util;
namespace com.avilance.Starrybound
{
class ListenerThread
{
public TcpListener tcpSocket;
public void run()
{
try
{
IPAddress localAdd = IPAddress.Parse(StarryboundServer.config.proxyIP);
tcpSocket = new TcpListener(localAdd, StarryboundServer.config.proxyPort);
tcpSocket.Start();
StarryboundServer.logInfo("Proxy server has been started on " + localAdd.ToString() + ":" + StarryboundServer.config.proxyPort);
StarryboundServer.serverState = ServerState.ListenerReady;
try
{
while (true)
{
TcpClient clientSocket = tcpSocket.AcceptTcpClient();
clientSocket.ReceiveTimeout = StarryboundServer.config.clientSocketTimeout;
clientSocket.SendTimeout = StarryboundServer.config.internalSocketTimeout;
new Thread(new ThreadStart(new Client(clientSocket).run)).Start();
}
}
catch (ThreadAbortException) { }
catch (Exception e)
{
if ((int)StarryboundServer.serverState > 3) return;
StarryboundServer.logException("ListenerThread Exception: " + e.ToString());
}
tcpSocket.Stop();
StarryboundServer.logFatal("ListenerThread has failed - No new connections will be possible.");
StarryboundServer.serverState = ServerState.Crashed;
}
catch (ThreadAbortException) { }
catch(SocketException e)
{
StarryboundServer.logFatal("TcpListener has failed to start: " + e.Message);
StarryboundServer.serverState = ServerState.Crashed;
}
}
}
}