示例#1
0
        private static void Start(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments   = NSJSFunctionCallbackInfo.From(info);
            HTTPApplication          application = GetApplication(arguments.This);

            if (application == null)
            {
                Throwable.ObjectDisposedException(arguments.VirtualMachine);
            }
            else
            {
                try
                {
                    IList <string> prefixes = ArrayAuxiliary.ToStringList(arguments.Length > 0 ? arguments[0] : null);
                    do
                    {
                        if (prefixes.IsNullOrEmpty())
                        {
                            Throwable.ArgumentNullException(arguments.VirtualMachine);
                            break;
                        }
                        application.Start(prefixes);
                        arguments.SetReturnValue(true);
                    } while (false);
                }
                catch (Exception e)
                {
                    Throwable.Exception(arguments.VirtualMachine, e);
                }
            }
        }
示例#2
0
        private static void Position(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            BaseStream stream = NSJSKeyValueCollection.Get <BaseStream>(arguments.This);

            if (stream == null)
            {
                Throwable.ObjectDisposedException(arguments.VirtualMachine);
            }
            else if (arguments.Length <= 0)
            {
                arguments.SetReturnValue(unchecked ((int)stream.Position));
            }
            else
            {
                NSJSInt32 value = arguments[0] as NSJSInt32;
                if (value == null)
                {
                    arguments.SetReturnValue(false);
                }
                else
                {
                    try
                    {
                        stream.Position = value.Value;
                        arguments.SetReturnValue(true);
                    }
                    catch (Exception e)
                    {
                        Throwable.Exception(arguments.VirtualMachine, e);
                    }
                }
            }
        }
示例#3
0
        private static void Flush(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            BaseStream stream = NSJSKeyValueCollection.Get <BaseStream>(arguments.This);

            if (stream == null)
            {
                Throwable.ObjectDisposedException(arguments.VirtualMachine);
            }
            else
            {
                Exception exception = null;
                bool      success   = false;
                try
                {
                    stream.Flush();
                }
                catch (Exception e)
                {
                    exception = e;
                }
                if (exception != null)
                {
                    Throwable.Exception(arguments.VirtualMachine, exception);
                }
                else
                {
                    arguments.SetReturnValue(success);
                }
            }
        }
示例#4
0
        private static void InternalExecute(NSJSFunctionCallbackInfo arguments,
                                            Action <DATATableGateway, DatabaseAccessAdapter, string> executing)
        {
            if (executing == null)
            {
                throw new ArgumentNullException("executing");
            }
            DATATableGateway gateway = GetGateway(arguments.This);

            if (gateway == null)
            {
                Throwable.ObjectDisposedException(arguments.VirtualMachine);
            }
            else if (arguments.Length <= 0)
            {
                Throwable.ObjectDisposedException(arguments.VirtualMachine);
            }
            else
            {
                string text = arguments.Length > 0 ? (arguments[0] as NSJSString)?.Value : null;
                if (text == null)
                {
                    Throwable.ArgumentNullException(arguments.VirtualMachine);
                }
                else
                {
                    executing(gateway, gateway.DatabaseAccessAdapter, text);
                }
            }
        }
示例#5
0
        private static void CopyTo(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            BaseStream stream = NSJSKeyValueCollection.Get <BaseStream>(arguments.This);

            if (stream == null)
            {
                Throwable.ObjectDisposedException(arguments.VirtualMachine);
            }
            else
            {
                BaseStream destination = NSJSKeyValueCollection.Get <BaseStream>(arguments.Length > 0 ? arguments[0] as NSJSObject : null);
                if (destination == null)
                {
                    Throwable.ArgumentNullException(arguments.VirtualMachine);
                }
                else
                {
                    try
                    {
                        stream.CopyTo(destination);
                    }
                    catch (Exception e)
                    {
                        Throwable.Exception(arguments.VirtualMachine, e);
                    }
                }
            }
        }
