protected virtual void ConnectionDisconnected(SocketErrorCode aValue) { }
protected override void ConnectionDisconnected(SocketErrorCode aValue) { // close the writer if (_writer != null) { _writer.Close(); } Packet.Connected = false; Packet.Part = null; Packet.Commit(); Packet.Parent.State = Bot.States.Idle; Packet.Parent.Commit(); Packet.Parent.HasNetworkProblems = false; if (Part != null) { Part.Packet = null; Part.State = FilePart.States.Closed; if (RemovePart) { Log.Info("ConnectionDisconnected(" + Packet + ") removing part"); Part.State = FilePart.States.Broken; FileActions.RemovePart(File, Part); } else { // the file is ok if the size is equal or it has an additional buffer for checking if (CurrrentSize == StopSize || (!Part.Checked && CurrrentSize == StopSize + Settings.Instance.FileRollbackCheckBytes)) { Part.State = FilePart.States.Ready; Log.Info("ConnectionDisconnected(" + Packet + ") ready" + (Part.Checked ? "" : " but unchecked")); // statistics Statistic.Instance.Increase(StatisticType.PacketsCompleted); } // that should not happen else if (CurrrentSize > StopSize) { Part.State = FilePart.States.Broken; Log.Error("ConnectionDisconnected(" + Packet + ") size is bigger than excepted: " + CurrrentSize + " > " + StopSize); // this mostly happens on the last part of a file - so lets remove the file and load the package again if (File.Parts.Count() == 1 || Part.StopSize == File.Size) { FileActions.RemoveFile(File); Log.Error("ConnectionDisconnected(" + Packet + ") removing corupted " + File); } // statistics Statistic.Instance.Increase(StatisticType.PacketsBroken); } // it did not start else if (_receivedBytes == 0) { Log.Error("ConnectionDisconnected(" + Packet + ") downloading did not start, disabling packet"); Packet.Enabled = false; Packet.Parent.HasNetworkProblems = true; // statistics Statistic.Instance.Increase(StatisticType.BotConnectsFailed); } // it is incomplete else { Log.Error("ConnectionDisconnected(" + Packet + ") incomplete"); // statistics Statistic.Instance.Increase(StatisticType.PacketsIncompleted); } } } // the connection didnt even connected to the given ip and port else { // lets disable the packet, because the bot seems to have broken config or is firewalled Log.Error("ConnectionDisconnected(" + Packet + ") connection did not work, disabling packet"); Packet.Enabled = false; Packet.Parent.HasNetworkProblems = true; // statistics Statistic.Instance.Increase(StatisticType.BotConnectsFailed); } Part.Commit(); Packet.Parent.Commit(); Disconnected(Packet, this); }
public override void Connect() { _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType + "(" + Hostname + ":" + Port + ")"); _isConnected = false; using (_tcpClient = new TcpClient()) { _tcpClient.ReceiveTimeout = (MaxData > 0 ? Settings.Instance.DownloadTimeoutTime : Settings.Instance.ServerTimeoutTime) * 1000; try { _log.Info("Connect(" + (MaxData > 0 ? "" + MaxData : "") + ") start"); _tcpClient.Connect(Hostname, Port); _isConnected = true; } catch (SocketException ex) { _errorCode = (SocketErrorCode) ex.ErrorCode; _log.Error("Connect(" + (MaxData > 0 ? "" + MaxData : "") + ") : " + ((SocketErrorCode) ex.ErrorCode), ex); } catch (Exception ex) { _log.Fatal("Connect(" + (MaxData > 0 ? "" + MaxData : "") + ")", ex); } if (_isConnected) { _log.Info("Connect(" + (MaxData > 0 ? "" + MaxData : "") + ") connected"); using (NetworkStream stream = _tcpClient.GetStream()) { // we just need a writer if reading in text mode if (MaxData == 0) { _writer = new StreamWriter(stream) {NewLine = "\r\n", AutoFlush = true}; } FireConnected(); _allowRunning = true; try { #region BINARY READING if (MaxData > 0) { using (var reader = new BinaryReader(stream)) { Int64 missing = MaxData; Int64 max = Settings.Instance.DownloadPerReadBytes; byte[] data = null; do { try { data = reader.ReadBytes((int) (missing < max ? missing : max)); } catch (ObjectDisposedException) { break; } catch (SocketException ex) { _errorCode = (SocketErrorCode) ex.ErrorCode; if (_errorCode != SocketErrorCode.InterruptedFunctionCall) { _log.Fatal("Connect(" + (MaxData > 0 ? "" + MaxData : "") + ") reading: " + (_errorCode), ex); } } catch (IOException ex) { if (ex.InnerException is SocketException) { var exi = (SocketException) ex.InnerException; _errorCode = (SocketErrorCode) exi.ErrorCode; _log.Fatal("Connect(" + (MaxData > 0 ? "" + MaxData : "") + ") reading: " + (_errorCode), ex); } else { _log.Fatal("Connect(" + (MaxData > 0 ? "" + MaxData : "") + ") reading", ex); } break; } catch (Exception ex) { _log.Fatal("Connect(" + (MaxData > 0 ? "" + MaxData : "") + ") reading", ex); break; } if (data != null && data.Length != 0) { FireDataBinaryReceived(data); missing -= data.Length; } else { _log.Warn("Connect(" + (MaxData > 0 ? "" + MaxData : "") + ") no data received"); break; } } while (_allowRunning && missing > 0); } } #endregion #region TEXT READING else { using (var reader = new StreamReader(stream)) { int failCounter = 0; string data = ""; do { try { data = reader.ReadLine(); } catch (ObjectDisposedException) { break; } catch (SocketException ex) { _errorCode = (SocketErrorCode) ex.ErrorCode; // we dont need to log this kind of exception, because it is just normal //Log("Connect(" + (MaxData > 0 ? "" + MaxData : "") + ") reading: " + ((SocketErrorCode)ex.ErrorCode), LogLevel.Exception); } catch (IOException ex) { if (ex.InnerException is SocketException) { var exi = (SocketException) ex.InnerException; _errorCode = (SocketErrorCode) exi.ErrorCode; // we dont need to log this kind of exception, because it is just normal //Log("Connect(" + (MaxData > 0 ? "" + MaxData : "") + ") reading: " + ((SocketErrorCode)exi.ErrorCode), LogLevel.Exception); } else { _log.Fatal("Connect(" + (MaxData > 0 ? "" + MaxData : "") + ") reading", ex); } break; } catch (Exception ex) { _log.Fatal("Connect(" + (MaxData > 0 ? "" + MaxData : "") + ") reading", ex); break; } if ( /*data != "" && */data != null) { if (data != "") { failCounter = 0; FireDataTextReceived(data); } } else { failCounter++; if (failCounter > Settings.Instance.MaxNoDataReceived) { _log.Warn("Connect(" + (MaxData > 0 ? "" + MaxData : "") + ") no data received"); break; } data = ""; } } while (_allowRunning); } } #endregion Disconnect(); } catch (ObjectDisposedException) { // this is ok... } _log.Info("Connect(" + (MaxData > 0 ? "" + MaxData : "") + ") end"); } } FireDisconnected(_errorCode); } _tcpClient = null; _writer = null; }
public override void SendData(string aData) { // we have to wait, until the connection is initialized - otherwise ther is nor logger if (_log != null) { _log.Debug("SendData(" + aData + ")"); } if (_writer != null) { try { _writer.WriteLine(aData); } catch (ObjectDisposedException) { // this is ok... } catch (SocketException ex) { _errorCode = (SocketErrorCode) ex.ErrorCode; // we dont need to log this kind of exception, because it is just normal //Log("SendData(" + aData + ") : " + ((SocketErrorCode)ex.ErrorCode), LogLevel.Exception); } catch (IOException ex) { if (ex.InnerException is SocketException) { var exi = (SocketException) ex.InnerException; _errorCode = (SocketErrorCode) exi.ErrorCode; // we dont need to log this kind of exception, because it is just normal //Log("SendData(" + aData + ") : " + ((SocketErrorCode)exi.ErrorCode), LogLevel.Exception); } else { if (_log != null) { _log.Fatal("SendData(" + aData + ") ", ex); } } } catch (Exception ex) { if (_log != null) { _log.Fatal("SendData(" + aData + ") ", ex); } } } }
public void FireDisconnected(SocketErrorCode aValue) { if (Disconnected != null) { Disconnected(aValue); } }
void ServerDisconnected(Core.Server aServer, SocketErrorCode aValue) { if (_servers.ContainsKey(aServer)) { ServerConnection con = _servers[aServer]; if (aServer.Enabled) { // disable the server if the host was not found // this is also triggered if we have no internet connection and disables all channels /*if( aValue == SocketErrorCode.HostNotFound || aValue == SocketErrorCode.HostNotFoundTryAgain) { aServer.Enabled = false; } else*/ { int time = Settings.Instance.ReconnectWaitTime; switch (aValue) { case SocketErrorCode.HostIsDown: case SocketErrorCode.HostUnreachable: case SocketErrorCode.ConnectionTimedOut: case SocketErrorCode.ConnectionRefused: time = Settings.Instance.ReconnectWaitTimeLong; break; // case SocketErrorCode.HostNotFound: // case SocketErrorCode.HostNotFoundTryAgain: // time = Settings.Instance.ReconnectWaitTimeReallyLong; // break; } new Timer(ServerReconnect, aServer, time * 1000, Timeout.Infinite); } } else { con.Connected -= ServerConnected; con.Disconnected -= ServerDisconnected; con.Server = null; con.IrcParser = null; _servers.Remove(aServer); } con.Connection = null; } else { Log.Error("ServerConnectionDisconnected(" + aServer + ", " + aValue + ") is not in the dictionary"); } }
protected override void ConnectionDisconnected(SocketErrorCode aValue) { _isRunning = false; _server.ErrorCode = aValue; _server.Connected = false; _server.Commit(); _timedObjects.Clear(); _latestPacketRequests.Clear(); Disconnected(_server, aValue); }