public Server(Reactor.Http.Server server, string path) { this.path = path; this.server = server; if (this.server.OnContext == null) { this.server.OnContext = context => { context.Response.StatusCode = 401; context.Response.ContentType = "text/plain"; context.Response.Write("method not allowed"); context.Response.End(); }; } this.servercb = this.server.OnContext; this.server.OnContext = this.OnContext; this.OnUpgrade = (context, callback) => callback(true, string.Empty); }
internal static void ReadBytesAsync(Stream stream, int length, Reactor.Action <byte[]> completed, Reactor.Action <Exception> error) { var buffer = new byte[length]; stream.BeginRead(buffer, 0, length, result => { try { var len = stream.EndRead(result); var bytes = len < 1 ? new byte[0] : len < length ? Util.readBytes(stream, buffer, len, length - len) : buffer; if (completed != null) { completed(bytes); } } catch (Exception ex) { { if (error != null) { error(ex); } } } }, null); }
public static IDomain Create(string name, Reactor.Action callback) { var setup = new AppDomainSetup() { ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase, ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, ApplicationName = AppDomain.CurrentDomain.SetupInformation.ApplicationName, LoaderOptimization = LoaderOptimization.MultiDomainHost }; var appdomain = AppDomain.CreateDomain(name, null, setup); var domain = (Domain)appdomain.CreateInstanceAndUnwrap( typeof(Domain).Assembly.FullName, typeof(Domain).FullName); domain.Start(appdomain, callback); return(domain); }
public Server Listen(int port, Reactor.Action <Exception> callback) { try { this.httplistener = new Reactor.Net.HttpListener(); this.httplistener.Prefixes.Add(string.Format("http://*:{0}/", port)); this.httplistener.Start(); this.GetContext(); callback(null); } catch (Exception exception) { if (exception is HttpListenerException) { callback(exception); } else { callback(exception); } } return(this); }
public Socket(string filter) { this.filter = filter; this.handle = IntPtr.Zero; this.started = false; this.readbuffer = Marshal.AllocHGlobal((int)buffersize); // clean these up this.writebuffer = Marshal.AllocHGlobal((int)buffersize); this.addr = default(WINDIVERT_ADDRESS); this.onread = data => this.Write(data); this.onerror = error => { }; this.handle = WinDivert.WinDivertOpen(this.filter, WINDIVERT_LAYER.WINDIVERT_LAYER_NETWORK, 0, 0); if (handle == new IntPtr(-1)) { var exception = new Win32Exception(Marshal.GetLastWin32Error()); throw exception; } this.started = true; this.thread = new Thread(this.Runtime); this.thread.Start(); }
public Server() { this.socket = Reactor.Udp.Socket.Create(); this.onsocket = socket => { }; this.sockets = new Dictionary<System.Net.EndPoint, Reactor.Fusion.Socket>(); }
private void Start(AppDomain domain, Reactor.Action callback) { this.domain = domain; Reactor.Loop.Start(); Reactor.Loop.Post(callback); }
public Server() { this.socket = Reactor.Udp.Socket.Create(); this.onsocket = socket => { }; this.sockets = new Dictionary <System.Net.EndPoint, Reactor.Fusion.Socket>(); }
public static Server Create(Reactor.Action <Reactor.Fusion.Socket> callback) { var server = new Server(); server.onsocket = callback; return(server); }
public ThrottlePool(Reactor.Action <TRequest, Reactor.Action <Exception, TResponse> > process, int concurrency) { this.process = process; this.concurrency = concurrency; this.running = 0; this.workers = new Queue <Worker>(); }
public void Run(TRequest request, Reactor.Action <Exception, TResponse> callback) { var worker = new Worker(); worker.Request = request; worker.Callback = callback; this.workers.Enqueue(worker); this.Process(); }
/// <summary> /// (experimental) Protects this object against GC collection for a set period of time. /// </summary> /// <param name="obj">The object to pin.</param> /// <param name="timeout">The timeout (in milliseconds) in which this object should be pinned.</param> /// <param name="callback">The callback triggered when the timeout completes.</param> public static void Pin(object obj, int timeout, Reactor.Action callback) { var instance = GC.Instance; instance.PinObject(obj); Reactor.Timeout.Create(() => { instance.UnPinObject(obj); callback(); }, timeout); }
/// <summary> /// Runs a asynchronous operation in parallel. /// </summary> /// <typeparam name="TInput">The methods input type</typeparam> /// <typeparam name="TOutput">The methods output type</typeparam> /// <param name="action">The method or action encapsulating the async operation</param> /// <param name="inputs">The method or action input array.</param> /// <param name="callback">The callback to receive async results</param> public static void Parallel <TInput, TOutput>(Reactor.Action <TInput, Reactor.Action <Exception, TOutput> > action, IEnumerable <TInput> inputs, Reactor.Action <IEnumerable <AsyncResult <TOutput> > > callback) { var count = 0; foreach (var item in inputs) { count++; } if (count == 0) { callback(new List <AsyncResult <TOutput> >()); return; } Reactor.Action <int, TInput, Reactor.Action <int, Exception, TOutput> > container = null; container = (index, input, _callback) => { action(input, (exception, output) => { _callback(index, exception, output); }); }; var outputs = new AsyncResult <TOutput> [count]; var completed = 0; var index_in = 0; foreach (var input in inputs) { container(index_in, input, (index_out, exception, output) => { outputs[index_out] = new AsyncResult <TOutput>(exception, output); completed++; if (completed == count) { callback(outputs); } }); index_in++; } }
private void Upgrade(Reactor.Http.HttpContext context, Reactor.Action <Exception, Reactor.Web.Socket.Socket> callback) { var request = ServerWebSocketUpgradeRequest.Create(context); //-------------------------------------------------------- // if not a web socket attempt, defer to http callback. //-------------------------------------------------------- if (request == null) { this.servercb(context); return; } var response = ServerWebSocketUpgradeResponse.Create(request); var socket_context = new Reactor.Web.Socket.Context(context); this.OnUpgrade(socket_context, (success, reason) => { if (!success) { response.Reject(reason == null ? "" : reason, (exception) => callback(exception, null)); return; } response.Accept((exception) => { if (exception != null) { callback(exception, null); return; } var channel = new Transport(context.Connection); var socket = new Socket(channel); socket.Context = socket_context; callback(null, socket); }); }); }
public void Handler(Reactor.Web.Context context, Reactor.Action next) { Reactor.Web.MiddlewareProcessor.Process(context, this.middleware, () => { foreach (var route in this.routes) { if (route.Match(context.Request)) { context.Params = route.ComputeParams(context.Request); route.Invoke(context); return; } } next(); }); }
public Server(Reactor.Http.Server httpserver) { this.router = new Router(); this.httpserver = httpserver; this.servercb = this.httpserver.OnContext; this.httpserver.OnContext = this.OnHttpContext; this.httpserver.OnError += (error) => { if (this.OnError != null) { this.OnError(error); } }; }
internal Socket(Reactor.Web.Socket.Transport transport) { this.transport = transport; this.State = SocketState.Open; this.transport.OnOpen = () => { if (this.OnOpen != null) { this.OnOpen(); } }; this.transport.OnError = (exception) => { if (this.OnError != null) { this.OnError(exception); } }; this.transport.OnClose = () => { this.State = SocketState.Closed; this.Close(); if (this.OnClose != null) { this.OnClose(); } }; this.transport.OnMessage = (message) => { if (this.OnMessage != null) { this.OnMessage(message); } }; }
public Server(int port, string path) { this.path = path; this.server = Reactor.Http.Server.Create(context => { context.Response.StatusCode = 401; context.Response.ContentType = "text/plain"; context.Response.Write("method not allowed"); context.Response.End(); }).Listen(port); this.servercb = this.server.OnContext; this.server.OnContext = this.OnContext; this.OnUpgrade = (context, callback) => callback(true, string.Empty); }
/// <summary> /// Runs a asynchronous operation in series. /// </summary> /// <typeparam name="TInput">The methods input type</typeparam> /// <typeparam name="TOutput">The methods output type</typeparam> /// <param name="action">The method or action encapsulating the async operation</param> /// <param name="inputs">The method or action input array.</param> /// <param name="callback">The callback to receive async results</param> public static void Series <TInput, TOutput>(Reactor.Action <TInput, Reactor.Action <Exception, TOutput> > action, IEnumerable <TInput> inputs, Reactor.Action <IEnumerable <AsyncResult <TOutput> > > callback) { var queue = new Queue <TInput>(); var list = new List <AsyncResult <TOutput> >(); foreach (var input in inputs) { queue.Enqueue(input); } if (queue.Count == 0) { callback(new List <AsyncResult <TOutput> >()); return; } Reactor.Action container = null; container = () => { var input = queue.Dequeue(); action(input, (exception, output) => { list.Add(new AsyncResult <TOutput>(exception, output)); if (queue.Count > 0) { container(); return; } callback(list); }); }; container(); }
public static void Process(Reactor.Web.Context context, List <Middleware> middlware, Action next) { if (middlware == null) { next(); return; } if (middlware.Count == 0) { next(); return; } int index = 0; Reactor.Action action = null; action = () => { if (index < middlware.Count) { middlware[index](context, () => { index++; action(); }); } else { next(); } }; action(); }
public Router Options(string pattern, Reactor.Web.Middleware[] middleware, Reactor.Action <Context> handler) { return(this.router.Options(pattern, middleware, handler)); }
public Server(Reactor.Http.Server server, string path) { this.path = path; this.server = server; if(this.server.OnContext == null) { this.server.OnContext = context => { context.Response.StatusCode = 401; context.Response.ContentType = "text/plain"; context.Response.Write("method not allowed"); context.Response.End(); }; } this.servercb = this.server.OnContext; this.server.OnContext = this.OnContext; this.OnUpgrade = (context, callback) => callback(true, string.Empty); }
/// <summary> /// Creates a async throttle. Limits the amount of concurrent async operations. /// </summary> /// <typeparam name="TRequest">The Request Type</typeparam> /// <typeparam name="TResponse">The Response Type</typeparam> /// <param name="action">The action or method.</param> /// <param name="concurrency">The maximum allowed number of concurrent operations.</param> /// <returns>A delegate to the pool.</returns> public static Reactor.Action <TRequest, Reactor.Action <Exception, TResponse> > Throttle <TRequest, TResponse>(Reactor.Action <TRequest, Reactor.Action <Exception, TResponse> > action, int concurrency) { var taskpool = new ThrottlePool <TRequest, TResponse>(action, concurrency); return(taskpool.Run); }
public Router Delete(string pattern, Reactor.Action <Context> handler) { return(this.router.Delete(pattern, handler)); }
internal Socket(string url, Dictionary <string, string> Headers) { var request = WebSocketRequest.Create(url); request.Headers = Headers; request.GetResponse((exception, response) => { //--------------------------------------- // check for handshake error //--------------------------------------- if (exception != null) { if (this.OnError != null) { this.OnError(exception); } return; } //--------------------------------------- // check for non upgrade errors //--------------------------------------- if (response.StatusCode != 101) { if (this.OnError != null) { this.OnError(new Exception("server rejected connection")); } return; } //--------------------------------------- // configure events //--------------------------------------- this.transport = new Transport(response.Socket); //--------------------------------------- // emit open //--------------------------------------- if (this.OnOpen != null) { this.OnOpen(); } this.transport.OnError += (error) => { if (this.OnError != null) { this.OnError(error); } }; this.transport.OnClose += () => { this.State = SocketState.Closed; if (this.OnClose != null) { this.OnClose(); } }; this.transport.OnMessage += (message) => { if (this.OnMessage != null) { this.OnMessage(message); } }; //-------------------------------------------- // accept any frames passed on the response. //-------------------------------------------- foreach (var frame in response.Frames) { this.transport.AcceptFrame(frame); } }); }
public Router Delete(string pattern, Reactor.Action <Context> handler) { this.routes.Add(new Route(pattern, "DELETE", handler)); return(this); }
public Router Put(string pattern, Reactor.Action <Context> handler) { this.routes.Add(new Route(pattern, "PUT", handler)); return(this); }
/// <summary> /// Creates dynamic delegate. /// </summary> /// <param name="types">The type arguments (typically parsed as a generic argument list)</param> /// <param name="callback">The callback to receive results</param> /// <returns>A Delegate</returns> public static Delegate Create(Type[] types, Reactor.Action <object[]> callback) { var d = new DynamicAction(types, callback); return(d.Delegate); }
public void Post(Reactor.Action message) { this.actions.Enqueue(message); }
public Router Options(string pattern, Reactor.Web.Middleware[] middleware, Reactor.Action <Context> handler) { this.routes.Add(new Route(pattern, "OPTIONS", middleware, handler)); return(this); }
public DynamicAction(Type[] types, Reactor.Action <object[]> callback) { this.callback = callback; if (types.Length == 0) { Type actionType = typeof(Reactor.Action); var methodinfo = this.GetType().GetMethod("Handler0", BindingFlags.NonPublic | BindingFlags.Instance); this.Delegate = Delegate.CreateDelegate(actionType, this, methodinfo); } if (types.Length == 1) { Type actionType = typeof(Reactor.Action <>); Type genericActionType = actionType.MakeGenericType(new Type[] { types[0] }); var methodinfo = this.GetType().GetMethod("Handler1", BindingFlags.NonPublic | BindingFlags.Instance); var genericMethodInfo = methodinfo.MakeGenericMethod(new Type[] { types[0] }); this.Delegate = Delegate.CreateDelegate(genericActionType, this, genericMethodInfo); } if (types.Length == 2) { Type actionType = typeof(Reactor.Action <,>); Type genericActionType = actionType.MakeGenericType(new Type[] { types[0], types[1] }); var methodinfo = this.GetType().GetMethod("Handler2", BindingFlags.NonPublic | BindingFlags.Instance); var genericMethodInfo = methodinfo.MakeGenericMethod(new Type[] { types[0], types[1] }); this.Delegate = Delegate.CreateDelegate(genericActionType, this, genericMethodInfo); } if (types.Length == 3) { Type actionType = typeof(Reactor.Action <, ,>); Type genericActionType = actionType.MakeGenericType(new Type[] { types[0], types[1], types[2] }); var methodinfo = this.GetType().GetMethod("Handler3", BindingFlags.NonPublic | BindingFlags.Instance); var genericMethodInfo = methodinfo.MakeGenericMethod(new Type[] { types[0], types[1], types[2] }); this.Delegate = Delegate.CreateDelegate(genericActionType, this, genericMethodInfo); } if (types.Length == 4) { Type actionType = typeof(Reactor.Action <, , ,>); Type genericActionType = actionType.MakeGenericType(new Type[] { types[0], types[1], types[2], types[3] }); var methodinfo = this.GetType().GetMethod("Handler4", BindingFlags.NonPublic | BindingFlags.Instance); var genericMethodInfo = methodinfo.MakeGenericMethod(new Type[] { types[0], types[1], types[2], types[3] }); this.Delegate = Delegate.CreateDelegate(genericActionType, this, genericMethodInfo); } }
public Router Post(string pattern, Reactor.Action <Context> handler) { return(this.router.Post(pattern, handler)); }