Example #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) { }
     }
 }
Example #2
0
 private static void ReceiveAsync(IntPtr info)
 {
     InternalReceiveAsync(info, false, (socket, socketobject, data, buffer, ofs, count, flags, remoteep, callback) =>
     {
         bool success = false;
         if ((success = SocketExtension.BeginReceive(socket, buffer, ofs, count, flags, (result) =>
         {
             int len = SocketExtension.EndReceive(socket, result, out SocketError error);
             NSJSVirtualMachine machine = socketobject.VirtualMachine;
             machine.Join((sender, state) =>
             {
                 if (len > 0)
                 {
                     for (int i = ofs; i < len; i++)
                     {
                         data[i] = buffer[i];
                     }
                 }
                 if (callback != null)
                 {
                     callback.Call(socketobject, NSJSInt32.New(machine, unchecked ((int)error)), NSJSInt32.New(machine, len));
                 }
             });
         })))
         {
             if (callback != null)
             {
                 callback.CrossThreading = true;
             }
             data.CrossThreading = true;
         }
         return(success);
     });
Example #3
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);
        }
Example #4
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);
                        }
                    }
                });
            }
Example #5
0
        private static bool fill2object(NSJSObject o, HttpClientResponse responset)
        {
            if (o == null || responset == null)
            {
                return(false);
            }
            NSJSVirtualMachine machine = o.VirtualMachine;

            machine.Join((sender, state) =>
            {
                o.Set("AsynchronousMode", responset.AsynchronousMode);
                o.Set("CharacterSet", responset.CharacterSet ?? string.Empty);
                o.Set("ContentLength", responset.ContentLength);
                o.Set("ContentType", responset.ContentType ?? string.Empty);
                o.Set("LastModified", NSJSDateTime.Invalid(responset.LastModified) ? NSJSDateTime.Min :
                      responset.LastModified);
                o.Set("ManualWriteToStream", responset.ManualWriteToStream);
                o.Set("ResponseUri", responset.ResponseUri == null ? string.Empty :
                      responset.ResponseUri.ToString());
                o.Set("Server", responset.Server ?? string.Empty);
                o.Set("StatusCode", unchecked ((int)responset.StatusCode));
                o.Set("ContentEncoding", responset.ContentEncoding ?? string.Empty);
                o.Set("StatusDescription", responset.StatusDescription ?? string.Empty);
            });
            return(true);
        }
Example #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);
        }
Example #7
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);
     });
 }
Example #8
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) { }
 }
Example #9
0
        private static void DoProcessRequest(object sender, Action <HTTPApplication, NSJSObject, NSJSVirtualMachine> callback)
        {
            if (callback == null)
            {
                throw new ArgumentNullException("callback");
            }
            HTTPApplication application = sender as HTTPApplication;

            if (application == null)
            {
                return /* false */;
            }
            NSJSObject origin = application.Tag as NSJSObject;

            if (origin == null)
            {
                return /* false */;
            }
            NSJSVirtualMachine machine = origin.VirtualMachine;

            machine.Join((M, X) => callback(application, origin, machine));
        }
Example #10
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);
        }
Example #11
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);
        }