public async Task SendRawMessage(string message) { if (await _WaitForConnection()) { await _outputStream.WriteLineAsync(message); await _outputStream.FlushAsync(); } else { TimeoutException ex = new TimeoutException("Could not send message because the operation timed out before a valid connection was found."); ex.Data.Add("message", message); await OnConnectionException.InvokeAsync(this, new ConnectionExceptionEventArgs(ex)); } }
/// <summary> /// Connect to remote server async. Retries to connect <see cref="ReconnectTryCount"/> times if server is unavailable. (Implementation of <see cref="IConnectable"/>) /// </summary> public async Task ConnectAsync() { if (Connected) { return; } try { await ConnectInternalAsync(); } catch (SocketException ex) { _open = false; OnConnectionException?.Invoke(new EstablishConnectionException(ex)); } }
/// <summary> /// Connect to remote server. Retries to connect <see cref="ReconnectTryCount"/> times if server is unavailable. (Implementation of <see cref="IConnectable"/>) /// </summary> public void Connect() { if (Connected) { return; } try { ConnectInternal(); OnConnected?.Invoke(); } catch (SocketException ex) { _open = false; OnConnectionException?.Invoke(new EstablishConnectionException(ex)); } }
public async Task <ITwitchIRCChannel> JoinChannel(string name) { if (await _WaitForConnection()) { await _outputStream.WriteLineAsync(String.Format("JOIN #{0}", name)); await _outputStream.FlushAsync(); return(_channels.GetOrAdd(name, new TwitchIRCChannel(name, this))); } else { TimeoutException ex = new TimeoutException("Could not join channel because the operation timed out before a valid connection was found."); ex.Data.Add("channel", name); await OnConnectionException.InvokeAsync(this, new ConnectionExceptionEventArgs(ex)); } return(null); }
public async Task LeaveChannel(ITwitchIRCChannel channel) { if (await _WaitForConnection()) { ITwitchIRCChannel removed; if (_channels.TryRemove(channel.Name, out removed)) { await _outputStream.WriteLineAsync(String.Format("PART #{0}", channel.Name)); await _outputStream.FlushAsync(); } } else { TimeoutException ex = new TimeoutException("Could not leave channel because the operation timed out before a valid connection was found."); ex.Data.Add("channel", channel); await OnConnectionException.InvokeAsync(this, new ConnectionExceptionEventArgs(ex)); } }
private async Task <int> RetrySendAction(Func <Task <int> > sendAction, Func <Task> connectAction) { try { return(await sendAction()); } catch (SocketException ex) { try { Close(); await connectAction(); await Task.Delay(100); return(await sendAction()); } catch (SocketException sex) { OnConnectionException?.Invoke(new SendMessageConnectionException(ex)); } } return(-1); }
private void ReceivePacket(object sender, SocketAsyncEventArgs e) { if (e.BytesTransferred == 0) { Close(); OnConnectionException?.Invoke(new ReceiveMessageConnectionException(new SocketException())); return; } var token = e.UserToken as EventArgsToken; token.SetRate(e.BytesTransferred); var totalReceived = e.Offset + e.BytesTransferred; if (totalReceived >= e.Buffer.Length) { if (token.ReadHeader) {//extract message size from envelope header and start receiving actual payload var buffer = e.Buffer; if (BitConverter.IsLittleEndian) { Array.Reverse(buffer); } var messageLength = BitConverter.ToInt32(buffer, 0); ReceiveLoop(messageLength, false); } else { //restart loop for new messages ReceiveLoop(); try { //whole payload was received, pass it to handler OnReceive?.Invoke(new ReceiveContext { Payload = e.Buffer, Rate = token.Rate, ReceiveDuration = token.ReceiveDuration }); } catch (Exception ex) { Trace.WriteLine(ex.ToString()); } } } else {//didn't get full message, continue receiving token.ChunkTimestamp = DateTime.UtcNow.Ticks; e.UserToken = token; e.SetBuffer(e.Buffer, totalReceived, e.Buffer.Length - totalReceived); if (!_socket.ReceiveAsync(e)) { ReceivePacket(this, e); } } }
protected void InvokeOnConnectionException() { OnConnectionException?.Invoke(); }
private async Task _InvokeConnectionException(ConnectionExceptionEventArgs e) { await OnConnectionException.InvokeAsync(this, e); }