Пример #1
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);
        }