示例#6
0
        private static void GetBytes(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            ENCODING encoding = GetEncoding(arguments.This);

            if (encoding == null)
            {
                Throwable.ObjectDisposedException(arguments.VirtualMachine);
            }
            else
            {
                byte[] buffer = null;
                if (arguments.Length > 0)
                {
                    NSJSString s = arguments[0] as NSJSString;
                    if (s != null)
                    {
                        buffer = encoding.GetBytes(s.Value);
                    }
                }
                if (buffer == null)
                {
                    buffer = BufferExtension.EmptryBuffer;
                }
                arguments.SetReturnValue(buffer);
            }
        }
示例#7
0
        private static void ContentEncoding(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            HTTPResponse             response  = GetResponse(arguments.This);

            if (response == null)
            {
                Throwable.ObjectDisposedException(arguments.VirtualMachine);
            }
            else
            {
                NSJSVirtualMachine machine = arguments.VirtualMachine;
                if (arguments.Length <= 0)
                {
                    arguments.SetReturnValue(NSJSEncoding.New(machine, response.ContentEncoding));
                }
                else
                {
                    var encoding = NSJSEncoding.GetEncoding(arguments[0] as NSJSObject);
                    if (encoding == null)
                    {
                        encoding = NSJSEncoding.DefaultEncoding;
                    }
                    response.ContentEncoding = encoding;
                    arguments.SetReturnValue(true);
                }
            }
        }
示例#8
0
        private void EncryptOrDecrypt(IntPtr info, bool decrypt)
        {
            NSJSFunctionCallbackInfo      arguments = NSJSFunctionCallbackInfo.From(info);
            RijndaelCryptoServiceProvider provider  = NSJSKeyValueCollection.Get <RijndaelCryptoServiceProvider>(arguments.This);

            if (provider == null)
            {
                Throwable.ObjectDisposedException(arguments.VirtualMachine);
            }
            else
            {
                byte[]    result    = null;
                Exception exception = null;
                if (arguments.Length > 0)
                {
                    byte[] buffer = (arguments[0] as NSJSUInt8Array)?.Buffer;
                    if (buffer != null)
                    {
                        int ofs = 0;
                        if (arguments.Length > 1)
                        {
                            ofs = ((arguments[1] as NSJSInt32)?.Value).GetValueOrDefault();
                        }
                        int count = buffer.Length;
                        if (arguments.Length > 2)
                        {
                            count = ((arguments[2] as NSJSInt32)?.Value).GetValueOrDefault();
                        }
                        try
                        {
                            if (decrypt)
                            {
                                result = provider.Decrypt(buffer, ofs, count);
                            }
                            else
                            {
                                result = provider.Encrypt(buffer, ofs, count);
                            }
                        }
                        catch (Exception e)
                        {
                            exception = e;
                        }
                    }
                }
                if (exception != null)
                {
                    Throwable.Exception(arguments.VirtualMachine, exception);
                }
                else if (result != null)
                {
                    arguments.SetReturnValue(result);
                }
                else
                {
                    Throwable.ArgumentNullException(arguments.VirtualMachine, exception);
                }
            }
        }
示例#9
0
        private static void GetString(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            ENCODING encoding = NSJSKeyValueCollection.Get <ENCODING>(arguments.This);

            if (encoding == null)
            {
                Throwable.ObjectDisposedException(arguments.VirtualMachine);
            }
            else
            {
                string s = null;
                if (arguments.Length > 0)
                {
                    NSJSUInt8Array chars = arguments[0] as NSJSUInt8Array;
                    if (chars != null)
                    {
                        byte[] buffer = chars.Buffer;
                        if (buffer != null)
                        {
                            NSJSInt32 index = null;
                            NSJSInt32 len   = null;
                            switch (arguments.Length)
                            {
                            case 2:
                                len = arguments[1] as NSJSInt32;
                                break;

                            case 3:
                                index = arguments[1] as NSJSInt32;
                                len   = arguments[2] as NSJSInt32;
                                break;
                            }
                            int ofs   = index != null ? index.Value : 0;
                            int count = len != null ? len.Value : buffer.Length;
                            if (count < 0)
                            {
                                count = 0;
                            }
                            if (ofs < 0)
                            {
                                ofs = 0;
                            }
                            s = encoding.GetString(buffer, ofs, count);
                        }
                    }
                }
                if (s != null)
                {
                    arguments.SetReturnValue(s);
                }
                else
                {
                    arguments.SetReturnValue(NSJSValue.Undefined(arguments.VirtualMachine));
                }
            }
        }
