Beispiel #1
0
 private static void ProcessConnected(object sender, SocketAsyncEventArgs e)
 {
     using (e)
     {
         try
         {
             SocketContext context = e.UserToken as SocketContext;
             do
             {
                 if (context == null)
                 {
                     break;
                 }
                 NSJSFunction function = context.ConnectedAsyncCallback;
                 NSJSObject   socket   = context.This;
                 context.ConnectedAsync         = null;
                 context.ConnectedAsyncCallback = null;
                 if (function == null)
                 {
                     break;
                 }
                 NSJSVirtualMachine machine = function.VirtualMachine;
                 if (machine == null)
                 {
                     break;
                 }
                 machine.Join((sendert, statet) => function.Call(socket, NSJSInt32.New(machine, unchecked ((int)e.SocketError))));
             } while (false);
         }
         catch (Exception) { }
     }
 }
Beispiel #2
0
        private static bool ProcessEvent(object sender, string evt, EventArgs e)
        {
            if (sender == null || string.IsNullOrEmpty(evt))
            {
                return(false);
            }
            WebSocket socket = sender as WebSocket;

            if (socket == null)
            {
                return(false);
            }
            NSJSObject websocket = Get(socket);

            if (websocket == null)
            {
                return(false);
            }
            NSJSVirtualMachine machine = websocket.VirtualMachine;

            machine.Join(delegate
            {
                NSJSFunction callback = websocket.Get(evt) as NSJSFunction;
                if (callback != null)
                {
                    NSJSObject data = WebSocketClient.GetMessageEventData(machine, e);
                    callback.Call(new NSJSValue[] { data });
                }
            });
            return(true);
        }
Beispiel #3
0
            public void ProcessRequest(HTTPContext context)
            {
                if (context == null)
                {
                    return /*undefined*/;
                }
                NSJSVirtualMachine machine = this.GetVirtualMachine();

                machine.Join((sender, state) =>
                {
                    NSJSFunction function = this.GetProcessRequestCallback();
                    if (function != null)
                    {
                        NSJSObject context_object = null;
                        try
                        {
                            context_object = this.NewContextObject(context);
                        }
                        catch (Exception) { /*-----*/ }
                        if (context_object != null)
                        {
                            function.Call(context_object);
                        }
                    }
                });
            }
Beispiel #4
0
 private static void OnEndProcessRequest(object sender, HTTPContext e)
 {
     DoProcessRequest(sender, (application, origin, machine) =>
     {
         NSJSFunction callback = origin.Get("EndProcessRequest") as NSJSFunction;
         if (callback != null)
         {
             callback.Call(HttpHandler.NewContextObject(machine, origin, e));
         }
     });
 }
Beispiel #5
0
        private static void AcceptAsync(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            bool success = false;

            do
            {
                SocketContext context = GetSocketContext(arguments.This);
                if (context == null)
                {
                    Throwable.ObjectDisposedException(arguments.VirtualMachine);
                    break;
                }
                SOCKET socket = context.Socket;
                if (socket == null || SocketExtension.CleanedUp(socket))
                {
                    Throwable.ObjectDisposedException(arguments.VirtualMachine);
                    break;
                }
                NSJSFunction callback = arguments.Length > 0 ? arguments[0] as NSJSFunction : null;
                try
                {
                    SocketAsyncEventArgs e = context.AcceptAsync;
                    if (e != null)
                    {
                        break;
                    }
                    e                   = new SocketAsyncEventArgs();
                    e.Completed        += ProcessAccept;
                    e.UserToken         = context;
                    context.AcceptAsync = e;
                    if (callback != null)
                    {
                        NSJSObject socketobject = arguments.This;
                        callback.CrossThreading     = true;
                        context.AcceptAsyncCallback = callback;
                    }
                    if (!socket.AcceptAsync(e))
                    {
                        ProcessAccept(socket, e);
                    }
                    success = true;
                }
                catch (Exception e)
                {
                    Throwable.Exception(arguments.VirtualMachine, e);
                }
            } while (false);
            arguments.SetReturnValue(success);
        }
Beispiel #6
0
        // [native] bool HttpClient.TryUploadAsync(string url, HttpPostValue[] blobs, HttpClientOptions options, HttpClientResponse response, HttpClientAsyncCallback callback)
        private static void TryUploadAsync(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            bool success = false;

            if (arguments.Length > 4)
            {
                string                      url      = (arguments[0] as NSJSString)?.Value;
                HttpClientOptions           options  = HttpClient.object2options(arguments[2] as NSJSObject);
                NSJSObject                  response = arguments[3] as NSJSObject;
                IEnumerable <HttpPostValue> blobs    = HttpClient.array2blobs(arguments[1] as NSJSArray);
                if (options != null && response != null)
                {
                    NSJSFunction callback = arguments[4] as NSJSFunction;
                    if (callback != null)
                    {
                        callback.CrossThreading = true;
                    }
                    response.CrossThreading = true;
                    bool fillToObject            = false;
                    HttpClientResponse responset = HttpClient.object2response(response);
                    success = RESTClient.TryUploadAsync(url, blobs, options, responset, (error, buffer, count) =>
                    {
                        NSJSVirtualMachine machine = arguments.VirtualMachine;
                        if (error == HttpClientError.Success && !fillToObject)
                        {
                            fillToObject = true;
                            fill2object(response, responset);
                        }
                        if (callback != null)
                        {
                            bool breakto = false;
                            machine.Join((sender, state) => breakto = ((callback.Call
                                                                        (
                                                                            NSJSInt32.New(machine, (int)error),
                                                                            NSJSValue.NullMerge(machine, buffer != null && count >= 0 ? NSJSUInt8Array.New(machine, buffer, count) : null)
                                                                        ) as NSJSBoolean)?.Value) == false);
                            if (breakto)
                            {
                                return(false);
                            }
                        }
                        return(count > 0);
                    });
                }
            }
            arguments.SetReturnValue(success);
        }
