Example #1
0
        static IEnumerable<object> ProcessSocketInternal(this ISocket socket, IHttpSupport http, OwinApplication application)
        {
            var beginRequest = http.BeginRequest(socket);
            yield return beginRequest;

            var request = beginRequest.Result;

            var invoke = new ContinuationState<Tuple<string, IDictionary<string, IList<string>>, IEnumerable<object>>>
                ((r, e) =>
                    application(request,
                                (s,h,b) =>
                                    r(new Tuple<string,IDictionary<string,IList<string>>,IEnumerable<object>>(s, h, b)),
                                e));

            yield return invoke;

            var response = invoke.Result;

            yield return http.BeginResponse(socket, response.Item1, response.Item2);

            foreach (var obj in response.Item3)
            {
                var objectToWrite = obj;

                if (obj is Action<Action<object>, Action<Exception>>)
                {
                    var cs = new ContinuationState<object>(obj as Action<Action<object>, Action<Exception>>);

                    yield return cs;

                    objectToWrite = cs.Result;
                }

                if (objectToWrite is FileInfo)
                {
                    yield return new ContinuationState(socket.WriteFile((objectToWrite as FileInfo).Name));
                    continue;
                }

                var chunk = default(ArraySegment<byte>);

                if (obj is ArraySegment<byte>)
                    chunk = (ArraySegment<byte>)obj;
                else if (obj is byte[])
                    chunk = new ArraySegment<byte>(obj as byte[]);
                else
                    continue;
                    //throw new ArgumentException("Invalid object of type " + obj.GetType() + " '" + obj.ToString() + "'");

                var write = socket.WriteChunk(chunk);
                yield return write;

                // TODO enumerate to completion
                if (write.Exception != null)
                    throw write.Exception;
            }

            socket.Dispose();
        }
Example #2
0
 public static void Host(this IKayakServer server, IHttpSupport http, OwinApplication application, Action<Action> trampoline)
 {
     server.HostInternal(http, application, trampoline).AsContinuation<object>(trampoline)
         (_ => { }, e => {
             Console.WriteLine("Error while hosting application.");
             Console.Out.WriteException(e);
         });
 }
Example #3
0
 public static void ProcessSocket(this ISocket socket, IHttpSupport http, OwinApplication application, Action<Action> trampoline)
 {
     socket.ProcessSocketInternal(http, application).AsContinuation<object>(trampoline)
         (_ => { }, e =>
         {
             Console.WriteLine("Error while processing request.");
             Console.Out.WriteException(e);
         });
 }
Example #4
0
 public static void ProcessSocket(this ISocket socket, IHttpSupport http, OwinApplication application, Action <Action> trampoline)
 {
     socket.ProcessSocketInternal(http, application).AsContinuation <object>(trampoline)
         (_ => { }, e =>
     {
         Console.WriteLine("Error while processing request.");
         Console.Out.WriteException(e);
     });
 }
Example #5
0
        static IEnumerable<object> HostInternal(this IKayakServer server, IHttpSupport http, OwinApplication application, Action<Action> trampoline)
        {
            while (true)
            {
                var accept = new ContinuationState<ISocket>((r, e) => server.GetConnection()(r));
                yield return accept;

                if (accept.Result == null)
                    break;

                accept.Result.ProcessSocket(http, application, trampoline);
            }
        }
Example #6
0
 public static IDisposable Invoke(this IObservable<ISocket> sockets, IApplication application, IHttpSupport http)
 {
     return sockets.Subscribe(s =>
         {
             s.Invoke(application, http).ContinueWith(t =>
             {
                 if (t.IsFaulted)
                 {
                     Console.WriteLine("Error while processing request.");
                     Console.Out.WriteException(t.Exception);
                 }
             });
         });
 }
