public void Shutdown() { if (!IsConnected()) { return; } if (_workerNetwork == null) { return; } try { SendPacket(new PacketWorkerDisconnect("client exit")); } catch { ; } finally { _workerNetwork = null; } }
public WorkerNetworkHandler(WorkerNetwork network, WorkerNetworkSystem netSystem) { _network = network; _netSystem = netSystem; _isLogged = false; }
public void Tick() { if (!_isInitialized) { return; } while (!IsConnected()) { try { _remoteAddress = Dns.GetHostAddresses(_gridWorker.Settings.ServerAddress).FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork); if (_remoteAddress == null) { throw new Exception($"Unable to lookup ip for remote '{_gridWorker.Settings.ServerAddress}'"); } _tcpClient = new TcpClient(new IPEndPoint(IPAddress.Any, 0)) { ReceiveTimeout = _gridWorker.Settings.ReceiveTimeout, NoDelay = true }; _tcpClient.Connect(new IPEndPoint(_remoteAddress, (int)_gridWorker.Settings.ServerPort)); _tcpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); _workerNetwork = new WorkerNetwork(this, _tcpClient, _gridWorker); Logger.Debug("Server is connected to remote grid, wait for logon"); _workerNetwork.SendPacket(new PacketWorkerLoginRequest(_gridWorker.Settings.WorkerName)); _repeatRefuseMsg = true; } catch (SocketException se) { if (_repeatRefuseMsg) { if (se.SocketErrorCode == SocketError.ConnectionRefused) { Logger.Warn("Remote server refused connection, maybe not running (trying to connect in background)"); } else { Logger.Warn("Unable to connect to main server (trying to connect in background)", se); } _repeatRefuseMsg = false; } Thread.Sleep(_gridWorker.Settings.ConnectRepeatInterval); } catch (Exception e) { Logger.Warn("Unable to connect to main server", e); Thread.Sleep(_gridWorker.Settings.ConnectRepeatInterval); } } if (_workerNetwork == null) { _tcpClient.Close(); return; } try { _workerNetwork.ChannelRead(); _workerNetwork.CheckTimedOut(); if (!IsConnected()) { Logger.Info("Disconnected from server: Read timeout"); } } catch (IOException e) { if (_workerNetwork.IsLoggedOnServer()) { ForceDisconnect(); return; } Logger.Error("IO Error during worker network channel read", e); } }