private async Task OnReconnect() { if (State == IrcConnectionState.Disconnected) { State = IrcConnectionState.Connecting; await OpenSocketAsync(); } }
private async Task OnOther(IrcMessage message) { int code; if (int.TryParse(message.Command, out code)) { var e = new IrcInfoEventArgs(message); if (e.Code == IrcCode.RPL_WELCOME) { if (e.Text.StartsWith("Welcome to the ")) { var parts = e.Text.Split(' '); NetworkName = parts[3]; } State = IrcConnectionState.Connected; } if (_captures.Count > 0) { var capturesToRemove = new List <IrcCodeHandler>(); List <IrcCodeHandler> copy; lock (_captures) { copy = _captures.ToList(); } foreach (var capture in copy.Where(c => c.Codes.Contains(e.Code))) { bool result = await capture.Handler(e).ConfigureAwait(false); if (result) { // if it returns true remove the handler capturesToRemove.Add(capture); } if (e.Handled) { break; // if its handled stop processing } } lock (_captures) { _captures = _captures.Except(capturesToRemove).ToList(); } } RaiseEvent(InfoReceived, e); } }
/// <summary> /// Default constructor. /// </summary> public IrcConnection() { State = IrcConnectionState.Disconnected; UserModes = new char[0]; }
internal IrcConnectionStateEventArgs(IrcConnectionState state) : this(state, null) { }
private IrcConnectionStateEventArgs(IrcConnectionState state, Exception faultReason) { _state = state; _faultReason = faultReason; }
private void OnConnectCompleted(IAsyncResult ar) { var asyncState = (AsyncState)(ar.AsyncState); try { asyncState.Client.EndConnect(ar); _client = asyncState.Client; _stream = asyncState.Client.GetStream(); State = IrcConnectionState.Opened; BeginRead(); if (asyncState.Buffer != null) { BeginWrite(asyncState.Buffer); } } catch (Exception exc) { Fault(exc); } }
private void BeginConnect(Byte[] bufferToSend) { EnsureCreated(); State = IrcConnectionState.Opening; if ((_client != null) && (_client.Connected)) { _stream = _client.GetStream(); _state = IrcConnectionState.Opened; BeginRead(); } else { _client = new TcpClient(); _client.BeginConnect(_endPoint.Address, _endPoint.Port, OnConnectCompleted, CreateAsyncState(bufferToSend)); } }
protected void Fault(Exception reason) { if (State == IrcConnectionState.Faulted) { throw new InvalidOperationException(); } _state = IrcConnectionState.Faulted; _faultReason = reason; OnStateChanged(new IrcConnectionStateEventArgs(reason)); }
public void Dispose() { GC.SuppressFinalize(this); if (_events != null) { _events.Dispose(); } if (_stream != null) { _stream.Close(); _stream = null; } if (_client != null) { _client.Close(); _client = null; } if (State != IrcConnectionState.Faulted) { State = IrcConnectionState.Closed; } }
/// <summary> /// Asynchronnous closing of connection /// </summary> public void CloseAsync() { if (State != IrcConnectionState.Faulted) { State = IrcConnectionState.Closing; Dispose(); } }