Beispiel #7
0
 private static void OnBeginProcessRequest(object sender, HttpBeginProcessRequestEventArgs e)
 {
     DoProcessRequest(sender, (application, origin, machine) =>
     {
         NSJSFunction callback = origin.Get("BeginProcessRequest") as NSJSFunction;
         if (callback != null)
         {
             NSJSObject args = NSJSObject.New(machine);
             args.Set("Cancel", e.Cancel);
             args.Set("Application", origin);
             args.Set("CurrentContext", HttpHandler.NewContextObject(machine, origin, e.CurrentContext));
             callback.Call(args);
             e.Cancel = args.Get("Cancel").As <bool>();
         }
     });
 }
Beispiel #8
0
 private static void InternalSend(IntPtr info, bool synchronization)
 {
     ObjectAuxiliary.Call <MailClient>(info, (smtp, arguments) =>
     {
         do
         {
             NSJSVirtualMachine machine = arguments.VirtualMachine;
             if (arguments.Length <= 0)
             {
                 Throwable.ArgumentException(machine);
                 break;
             }
             MailMessage message = null;
             try
             {
                 message = ObjectAuxiliary.ToMailMessage(arguments[0]);
             }
             catch (Exception exception)
             {
                 Throwable.Exception(machine, exception);
                 break;
             }
             if (message == null)
             {
                 Throwable.ArgumentNullException(machine);
                 break;
             }
             if (synchronization)
             {
                 arguments.SetReturnValue(smtp.Send(message));
             }
             else
             {
                 NSJSFunction function        = arguments.Length > 1 ? arguments[1] as NSJSFunction : null;
                 Action <Exception> callbackt = null;
                 if (function != null)
                 {
                     callbackt = (exception) => machine.Join((sender, state) =>
                                                             function.Call(new[] { Throwable.FormatMessage(exception) }));
                     function.CrossThreading = true;
                 }
                 arguments.SetReturnValue(smtp.SendAsync(message, callbackt));
             }
         } while (false);
     });
 }
Beispiel #9
0
        public static NSJSValue SendEvent(NSJSObject obj, string evt, IEnumerable <NSJSValue> args)
        {
            if (obj == null || string.IsNullOrEmpty(evt))
            {
                return(null);
            }
            NSJSFunction callback = obj.Get(evt) as NSJSFunction;

            if (callback == null)
            {
                return(null);
            }
            if (args == null)
            {
                return(callback.Call());
            }
            return(callback.Call(args));
        }
Beispiel #10
0
 private static void ProcessAccept(object sender, SocketAsyncEventArgs e)
 {
     try
     {
         SocketContext context = e.UserToken as SocketContext;
         do
         {
             SOCKET client = e.AcceptSocket;
             SOCKET server = (SOCKET)sender;
             e.AcceptSocket = null;
             do
             {
                 if (context == null)
                 {
                     break;
                 }
                 NSJSFunction function = context.AcceptAsyncCallback;
                 NSJSObject   socket   = context.This;
                 if (function == null)
                 {
                     break;
                 }
                 NSJSVirtualMachine machine = function.VirtualMachine;
                 if (machine == null)
                 {
                     break;
                 }
                 machine.Join((sendert, statet) => function.Call(socket,
                                                                 NSJSInt32.New(machine, unchecked ((int)e.SocketError)),
                                                                 NSJSValue.NullMerge(machine, New(machine, client))));
             } while (false);
             if (!server.AcceptAsync(e))
             {
                 ProcessAccept(server, e);
             }
         } while (false);
     }
     catch (Exception) { }
 }
Beispiel #11
0
        private static void Using(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            NSJSObject   self     = arguments.Length > 0 ? arguments[0] as NSJSObject : null;
            NSJSFunction function = arguments.Length > 1 ? arguments[1] as NSJSFunction : null;

            if (function != null)
            {
                function.Call(self);
            }
            if (self != null)
            {
                NSJSFunction close = self.Get("Dispose") as NSJSFunction;
                if (close == null)
                {
                    close = self.Get("Close") as NSJSFunction;
                }
                if (close != null)
                {
                    close.Call();
                }
            }
        }
