Пример #1
0
 public FoldedPromise(MultiPromise <A> promise, I initial, Func <I, A, I> functor)
 {
     this.value   = initial;
     this.promise = promise;
     this.promise
     .SingleThen(item => this.value = functor(this.value, item))
     .Then(value => this.Resolve(this.value), ex => this.Reject(ex));
 }
Пример #2
0
        public IMultiPromise <byte[]> InputStream()
        {
            var watch = new Stopwatch();;

            watch.Start();

            var promise = new MultiPromise <byte[]>();

            CIOReactor.Spawn(this.name + "_inputStreamOf" + this.ConnectionHash, true, () =>
            {
                var sync = new ManualResetEvent(false);
                var run  = true;

                Log.Debug("{server} starting input stream for {hash}", this.name, this.ConnectionHash);
                while (run)
                {
                    sync.Reset();
                    this.readPacket().Then(value =>
                    {
                        if (value.Count() == 0)
                        {
                            run = false;
                            Log.Debug("{server} received empty response from {hash}, terminating.", this.name, this.ConnectionHash);
                            promise.Reject(new EmptyPacketException());
                        }
                        else
                        {
                            promise.SingleResolve(value);
                        }

                        sync.Set();
                    }).Catch(ex =>
                    {
                        Log.Debug("{server} readPacket() from {hash} rejected.", this.name, this.ConnectionHash);
                        run = false;
                        promise.Reject(ex);
                        sync.Set();
                    });

                    sync.WaitOne();
                }

                Log.Debug("{server} input stream for {hash} ended", this.name, this.ConnectionHash);
            }).Catch(ex =>
            {
                Log.Error("{server} input stream for {hash} crashed, rejecting its promise", this.name, this.ConnectionHash);
            });

            return(promise);
        }
Пример #3
0
 public MappedPromise(MultiPromise <A> promise, Func <A, B> functor, bool compact = false)
 {
     this.promise = promise;
     this.promise.SingleThen(value =>
     {
         var processedItem = functor(value);
         if (!compact || processedItem != null)
         {
             this.SingleResolve(processedItem);
         }
     })
     .Then(value =>
     {
         this.Resolve(default(B));
     })
     .Catch(ex => this.Reject(ex));
 }
Пример #4
0
        public IMultiPromise <CIOSocket> Incoming()
        {
            var promise = new MultiPromise <CIOSocket>();

            CIOReactor.Spawn(this.name + "_incomingConnections", true, () =>
            {
                Log.Information("{server} listening for connections", this.name);
                var sync = new ManualResetEvent(false);
                while (true)
                {
                    try
                    {
                        sync.Reset();

                        this.listener.BeginAccept(new AsyncCallback(result =>
                        {
                            try
                            {
                                var socket        = this.listener.EndAccept(result);
                                var wrappedSocket = new CIOSocket(this.name, socket);

                                Log.Debug("{server} accepted connection {hash} from {ip}", this.name, wrappedSocket.GetHashCode(), wrappedSocket.Endpoint);
                                promise.SingleResolve(wrappedSocket);
                            }
                            catch (Exception e)
                            {
                                Log.Error("{server} end accept caught: {exception}", this.name, e);
                            }

                            sync.Set();
                        }), null);

                        sync.WaitOne();
                    }
                    catch (Exception e)
                    {
                        Log.Error("{server} begin accept caught exception: {exception}", this.name, e);
                    }
                }
            });

            return(promise);
        }