/// <summary> /// Initializes a new instance of the <see cref="ServiceExceptionContext" /> class. /// </summary> /// <param name="exception">The exception that was caught.</param> /// <param name="bufferSlice">The buffer slice that caused the exception (directly = protocol failure, indirectly = the handling failed).</param> public ServiceExceptionContext(Exception exception, IBufferSlice bufferSlice) { if (exception == null) throw new ArgumentNullException("exception"); if (bufferSlice == null) throw new ArgumentNullException("bufferSlice"); BufferSlice = bufferSlice; Exception = exception; }
/// <summary> /// Initializes a new instance of the <see cref="ChannelTcpClient{T}" /> class. /// </summary> /// <param name="encoder">Used to encode outbound messages.</param> /// <param name="decoder">Used to decode inbound messages.</param> /// <param name="readBuffer">Buffer used to receive bytes..</param> /// <exception cref="System.ArgumentNullException"> /// encoder /// or /// decoder /// or /// readBuffer /// </exception> public ChannelTcpClient(IMessageEncoder encoder, IMessageDecoder decoder, IBufferSlice readBuffer) { if (encoder == null) { throw new ArgumentNullException("encoder"); } if (decoder == null) { throw new ArgumentNullException("decoder"); } if (readBuffer == null) { throw new ArgumentNullException("readBuffer"); } _channel = new TcpChannel(readBuffer, encoder, decoder) { Disconnected = OnDisconnect, MessageSent = OnSendCompleted, MessageReceived = OnChannelMessageReceived }; decoder.MessageReceived = OnMessageReceived; _args.Completed += OnConnect; }
/// <summary> /// Initializes a new instance of the <see cref="TcpChannel" /> class. /// </summary> /// <param name="readBuffer">Buffer used for our reading.</param> /// <param name="encoder">Used to encode messages before they are put in the MicroMessage body of outbound messages.</param> /// <param name="decoder"> /// Used to decode the body of incoming MicroMessages. The <c>MessageReceived</c> delegate will be /// overridden by this class. /// </param> public TcpChannel(IBufferSlice readBuffer, IMessageEncoder encoder, IMessageDecoder decoder) { if (readBuffer == null) throw new ArgumentNullException("readBuffer"); if (encoder == null) throw new ArgumentNullException("encoder"); if (decoder == null) throw new ArgumentNullException("decoder"); _readArgs = new SocketAsyncEventArgs(); _readArgs.SetBuffer(readBuffer.Buffer, readBuffer.Offset, readBuffer.Capacity); _readArgs.Completed += OnReadCompleted; _readArgsWrapper = new SocketAsyncEventArgsWrapper(_readArgs); _encoder = encoder; _decoder = decoder; _decoder.MessageReceived = OnMessageReceived; _writeArgs = new SocketAsyncEventArgs(); _writeArgs.Completed += OnSendCompleted; _writeArgsWrapper = new SocketAsyncEventArgsWrapper(_writeArgs); _sendCompleteAction = (channel, message) => { }; _disconnectAction = (channel, exception) => { }; ChannelFailure = (channel, error) => HandleDisconnect(SocketError.ProtocolNotSupported); RemoteEndpoint = EmptyEndpoint.Instance; ChannelId = GuidFactory.Create().ToString(); Data = new ChannelData(); }
/// <summary> /// Initializes a new instance of the <see cref="HttpMessageBuilder" /> class. /// </summary> public HttpMessageBuilder() { _headerParser.HeaderParsed += OnHeader; _headerParser.Completed += OnHeaderComplete; _headerParser.RequestLineParsed += OnRequestLine; _bodySlice = _stack.Pop(); }
/// <summary> /// Initializes a new instance of the <see cref="SliceStream" /> class. /// </summary> /// <param name="slice">The slice to use.</param> /// <exception cref="System.ArgumentNullException">slice</exception> public SliceStream(IBufferSlice slice) { if (slice == null) throw new ArgumentNullException("slice"); _slice = slice; _offset = 0; _length = 0; }
public SendMessage(IBufferSlice slice) { if (slice == null) throw new ArgumentNullException("slice"); BufferSlice = slice; }
private SecureTcpChannel CreateServerChannel(IBufferSlice slice, IMessageEncoder encoder, IMessageDecoder decoder) { var streamBuilder = new ServerSideSslStreamBuilder(_certificate); var channel = new SecureTcpChannel(slice, encoder, decoder, streamBuilder); return channel; }
public SocketBuffer(IBufferSlice readBuffer) { Buffer = readBuffer.Buffer; Capacity = readBuffer.Capacity; BaseOffset = readBuffer.Offset; Offset = readBuffer.Offset; }
/// <summary> /// Initializes a new instance of the <see cref="MessagingClientContext" /> class. /// </summary> /// <param name="readBuffer">The read buffer.</param> /// <param name="formatterFactory">Used to format messages </param> public MessagingClientContext(IBufferSlice readBuffer, IMessageFormatterFactory formatterFactory) : base(readBuffer) { if (formatterFactory == null) throw new ArgumentNullException("formatterFactory"); _formatterFactory = formatterFactory; _messageBuilder = _formatterFactory.CreateBuilder(); }
/// <summary> /// Initializes a new instance of the <see cref="StringBufferSliceReader"/> class. /// </summary> public StringBufferSliceReader(IBufferSlice slice, int count) { if (slice == null) throw new ArgumentNullException("slice"); _reader = slice; _length = count; _encoding = Encoding.ASCII; }
/// <summary> /// Create a new channel /// </summary> /// <param name="readBuffer">Buffer which should be used when reading from the socket</param> /// <param name="encoder">Used to encode outgoing data</param> /// <param name="decoder">Used to decode incoming data</param> /// <returns>Created channel</returns> public ITcpChannel Create(IBufferSlice readBuffer, IMessageEncoder encoder, IMessageDecoder decoder) { var channel = new TcpChannel(readBuffer, encoder, decoder); if (OutboundMessageQueueFactory != null) channel.OutboundMessageQueue = OutboundMessageQueueFactory(); return channel; }
/// <summary> /// Initializes a new instance of the <see cref="MicroMessageEncoder" /> class. /// </summary> /// <param name="serializer">Serialiser used to serialize the messages that should be sent. You might want to pick a serializer which is reasonable fast.</param> public MicroMessageEncoder(IMessageSerializer serializer) { if (serializer == null) throw new ArgumentNullException("serializer"); _serializer = serializer; _bufferSlice = new BufferSlice(new byte[65535], 0, 65535); }
/// <summary> /// Initializes a new instance of the <see cref="SliceSocketWriterJob" /> class. /// </summary> /// <param name="slice">Slice to send.</param> /// <param name="count">Number of bytes in the slice.</param> public SliceSocketWriterJob(IBufferSlice slice, int count) { if (slice == null) throw new ArgumentNullException("slice"); _slice = slice; _offset = _slice.Offset; _bytesLeft = count; _length = count; }
/// <summary> /// Send information to the remote end point /// </summary> /// <param name="slice">Buffer slice</param> /// <param name="length">Number of bytes in the slice.</param> public void Send(IBufferSlice slice, int length) { if (slice == null) { throw new ArgumentNullException("slice"); } this.writer.Send(new SliceSocketWriterJob(slice, length)); }
/// <summary> /// Set buffer which can be used for the currently active write operation. /// </summary> /// <param name="bufferSlice">Slice</param> public void SetWriteBuffer(IBufferSlice bufferSlice) { if (bufferSlice == null) { throw new ArgumentNullException("bufferSlice"); } this.writer.SetBuffer(bufferSlice); }
/// <summary> /// Enqueue a slice to be able to re-use it later /// </summary> /// <param name="bufferSlice">Slice to append</param> /// <exception cref="System.ArgumentNullException">bufferSlice</exception> public void Push(IBufferSlice bufferSlice) { if (bufferSlice == null) { throw new ArgumentNullException("bufferSlice"); } _slices.Push(bufferSlice); }
/// <summary> /// Initializes a new instance of the <see cref="HttpMessageBuilder" /> class. /// </summary> /// <param name="stack">Slices are used when processing incoming data.</param> /// <example> /// <code> /// var builder = new HttpMessageBuilder(new BufferSliceStack(100, 65535)); /// </code> /// </example> public HttpMessageBuilder(IBufferSliceStack stack) { this.stack = stack; this.headerParser.HeaderParsed += this.OnHeader; this.headerParser.Completed += this.OnHeaderComplete; this.headerParser.RequestLineParsed += this.OnRequestLine; this.bodySlice = this.stack.Pop(); }
/// <summary> /// Initializes a new instance of the <see cref="HttpMessageBuilder" /> class. /// </summary> /// <param name="stack">Slices are used when processing incoming data.</param> /// <example> /// <code> /// var builder = new HttpMessageBuilder(new BufferSliceStack(100, 65535)); /// </code> /// </example> public HttpMessageBuilder(IBufferSliceStack stack) { _stack = stack; _headerParser.HeaderParsed += OnHeader; _headerParser.Completed += OnHeaderComplete; _headerParser.RequestLineParsed += OnRequestLine; _bodySlice = _stack.Pop(); }
/// <summary> /// Initializes a new instance of the <see cref="SendSlice"/> class. /// </summary> /// <param name="slice">The slice.</param> /// <param name="length">Number of bytes written to the slice.</param> public SendSlice(IBufferSlice slice, int length) { Length = length; if (slice == null) throw new ArgumentNullException("slice"); Slice = slice; }
/// <summary> /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// </summary> /// <filterpriority>2</filterpriority> public void Dispose() { var disposable = _slice as IDisposable; if (disposable != null) { disposable.Dispose(); _slice = null; } }
/// <summary> /// Initializes a new instance of the <see cref="SendSlice"/> class. /// </summary> /// <param name="slice">The slice.</param> public SendSlice(IBufferSlice slice) { if (slice == null) { throw new ArgumentNullException("slice"); } BufferSlice = slice; }
/// <summary> /// Initializes a new instance of the <see cref="SendSlice"/> class. /// </summary> /// <param name="slice">The slice.</param> /// <param name="length">Number of bytes written to the slice.</param> public SendSlice(IBufferSlice slice, int length) { Length = length; if (slice == null) { throw new ArgumentNullException("slice"); } Slice = slice; }
/// <summary> /// Initializes a new instance of the <see cref="SliceStream" /> class. /// </summary> /// <param name="slice">The slice to use.</param> /// <exception cref="System.ArgumentNullException">slice</exception> public SliceStream(IBufferSlice slice) { if (slice == null) { throw new ArgumentNullException("slice"); } this.slice = slice; this.offset = 0; this.length = 0; }
/// <summary> /// Initializes a new instance of the <see cref="MicroMessageEncoder" /> class. /// </summary> /// <param name="serializer"> /// Serializer used to serialize the messages that should be sent. You might want to pick a /// serializer which is reasonable fast. /// </param> public MicroMessageEncoder(IMessageSerializer serializer) { if (serializer == null) { throw new ArgumentNullException("serializer"); } _serializer = serializer; _bufferSlice = new BufferSlice(new byte[65535], 0, 65535); }
/// <summary> /// Initializes a new instance of the <see cref="MessagingClientContext" /> class. /// </summary> /// <param name="readBuffer">The read buffer.</param> /// <param name="formatterFactory">Used to format messages </param> public MessagingClientContext(IBufferSlice readBuffer, IMessageFormatterFactory formatterFactory) : base(readBuffer) { if (formatterFactory == null) { throw new ArgumentNullException("formatterFactory"); } this.formatterFactory = formatterFactory; this.messageBuilder = this.formatterFactory.CreateBuilder(); }
/// <summary> /// Initializes a new instance of the <see cref="ServerClientContext" /> class. /// </summary> /// <param name="readBuffer">The read buffer.</param> public ServerClientContext(IBufferSlice readBuffer) { if (readBuffer == null) throw new ArgumentNullException("readBuffer"); _readBuffer = readBuffer; _readStream = new SliceStream(ReadBuffer); _readArgs = new SocketAsyncEventArgs(); _readArgs.Completed += OnReadCompleted; _readArgs.SetBuffer(_readBuffer.Buffer, _readBuffer.Offset, _readBuffer.Count); _writer = new SocketWriter(); _writer.Disconnected += OnWriterDisconnect; }
/// <summary> /// Handles the read. /// </summary> /// <param name="slice">The slice.</param> /// <param name="bytesRead">The bytes read.</param> protected override void HandleRead(IBufferSlice slice, int bytesRead) { if (this.messageBuilder.Append(new SliceStream(slice, bytesRead))) { object message; while (this.messageBuilder.TryDequeue(out message)) { this.TriggerClientReceive(message); } } }
/// <summary> /// Initializes a new instance of the <see cref="SliceStream" /> class. /// </summary> /// <param name="slice">The slice.</param> /// <param name="length">Number of bytes which have already been written to the buffer.</param> /// <exception cref="System.ArgumentNullException">slice</exception> /// <exception cref="System.ArgumentOutOfRangeException">length;Must be less or equal to slice length which is + slice.Count</exception> public SliceStream(IBufferSlice slice, int length) { if (slice == null) throw new ArgumentNullException("slice"); if (length > slice.Count) throw new ArgumentOutOfRangeException("length", length, "Must be less or equal to slice length which is " + slice.Count); _slice = slice; _offset = 0; _length = length; }
/// <summary> /// Gives back a slice. /// </summary> /// <param name="slice">The slice.</param> /// <exception cref="System.InvalidOperationException">We did not give you away, hence we can't take you. Find your real stack.</exception> void IBufferSliceStack.Push(IBufferSlice slice) { if (slice == null) throw new ArgumentNullException("slice"); var mySlice = slice as PooledBufferSlice; if (mySlice == null || !mySlice.IsMyStack(this)) throw new InvalidOperationException( "We did not give you away, hence we can't take you. Find your real stack."); mySlice.Reset(); _slices.Push(mySlice); }
/// <summary> /// Handles the read. /// </summary> /// <param name="slice">The slice.</param> /// <param name="bytesRead">The bytes read.</param> protected override void HandleRead(IBufferSlice slice, int bytesRead) { if (_messageBuilder.Append(new SliceStream(slice, bytesRead))) { object message; while (_messageBuilder.TryDequeue(out message)) { TriggerClientReceive(message); } } }
/// <summary> /// Initializes a new instance of the <see cref="MicroMessageEncoder" /> class. /// </summary> /// <param name="serializer">Serialiser used to serialize the messages that should be sent. You might want to pick a serializer which is reasonable fast.</param> /// <param name="bufferSlice">Used when sending information.</param> /// <exception cref="ArgumentOutOfRangeException">bufferSlice; At least the header should fit in the buffer, and the header can be up to 520 bytes in the current version.</exception> public MicroMessageEncoder(IMessageSerializer serializer, IBufferSlice bufferSlice) { if (serializer == null) throw new ArgumentNullException("serializer"); if (bufferSlice == null) throw new ArgumentNullException("bufferSlice"); if (bufferSlice.Capacity < 520) throw new ArgumentOutOfRangeException("bufferSlice", bufferSlice.Capacity, "At least the header should fit in the buffer, and the header can be up to 520 bytes in the current version"); _serializer = serializer; _bufferSlice = bufferSlice; }
/// <summary> /// Dipose last buffer and reset indexes. /// </summary> public void Reset() { var disposable = this.slice as IDisposable; if (disposable != null) { disposable.Dispose(); } this.slice = null; this.Count = 0; this.Position = 0; }
/// <summary> /// Dipose last buffer and reset indexes. /// </summary> public void Reset() { var disposable = _slice as IDisposable; if (disposable != null) { disposable.Dispose(); } _slice = null; Count = 0; Position = 0; }
/// <summary> /// We've received something from the other end /// </summary> /// <param name="buffer">Buffer containing the received bytes</param> /// <param name="bytesRead">Amount of bytes that we received</param> /// <remarks>You have to handle all bytes, anything left will be discarded.</remarks> protected override void OnReceived(IBufferSlice buffer, int bytesRead) { var gotMessage = this.messageBuilder.Append(new SliceStream(buffer, bytesRead)); if (gotMessage) { object message; while (this.messageBuilder.TryDequeue(out message)) { this.Received(this, new ReceivedMessageEventArgs(message)); } } }
/// <summary> /// Initializes a new instance of the <see cref="ServiceExceptionContext" /> class. /// </summary> /// <param name="exception">The exception that was caught.</param> /// <param name="bufferSlice">The buffer slice that caused the exception (directly = protocol failure, indirectly = the handling failed).</param> public ServiceExceptionContext(Exception exception, IBufferSlice bufferSlice) { if (exception == null) { throw new ArgumentNullException("exception"); } if (bufferSlice == null) { throw new ArgumentNullException("bufferSlice"); } BufferSlice = bufferSlice; Exception = exception; }
/// <summary> /// We've received something from the other end /// </summary> /// <param name="buffer">Buffer containing the received bytes</param> /// <param name="bytesRead">Amount of bytes that we received</param> /// <remarks>You have to handle all bytes, anything left will be discarded.</remarks> protected override void OnReceived(IBufferSlice buffer, int bytesRead) { var gotMessage = _messageBuilder.Append(new SliceStream(buffer, bytesRead)); if (gotMessage) { object message; while (_messageBuilder.TryDequeue(out message)) { Received(this, new ReceivedMessageEventArgs(message)); } } }
/// <summary> /// Initializes a new instance of the <see cref="ServerClientContext" /> class. /// </summary> /// <param name="readBuffer">The read buffer.</param> public ServerClientContext(IBufferSlice readBuffer) { if (readBuffer == null) { throw new ArgumentNullException("readBuffer"); } this.readBuffer = readBuffer; this.readStream = new SliceStream(this.ReadBuffer); this.readArgs = new SocketAsyncEventArgs(); this.readArgs.Completed += this.OnReadCompleted; this.readArgs.SetBuffer(this.readBuffer.Buffer, this.readBuffer.Offset, this.readBuffer.Count); this.writer = new SocketWriter(); this.writer.Disconnected += this.OnWriterDisconnect; }
/// <summary> /// Assign a new slice to this writer /// </summary> /// <param name="slice">Slice to assign</param> /// <remarks>You must have called <see cref="Reset"/> first to release last buffer.</remarks> public void Assign(IBufferSlice slice) { if (slice == null) { throw new ArgumentNullException("slice"); } if (this.slice != null) { throw new InvalidOperationException("You must reset the writer before assigning a new buffer."); } this.Count = slice.Count; this.Position = slice.Offset; this.slice = slice; }
/// <summary> /// </summary> /// <param name="readBuffer"></param> /// <param name="encoder"></param> /// <param name="decoder"></param> /// <param name="sslStreamBuilder">Used to wrap the socket with a SSL stream</param> public SecureTcpChannel(IBufferSlice readBuffer, IMessageEncoder encoder, IMessageDecoder decoder, ISslStreamBuilder sslStreamBuilder) { if (readBuffer == null) throw new ArgumentNullException("readBuffer"); if (encoder == null) throw new ArgumentNullException("encoder"); if (decoder == null) throw new ArgumentNullException("decoder"); if (sslStreamBuilder == null) throw new ArgumentNullException("sslStreamBuilder"); _encoder = encoder; _decoder = decoder; _sslStreamBuilder = sslStreamBuilder; _decoder.MessageReceived = OnMessageReceived; _sendCompleteAction = (channel, message) => { }; _disconnectAction = (channel, exception) => { }; ChannelFailure = (channel, error) => HandleDisconnect(SocketError.ProtocolNotSupported); RemoteEndpoint = EmptyEndpoint.Instance; _readBuffer = new SocketBuffer(readBuffer); _writeBuffer = new SocketBuffer(); ChannelId = GuidFactory.Create().ToString(); Data = new ChannelData(); }
/// <summary> /// Create a new channel /// </summary> /// <param name="readBuffer">Buffer which should be used when reading from the socket</param> /// <param name="encoder">Used to encode outgoing data</param> /// <param name="decoder">Used to decode incoming data</param> /// <returns>Created channel</returns> public ITcpChannel Create(IBufferSlice readBuffer, IMessageEncoder encoder, IMessageDecoder decoder) { return new TcpChannel(readBuffer, encoder, decoder); }
/// <summary> /// Creates <see cref="MessagingClientContext"/> /// </summary> /// <param name="readBuffer">Read buffer</param> /// <returns>Created client</returns> protected override ServerClientContext CreateClientContext(IBufferSlice readBuffer) { if (readBuffer == null) throw new ArgumentNullException("readBuffer"); return new MessagingClientContext(readBuffer, _formatterFactory); }
/// <summary> /// Create a new client context /// </summary> /// <param name="readBuffer">The read buffer to use</param> /// <returns>Created context</returns> /// <remarks>The contexts are created at startup and then reused during the server lifetime. A context /// is assigned to a socket each time we've accepted one using the listener.</remarks> protected virtual ServerClientContext CreateClientContext(IBufferSlice readBuffer) { return new ServerClientContext(readBuffer); }
/// <summary> /// Handle incoming bytes /// </summary> /// <param name="readBuffer">Buffer containing received bytes</param> /// <param name="bytesReceived">Number of bytes that was recieved (will always be set, any errors have triggered <see cref="OnDisconnect"/> instead).</param> /// <remarks> /// <para>The default implementation will trigger the client with a <see cref="IBufferReader"/> as message. That means that /// you should not call the base method from your code.</para> /// </remarks> protected virtual void HandleRead(IBufferSlice readBuffer, int bytesReceived) { _client.HandleReceive(_readStream); }
/// <summary> /// Initializes a new instance of the <see cref="MessagingClientContext" /> class. /// </summary> /// <param name="readBuffer">The read buffer.</param> /// <param name="formatterFactory">Used to format messages </param> public MessagingClientContext(IBufferSlice readBuffer, IMessageFormatterFactory formatterFactory) : base(readBuffer) { _formatterFactory = formatterFactory; _messageBuilder = _formatterFactory.CreateBuilder(); }
/// <summary> /// Set buffer which can be used for the currently active write operation. /// </summary> /// <param name="bufferSlice">Slice</param> public void SetWriteBuffer(IBufferSlice bufferSlice) { if (bufferSlice == null) throw new ArgumentNullException("bufferSlice"); _writer.SetBuffer(bufferSlice); }
/// <summary> /// Dipose last buffer and reset indexes. /// </summary> public void Reset() { var disposable = _slice as IDisposable; if (disposable != null) disposable.Dispose(); _slice = null; Count = 0; Position = 0; }
/// <summary> /// Send information to the remote end point /// </summary> /// <param name="slice">Buffer slice</param> /// <param name="length">Number of bytes in the slice.</param> public void Send(IBufferSlice slice, int length) { if (slice == null) throw new ArgumentNullException("slice"); _writer.Send(new SliceSocketWriterJob(slice, length)); }
/// <summary> /// Initializes a new instance of the <see cref="BufferWriter" /> class. /// </summary> /// <param name="slice">The slice.</param> public BufferWriter(IBufferSlice slice) { if (slice == null) throw new ArgumentNullException("slice"); _slice = slice; }