Beispiel #12
0
        private static void SetTimer(IntPtr info, bool period)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            int handle = -1;

            if (arguments.Length > 1)
            {
                NSJSFunction callback = arguments[0] as NSJSFunction;
                NSJSInt32    millisec = arguments[1] as NSJSInt32;
                if (callback != null && millisec != null && millisec.Value >= 0)
                {
                    NSJSVirtualMachine machine = arguments.VirtualMachine;
                    callback.CrossThreading = true;
                    TIMER             timer = NSJSTimerScheduler.New(arguments.VirtualMachine);
                    IList <NSJSValue> argv  = new List <NSJSValue>();
                    for (int i = 2; i < arguments.Length; i++)
                    {
                        NSJSValue item = arguments[i];
                        item.CrossThreading = true;
                        argv.Add(item);
                    }
                    timer.Tick += (sender, e) =>
                    {
                        if (!period)
                        {
                            CloseTimer(handle);
                        }
                        machine.Join((sendert, statet) => callback.Call(argv));
                    };
                    handle         = AddTimer(timer);
                    timer.Interval = millisec.Value;
                    timer.Enabled  = true;
                }
            }
            arguments.SetReturnValue(handle);
        }
Beispiel #13
0
        private static bool ProcessEvent(NSJSObject server, WebSocket socket, string evt, EventArgs e, bool newing = false)
        {
            if (server == null || socket == null || string.IsNullOrEmpty(evt))
            {
                return(false);
            }
            NSJSVirtualMachine machine = server.VirtualMachine;

            machine.Join(delegate
            {
                NSJSObject socketclient = WebSocketClient.Get(socket);
                if (newing && socketclient == null)
                {
                    socketclient = WebSocketClient.New(machine, socket);
                }
                NSJSFunction function = server.Get(evt) as NSJSFunction;
                if (function != null)
                {
                    NSJSObject data = WebSocketClient.GetMessageEventData(machine, e);
                    function.Call(new NSJSValue[] { socketclient, data });
                }
            });
            return(true);
        }
Beispiel #14
0
        private static void Sort(IntPtr info, Action <NSJSArray, int, int, Func <NSJSValue, NSJSValue, bool> > sorting)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            bool success = false;

            if (arguments.Length > 1)
            {
                NSJSArray    s    = arguments[0] as NSJSArray;
                NSJSFunction max  = arguments[1] as NSJSFunction;
                int          low  = 0;
                int          high = -1;
                if (arguments.Length > 3 && max == null)
                {
                    max = arguments[3] as NSJSFunction;
                    NSJSInt32 i32 = arguments[1] as NSJSInt32;
                    if (i32 != null)
                    {
                        low = i32.Value;
                    }
                    i32 = arguments[2] as NSJSInt32;
                    if (i32 != null)
                    {
                        high = i32.Value;
                    }
                }
                if (max != null && s != null)
                {
                    if (high < 0)
                    {
                        high = s.Length - 1;
                    }
                    sorting(s, low, high, (x, y) => ((max.Call(x, y) as NSJSBoolean)?.Value).GetValueOrDefault());
                }
            }
            arguments.SetReturnValue(success);
        }
Beispiel #15
0
        private static void ConnectAsync(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            bool success = false;

            do
            {
                if (arguments.Length <= 0)
                {
                    Throwable.ObjectDisposedException(arguments.VirtualMachine);
                    break;
                }
                SocketContext context = GetSocketContext(arguments.This);
                if (context == null)
                {
                    Throwable.ObjectDisposedException(arguments.VirtualMachine);
                    break;
                }
                SOCKET socket = context.Socket;
                if (socket == null || SocketExtension.CleanedUp(socket))
                {
                    Throwable.ObjectDisposedException(arguments.VirtualMachine);
                    break;
                }
                EndPoint remoteEP = ObjectAuxiliary.ToEndPoint(arguments[0]);
                int      cbsolt   = 1;
                if (remoteEP == null)
                {
                    IPAddress address = ObjectAuxiliary.ToAddress(arguments[0]);
                    if (address == null)
                    {
                        break;
                    }
                    int port = arguments.Length > 1 ? ((arguments[1] as NSJSInt32)?.Value).GetValueOrDefault() : 0;
                    remoteEP = new IPEndPoint(address, port);
                    cbsolt++;
                }
                if (remoteEP == null)
                {
                    break;
                }
                NSJSFunction callback = arguments.Length > cbsolt ? arguments[cbsolt] as NSJSFunction : null;
                try
                {
                    SocketAsyncEventArgs e = context.ConnectedAsync;
                    if (e != null)
                    {
                        break;
                    }
                    else
                    {
                        e                      = new SocketAsyncEventArgs();
                        e.Completed           += ProcessConnected;
                        e.UserToken            = context;
                        context.ConnectedAsync = e;
                    }
                    e.RemoteEndPoint = remoteEP;
                    if (callback != null)
                    {
                        callback.CrossThreading        = true;
                        context.ConnectedAsyncCallback = callback;
                    }
                    if (!socket.ConnectAsync(e))
                    {
                        ProcessConnected(socket, e);
                    }
                    success = true;
                }
                catch (Exception e)
                {
                    Throwable.Exception(arguments.VirtualMachine, e);
                }
            } while (false);
            arguments.SetReturnValue(success);
        }