示例#10
0
        private static void Length(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            BaseStream stream = NSJSKeyValueCollection.Get <BaseStream>(arguments.This);

            if (stream == null)
            {
                Throwable.ObjectDisposedException(arguments.VirtualMachine);
            }
            else
            {
                arguments.SetReturnValue(unchecked ((int)stream.Length));
            }
        }
示例#11
0
        private static void Connected(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            SOCKET socket = GetSocket(arguments.This);

            if (socket == null || SocketExtension.CleanedUp(socket))
            {
                Throwable.ObjectDisposedException(arguments.VirtualMachine);
            }
            else
            {
                arguments.SetReturnValue(socket.Connected);
            }
        }
示例#12
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);
        }
示例#13
0
        private static void Stop(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments   = NSJSFunctionCallbackInfo.From(info);
            HTTPApplication          application = GetApplication(arguments.This);

            if (application == null)
            {
                Throwable.ObjectDisposedException(arguments.VirtualMachine);
            }
            else
            {
                application.Stop();
                arguments.SetReturnValue(true);
            }
        }
示例#14
0
        private static void ReadBytes(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            BaseStream stream = NSJSKeyValueCollection.Get <BaseStream>(arguments.This);

            if (stream == null)
            {
                Throwable.ObjectDisposedException(arguments.VirtualMachine);
            }
            else
            {
                NSJSValue result    = null;
                Exception exception = null;
                if (arguments.Length > 0)
                {
                    int max = ((arguments[0] as NSJSInt32)?.Value).GetValueOrDefault();
                    if (max >= 0)
                    {
                        try
                        {
                            byte[] ch  = new byte[max];
                            int    len = stream.Read(ch, 0, max);
                            result = NSJSUInt8Array.New(arguments.VirtualMachine, ch, len);
                        }
                        catch (Exception e)
                        {
                            exception = e;
                        }
                    }
                }
                if (exception != null)
                {
                    Throwable.Exception(arguments.VirtualMachine, exception);
                }
                else if (result != null)
                {
                    arguments.SetReturnValue(result);
                }
                else
                {
                    arguments.SetReturnValue(NSJSValue.Undefined(arguments.VirtualMachine));
                }
            }
        }
示例#15
0
        private static void Seek(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            BaseStream stream = NSJSKeyValueCollection.Get <BaseStream>(arguments.This);

            if (stream == null)
            {
                Throwable.ObjectDisposedException(arguments.VirtualMachine);
            }
            else
            {
                Exception exception = null;
                int       offset    = 0;
                int?      result    = null;
                if (stream != null)
                {
                    SeekOrigin origin = SeekOrigin.Begin;
                    if (arguments.Length > 0)
                    {
                        offset = ((arguments[0] as NSJSInt32)?.Value).GetValueOrDefault();
                    }
                    if (arguments.Length > 1)
                    {
                        origin = (SeekOrigin)((arguments[1] as NSJSInt32)?.Value).GetValueOrDefault();
                    }
                    try
                    {
                        result = unchecked ((int)stream.Seek(offset, origin));
                    }
                    catch (Exception e)
                    {
                        exception = e;
                    }
                }
                if (exception != null)
                {
                    Throwable.Exception(arguments.VirtualMachine, exception);
                }
                else
                {
                    arguments.SetReturnValue(result.GetValueOrDefault());
                }
            }
        }
