I am working on a service that is supposed to run on a large installation 24/7. Once the software is deployed, I will most likely not be able to make any changes anymore. This is my first time working on a network application so I want to make sure I am not making any obvious mistakes.
60 instances of this program are supposed to run on 30 machines, meaning there are 2 instances of it running on each machine. It is used to keep all the client instances (running this code) connected to the controller that gives them updates when needed.
I am wondering if there are any optimizations you guys can recommend to me or if there are any other insufficiencies that catch your eyes.
public class ControllerConnectionService {
public ControllerConnectionService() {
Initialize();
}
private Timer _pingTimer;
private SimpleTcpClient _tcpClient;
public event EventHandler<DataModel> ContentTypeChanged;
private void Initialize() {
CreateTcpClient();
Connect();
var messageString = GetMessageString(MessageType.SendId, MainViewModel.Id.ToString());
SendMessage(messageString);
_pingTimer = new Timer {Interval = 2000};
_pingTimer.Elapsed += PingTimerOnElapsed;
_pingTimer.Start();
}
private void PingTimerOnElapsed(object sender, ElapsedEventArgs elapsedEventArgs) {
if (!IsConnected()) Reconnect();
}
private bool IsConnected() {
try {
var pingBytes = Encoding.UTF8.GetBytes(GetMessageString(MessageType.Ping, string.Empty));
_tcpClient.TcpClient.Client.Send(pingBytes);
return true;
}
catch (Exception) {
return false;
}
}
private void CreateTcpClient() {
_tcpClient = new SimpleTcpClient();
_tcpClient.DataReceived += TcpClientOnDataReceived;
}
private void Connect() {
try {
_tcpClient.Connect(ControllerIp, ClientControllerPort);
}
catch (Exception) {
Reconnect();
}
}
private void Reconnect() {
try {
while (!IsConnected()) {
Thread.Sleep(500);
CreateTcpClient();
_tcpClient.Connect(ControllerIp, ClientControllerPort);
}
}
catch (Exception) {
Reconnect();
}
}
private void SendMessage(string message) {
try {
_tcpClient.TcpClient.Client.Send(Encoding.UTF8.GetBytes(message));
}
catch (Exception) {
Reconnect();
}
}
private void TcpClientOnDataReceived(object sender, Message message) {
if (string.IsNullOrEmpty(message.MessageString))
return;
var messageParts = message.MessageString.Split(';');
Enum.TryParse(messageParts[0], out MessageType messagetype);
if (messagetype == MessageType.UpdateContentType) {
Console.WriteLine($@"{DateStamp} Data received: {message.MessageString} ");
var dataModel = JsonConvert.DeserializeObject<DataModel>(messageParts[1]);
ContentTypeChanged?.Invoke(this, dataModel);
}
}
}
If you require any additional information or code, feel free to ask.