-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSourceRconConnection.cs
133 lines (115 loc) · 4.75 KB
/
SourceRconConnection.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
123
124
125
126
127
128
129
130
131
132
133
using RConControl.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace RConControl {
public class SourceRconConnection {
// Singleton
private static SourceRconConnection instance;
public static SourceRconConnection Instance {
get {
if (instance == null) {
instance = new SourceRconConnection();
}
return instance;
}
}
//*************************************************
// Variables
//*************************************************
public string connectedIP { get; private set; }
public string connectedPort { get; private set; }
public string lastMessage { get; private set; }
public delegate void StringHandler(string str);
public delegate void VoidHandler();
public delegate void StateHandler(State state);
public event StringHandler ErrorEvent;
public event VoidHandler OnlineStateEvent;
public enum State {
Connected,
Connecting,
Disconnected
};
private SourceRcon.SourceRcon srcRcon = null;
private Language mLangMan = Language.Instance;
private Thread mThreadConnect;
private int mReconnectTries = 0;
private bool mIsConnected = false; // true, if server is really connected. false if under reconnecting
//*************************************************
// CTor
//*************************************************
private SourceRconConnection() { }
//*************************************************
// Methods
//*************************************************
public void Connect() {
if (!String.IsNullOrEmpty(Settings.Default.RconIP)) {
mIsConnected = false;
connectedIP = Settings.Default.RconIP;
connectedPort = Settings.Default.RconPort.ToString();
srcRcon = null;
srcRcon = new SourceRcon.SourceRcon();
srcRcon.Errors += new SourceRcon.StringOutput(OnError);
srcRcon.ServerOutput += new SourceRcon.StringOutput(ConsoleOutput);
srcRcon.ConnectionSuccess += new SourceRcon.BoolInfo(ConnectionSuccessInfo);
mThreadConnect = new Thread(delegate() { srcRcon.Connect(new IPEndPoint(IPAddress.Parse(Settings.Default.RconIP), Settings.Default.RconPort), Settings.Default.RconPW); });
mThreadConnect.Start();
if (mReconnectTries == 0) OnlineStateEvent();
}
}
public void Disconnect() {
connectedIP = null;
srcRcon = null;
mReconnectTries = 0;
mIsConnected = false;
OnlineStateEvent();
}
public State GetState() {
if (srcRcon != null) {
if (mIsConnected) return State.Connected;
else return State.Connecting;
}
return State.Disconnected;
}
public void Send(string cmd) {
srcRcon.ServerCommand(cmd);
}
//*************************************************
// Event Receivers
//*************************************************
private void OnError(string output) {
if (srcRcon != null) {
if (!srcRcon.Connected && mReconnectTries == 0) {
Disconnect();
ErrorEvent(output);
} else {
mIsConnected = false;
Reconnect();
}
}
}
private void ConsoleOutput(string output) {
lastMessage = output;
}
private void Reconnect() {
if (mReconnectTries < GlobalConstants.RCON_RECONNECT_TRIES) {
mReconnectTries++;
ErrorEvent(String.Format(mLangMan.GetString("Error_Reconnecting"), mReconnectTries));
Connect();
} else {
Disconnect();
ErrorEvent(String.Format(mLangMan.GetString("Error_ReconnectFailed"), GlobalConstants.RCON_RECONNECT_TRIES));
}
}
private void ConnectionSuccessInfo(bool info) {
mReconnectTries = 0;
mIsConnected = true;
OnlineStateEvent();
}
}
}