示例#16
0
        private static void Accept(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            SOCKET socket = GetSocket(arguments.This);

            if (socket == null || SocketExtension.CleanedUp(socket))
            {
                Throwable.ObjectDisposedException(arguments.VirtualMachine);
            }
            else
            {
                try
                {
                    arguments.SetReturnValue(New(arguments.VirtualMachine, socket.Accept()));
                }
                catch (Exception e)
                {
                    Throwable.Exception(arguments.VirtualMachine, e);
                }
            }
        }
示例#17
0
        private static void Read(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            BaseStream stream = NSJSKeyValueCollection.Get <BaseStream>(arguments.This);

            if (stream == null)
            {
                Throwable.ObjectDisposedException(arguments.VirtualMachine);
            }
            else
            {
                Exception exception = null;
                int?      len       = null;
                if (arguments.Length > 0)
                {
                    NSJSUInt8Array buffer = arguments[0] as NSJSUInt8Array;
                    if (buffer != null)
                    {
                        int?count  = null;
                        int offset = 0;
                        switch (arguments.Length)
                        {
                        case 2:
                            count = ((arguments[1] as NSJSInt32)?.Value).GetValueOrDefault();
                            break;

                        case 3:
                            offset = ((arguments[1] as NSJSInt32)?.Value).GetValueOrDefault();
                            count  = (arguments[2] as NSJSInt32)?.Value;
                            break;
                        }
                        if (offset < 0)
                        {
                            offset = 0;
                        }
                        if (count == null)
                        {
                            count = buffer.Length;
                        }
                        if ((offset + count) > buffer.Length)
                        {
                            count = (buffer.Length - offset);
                        }
                        if (offset < 0 || offset >= count)
                        {
                            len = 0;
                        }
                        else
                        {
                            try
                            {
                                byte[] cch = new byte[count.Value];
                                len = stream.Read(cch, offset, cch.Length);
                                if (len > 0)
                                {
                                    for (int i = offset; i < len; i++)
                                    {
                                        buffer[i] = cch[i];
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                exception = e;
                            }
                        }
                    }
                }
                if (exception != null)
                {
                    Throwable.Exception(arguments.VirtualMachine, exception);
                }
                else
                {
                    arguments.SetReturnValue(len.GetValueOrDefault());
                }
            }
        }
示例#18
0
        private static void Write(IntPtr info)
        {
            NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info);
            BaseStream stream = NSJSKeyValueCollection.Get <BaseStream>(arguments.This);

            if (stream == null)
            {
                Throwable.ObjectDisposedException(arguments.VirtualMachine);
            }
            else
            {
                Exception exception = null;
                bool      success   = false;
                if (arguments.Length > 0)
                {
                    NSJSUInt8Array buffer = arguments[0] as NSJSUInt8Array;
                    if (buffer != null)
                    {
                        int?count  = null;
                        int offset = 0;
                        switch (arguments.Length)
                        {
                        case 2:
                            count = (arguments[1] as NSJSInt32)?.Value;
                            break;

                        case 3:
                            offset = ((arguments[1] as NSJSInt32)?.Value).GetValueOrDefault();
                            count  = (arguments[2] as NSJSInt32)?.Value;
                            break;
                        }
                        if (offset < 0)
                        {
                            offset = 0;
                        }
                        if (count == null)
                        {
                            count = buffer.Length;
                        }
                        if ((offset + count) > buffer.Length)
                        {
                            count = (buffer.Length - offset);
                        }
                        if (!(offset < 0 || offset >= count))
                        {
                            try
                            {
                                stream.Write(buffer.Buffer, offset, count.Value);
                            }
                            catch (Exception e)
                            {
                                exception = e;
                            }
                        }
                    }
                }
                if (exception != null)
                {
                    Throwable.Exception(arguments.VirtualMachine, exception);
                }
                else
                {
                    arguments.SetReturnValue(success);
                }
            }
        }
示例#19
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);
        }