Coder Social home page Coder Social logo

dshe / rxsockets Goto Github PK

View Code? Open in Web Editor NEW
19.0 8.0 6.0 384 KB

A minimal reactive / async streams socket implementation.

License: Apache License 2.0

C# 100.00%
socket reactive reactive-extensions observable-socket reactive-socket network sockets observable rxsockets rxsocket rx async-enumerable async-streams

rxsockets's Introduction

RxSockets   Build status NuGet NuGet License Ukraine

Minimal Reactive / Async Streams Socket Implementation

  • .NET 6.0 library
  • connect: asynchronous
  • send: synchronous
  • receive: async enumerable or observable
  • accept: async enumerable or observable
  • simple and intuitive API
  • dependencies: System.Reactive, Microsoft.Extensions.Logging

installation

PM> Install-Package RxSockets

example

using System;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
using Xunit;
using RxSockets;

server

interface IRxSocketServer : IAsyncDisposable
{
    EndPoint LocalEndPoint { get; }
    IAsyncEnumerable<IRxSocketClient> AcceptAllAsync { get; }
}
// Create a server using an available port on the local machine.
IRxSocketServer server = RxSocketServer.Create();

// Prepare to start accepting connections from clients.
server
    .AcceptAllAsync
    .ToObservableFromAsyncEnumerable()
    .Subscribe(onNext: acceptClient =>
    {
        // After the server accepts a client connection,
        // start receiving messages from the client and ...
        acceptClient
            .ReceiveAllAsync
            .ToObservableFromAsyncEnumerable()
            .ToStrings()
            .Subscribe(onNext: message =>
            {
                // Echo each message received back to the client.
                acceptClient.Send(message.ToByteArray());
            });
    });

client

interface IRxSocketClient : IAsyncDisposable
{
    EndPoint RemoteEndPoint { get; }
    bool Connected { get; }
    int Send(ReadOnlySpan<byte> buffer);
    IAsyncEnumerable<byte> ReceiveAllAsync { get; }
}
// Create a client connected to EndPoint of the server.
IRxSocketClient client = await server.LocalEndPoint.CreateRxSocketClientAsync();

// Send the message "Hello!" to the server,
// which the server will then echo back to the client.
client.Send("Hello!".ToByteArray());

// Receive the message from the server.
string message = await client.ReceiveAllAsync.ToStrings().FirstAsync();
Assert.Equal("Hello!", message);

await client.DisposeAsync();
await server.DisposeAsync();

notes

IObservable<T> ToObservableFromAsyncEnumerable<T>(this IAsyncEnumerable<T> source)

The extension method ToObservableFromAsyncEnumerable() may be used to create observables from the async enumerables IRxSocketClient.ReceiveAllAsync and IRxSocketServer.AcceptAllAsync.

Observable.Publish()[.RefCount() | .AutoConnect()] may be used to support multiple simultaneous observers.

To communicate using strings (see example above), the following extension methods are provided:

byte[] ToByteArray(this string source);
byte[] ToByteArray(this IEnumerable<string> source)

IEnumerable<string>      ToStrings(this IEnumerable<byte> source)
IAsyncEnumerable<string> ToStrings(this IAsyncEnumerable<byte> source)
IObservable<string>      ToStrings(this IObservable<byte> source)

To communicate using byte arrays with a 4 byte BigEndian integer length prefix, the following extension methods are provided:

byte[] ToByteArrayWithLengthPrefix(this byte[] source)

IEnumerable<byte[]>      ToArraysFromBytesWithLengthPrefix(this IEnumerable<byte> source)
IAsyncEnumerable<byte[]> ToArraysFromBytesWithLengthPrefix(this IAsyncEnumerable<byte> source)
IObservable<byte[]>      ToArraysFromBytesWithLengthPrefix(this IObservable<byte> source)

rxsockets's People

Contributors

dshe avatar kaw0 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rxsockets's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.