Example #7
0
        static IEnumerable<object> ProcessSocketInternal(this ISocket socket, IHttpSupport http, OwinApplication application)
        {
            var beginRequest = http.BeginRequest(socket);
            yield return beginRequest;

            var request = beginRequest.Result;

            var invoke = new ContinuationState<Tuple<string, IDictionary<string, IEnumerable<string>>, IEnumerable<object>>>
                ((r, e) => application(request, r, e));

            yield return invoke;

            var response = invoke.Result;

            yield return http.BeginResponse(socket, response.Item1, response.Item2);

            foreach (var obj in response.Item3)
            {
                var objectToWrite = obj;

                if (obj is Action<Action<object>, Action<Exception>>)
                {
                    var cs = new ContinuationState<object>(obj as Action<Action<object>, Action<Exception>>);

                    yield return cs;

                    objectToWrite = cs.Result;
                }

                if (objectToWrite is FileInfo)
                {
                    yield return new ContinuationState(socket.WriteFile((objectToWrite as FileInfo).Name));
                }
                var write = socket.WriteFileOrData(objectToWrite);
                yield return write;

                if (write.Exception != null)
                    throw write.Exception;
            }

            // HTTP/1.1 support might only close the connection if client wants to
            //Trace.Write("Closed connection.");
            socket.Dispose();
        }
Example #8
0
        static IEnumerable<object> InvokeInternal(this ISocket socket, IApplication application, IHttpSupport http)
        {
            var beginRequest = http.BeginRequest(socket);
            yield return beginRequest;

            if (beginRequest.IsFaulted)
                throw beginRequest.Exception;

            IRequest request = beginRequest.Result;

            var invoke = application.InvokeAsync(request);

            yield return invoke;

            if (invoke.IsFaulted)
                throw invoke.Exception;

            IResponse response = invoke.Result;

            yield return http.BeginResponse(socket, response);

            foreach (var obj in response.GetBody())
            {
                var objectToWrite = obj;

                if (obj is Task)
                {
                    var task = (obj as Task).AsTaskWithValue();

                    yield return task;

                    if (task.IsFaulted)
                        throw task.Exception;

                    objectToWrite = task.Result;
                }

                socket.WriteObject(objectToWrite);
            }

            // HTTP/1.1 support might only close the connection if client wants to
            //Trace.Write("Closed connection.");
            socket.Dispose();
        }
Example #9
0
        static IEnumerable <object> ProcessSocketInternal(this ISocket socket, IHttpSupport http, OwinApplication application)
        {
            var beginRequest = http.BeginRequest(socket);

            yield return(beginRequest);

            var request = beginRequest.Result;

            var invoke = new ContinuationState <Tuple <string, IDictionary <string, IList <string> >, IEnumerable <object> > >
                             ((r, e) =>
                             application(request,
                                         (s, h, b) =>
                                         r(new Tuple <string, IDictionary <string, IList <string> >, IEnumerable <object> >(s, h, b)),
                                         e));

            yield return(invoke);

            var response = invoke.Result;

            yield return(http.BeginResponse(socket, response.Item1, response.Item2));

            foreach (var obj in response.Item3)
            {
                var objectToWrite = obj;

                if (obj is Action <Action <object>, Action <Exception> > )
                {
                    var cs = new ContinuationState <object>(obj as Action <Action <object>, Action <Exception> >);

                    yield return(cs);

                    objectToWrite = cs.Result;
                }

                if (objectToWrite is FileInfo)
                {
                    yield return(new ContinuationState(socket.WriteFile((objectToWrite as FileInfo).Name)));

                    continue;
                }

                var chunk = default(ArraySegment <byte>);

                if (objectToWrite is ArraySegment <byte> )
                {
                    chunk = (ArraySegment <byte>)objectToWrite;
                }
                else if (objectToWrite is byte[])
                {
                    chunk = new ArraySegment <byte>(objectToWrite as byte[]);
                }
                else
                {
                    continue;
                }
                //throw new ArgumentException("Invalid object of type " + obj.GetType() + " '" + obj.ToString() + "'");

                var write = socket.WriteChunk(chunk);
                yield return(write);

                // TODO enumerate to completion
                if (write.Exception != null)
                {
                    throw write.Exception;
                }
            }

            socket.Dispose();
        }
Example #10
0
 public static void ProcessSocket(this ISocket socket, IHttpSupport http, OwinApplication application, Action <Action> trampoline)
 {
     socket.ProcessSocketInternal(http, application).AsContinuation <object>(trampoline)
         (_ => { }, e => logger.ErrorException("ProcessSocket", e));
 }
Example #11
0
 public static Task Invoke(this ISocket socket, IApplication application, IHttpSupport http)
 {
     return socket.InvokeInternal(application, http).AsCoroutine<Unit>();
 }