GithubHelp home page GithubHelp logo

asyncio's Introduction

AsyncIO

AsyncIO is portable high performance sockets library for .Net. The library is based on Windows IO Completion ports.

.Net Socket library doesn't give control over the threads and doesn't expose the IO completion port API, AsyncIO give full control over the threads and allow the developer to create high performance servers.

On Mono the library fall down to mono implementation but still give completion port like API.

Installation

You can install AsyncIO from nuget.

Using

Using AsyncIO is very similiar to using .Net Socket, to get the completion event of the operation you need to call GetQueuedCompletionStatus method of the completion port.

    static void Main(string[] args)
    {
        CompletionPort completionPort = CompletionPort.Create();

        AutoResetEvent listenerEvent = new AutoResetEvent(false);
        AutoResetEvent clientEvent = new AutoResetEvent(false);
        AutoResetEvent serverEvent = new AutoResetEvent(false);

        AsyncSocket listener = AsyncSocket.Create(AddressFamily.InterNetwork, 
            SocketType.Stream, ProtocolType.Tcp);
        completionPort.AssociateSocket(listener, listenerEvent);

        AsyncSocket server = AsyncSocket.Create(AddressFamily.InterNetwork, 
            SocketType.Stream, ProtocolType.Tcp);
        completionPort.AssociateSocket(server, serverEvent);

        AsyncSocket client = AsyncSocket.Create(AddressFamily.InterNetwork, 
            SocketType.Stream, ProtocolType.Tcp);
        completionPort.AssociateSocket(client, clientEvent);

        Task.Factory.StartNew(() =>
        {
            CompletionStatus completionStatus;

            while (true)
            {
                var result = completionPort.GetQueuedCompletionStatus(-1, out completionStatus);

                if (result)
                {
                    Console.WriteLine("{0} {1} {2}", completionStatus.SocketError, 
                        completionStatus.OperationType, completionStatus.BytesTransferred);

                    if (completionStatus.State != null)
                    {
                        AutoResetEvent resetEvent = (AutoResetEvent)completionStatus.State;
                        resetEvent.Set();
                    }
                }
            }
        });

        listener.Bind(IPAddress.Any, 5555);
        listener.Listen(1);

        client.Connect("localhost", 5555);

        listener.Accept(server);


        listenerEvent.WaitOne();
        clientEvent.WaitOne();

        byte[] sendBuffer = new byte[1] { 2 };
        byte[] recvBuffer = new byte[1];

        client.Send(sendBuffer);
        server.Receive(recvBuffer);

        clientEvent.WaitOne();
        serverEvent.WaitOne();

        server.Dispose();
        client.Dispose();
    }

asyncio's People

Contributors

masaeedu avatar migelu avatar romanros avatar somdoron avatar

Watchers

 avatar  avatar

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.