コード例 #1
0
ファイル: CIOSocket.cs プロジェクト: slavig/pepperspray
        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);
        }
コード例 #2
0
ファイル: Listener.cs プロジェクト: slavig/pepperspray
        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);
        }