IEnumerator ProcessTest(ChannelType channelType)
    {
        LogText.text = "ProcessTest(" + channelType + ")\n";

        // Create communicator

        var communicator = UnityCommunicatorFactory.Create();
        {
            var channelFactory = communicator.ChannelFactory;
            channelFactory.Type                = ChannelType.Tcp;
            channelFactory.ConnectEndPoint     = new IPEndPoint(IPAddress.Loopback, 5000);
            channelFactory.CreateChannelLogger = () => LogManager.GetLogger("Channel");
            channelFactory.PacketSerializer    = PacketSerializer.CreatePacketSerializer <DomainProtobufSerializer>();
        }

        // Connect channel

        var channel = communicator.CreateChannel();
        var t0      = channel.ConnectAsync();

        yield return(t0.WaitHandle);

        if (t0.Exception != null)
        {
            WriteLine("Connection Failed: " + t0.Exception.Message);
            yield break;
        }

        // Start communicating with actors via channel

        var greeter = channel.CreateRef <GreeterRef>();

        WriteLine("Start ProcessTest");
        WriteLine("");

        var t1 = greeter.Hello("Alice");

        yield return(t1.WaitHandle);

        WriteLine("Hello(Alice) = " + t1.Result);

        var t2 = greeter.Hello("Bob");

        yield return(t2.WaitHandle);

        WriteLine("Hello(Bob) = " + t2.Result);

        var t3 = greeter.GetHelloCount();

        yield return(t3.WaitHandle);

        WriteLine("GetHelloCount = " + t3.Result);

        WriteLine("");
        WriteLine("End ProcessTest");

        channel.Close();
    }
    IEnumerator ProcessTest(ChannelType channelType)
    {
        LogText.text = "ProcessTest(" + channelType + ")\n";

        // Create channel and connect to gateway

        var communicator = UnityCommunicatorFactory.Create();

        communicator.ChannelFactory.Type                = channelType;
        communicator.ChannelFactory.ConnectEndPoint     = new IPEndPoint(IPAddress.Loopback, 5001);
        communicator.ChannelFactory.CreateChannelLogger = () => LogManager.GetLogger("Channel");
        communicator.ChannelFactory.PacketSerializer    = PacketSerializer.CreatePacketSerializer <InterfaceProtobufSerializer>();

        var channel = communicator.CreateChannel();

        var t0 = channel.ConnectAsync();

        yield return(t0.WaitHandle);

        if (t0.Exception != null)
        {
            WriteLine("Connection Failed: " + t0.Exception.Message);
            yield break;
        }

        // Start communicating with actors via channel

        var entry = channel.CreateRef <EntryRef>();

        WriteLine("Start ProcessTest");
        WriteLine("");

        var t1 = entry.GetGreeter();

        yield return(t1.WaitHandle);

        yield return(StartCoroutine(ProcessGreeter(channel, t1.Result)));

        var t2 = entry.GetCalculator();

        yield return(t2.WaitHandle);

        yield return(StartCoroutine(ProcessCalculator(t2.Result)));

        var t3 = entry.GetCounter();

        yield return(t3.WaitHandle);

        yield return(StartCoroutine(ProcessCounter(t3.Result)));

        var t4 = entry.GetPedantic();

        yield return(t4.WaitHandle);

        yield return(StartCoroutine(ProcessPedantic(t4.Result)));

        var t5 = entry.GetGreeterOnAnotherChannel();

        yield return(t5.WaitHandle);

        yield return(StartCoroutine(ProcessGreeterOnAnotherChannel(t5.Result)));

        channel.Close();

        WriteLine("Done!");
    }
Example #3
0
    private IEnumerator ProcessLogin(string server, ChannelType type, string id, string password)
    {
        try
        {
            IPEndPoint serverEndPoint;
            try
            {
                serverEndPoint = GetEndPointAddress(server);
            }
            catch (Exception e)
            {
                UiMessageBox.Show("Server address error:\n" + e.ToString());
                yield break;
            }

            var communicator = UnityCommunicatorFactory.Create();
            {
                var channelFactory = communicator.ChannelFactory;
                channelFactory.Type                = type;
                channelFactory.ConnectEndPoint     = serverEndPoint;
                channelFactory.CreateChannelLogger = () => LogManager.GetLogger("Channel");
                channelFactory.PacketSerializer    = PacketSerializer.CreatePacketSerializer <DomainProtobufSerializer>();
            }
            var channel = communicator.CreateChannel();

            // connect to gateway

            var t0 = channel.ConnectAsync();
            yield return(t0.WaitHandle);

            if (t0.Exception != null)
            {
                UiMessageBox.Show("Connect error:\n" + t0.Exception.Message);
                yield break;
            }

            // Try Login

            var userLogin = channel.CreateRef <UserLoginRef>();
            var observer  = communicator.ObserverRegistry.Create(_userEventObserver);
            var t1        = userLogin.Login(id, password, observer);
            yield return(t1.WaitHandle);

            if (t1.Exception != null)
            {
                communicator.ObserverRegistry.Remove(observer);
                var re = t1.Exception as ResultException;
                if (re != null)
                {
                    UiMessageBox.Show("Login error:\n" + re.ResultCode.ToString());
                }
                else
                {
                    UiMessageBox.Show("Login error:\n" + t1.Exception.ToString());
                }
                channel.Close();
                yield break;
            }

            var user = (UserRef)t1.Result;
            if (user.IsChannelConnected() == false)
            {
                var t2 = user.ConnectChannelAsync();
                yield return(t2.WaitHandle);

                if (t2.Exception != null)
                {
                    UiMessageBox.Show("ConnectToUser error:\n" + t2.Exception.ToString());
                    channel.Close();
                    yield break;
                }
                channel.Close();
            }

            G.Communicator = communicator;
            G.User         = user;
            G.UserId       = id;
            Hide(id);
        }
        finally
        {
            _isLoginBusy = false;
        }
    }
Example #4
0
    private static IEnumerator LoginCoroutine(IPEndPoint endPoint, string id, string password,
                                              ISlimTaskCompletionSource <bool> tcs, Action <string> progressReport)
    {
        // Connect

        if (progressReport != null)
        {
            progressReport("Connect");
        }

        var communicator = UnityCommunicatorFactory.Create();
        {
            var channelFactory = communicator.ChannelFactory;
            channelFactory.Type                = ChannelType.Udp;
            channelFactory.ConnectEndPoint     = endPoint;
            channelFactory.CreateChannelLogger = () => LogManager.GetLogger("Channel");
            channelFactory.PacketSerializer    = PacketSerializer.CreatePacketSerializer <DomainProtobufSerializer>();
        }

        var channel = communicator.CreateChannel();
        var t0      = channel.ConnectAsync();

        yield return(t0.WaitHandle);

        if (t0.Exception != null)
        {
            tcs.TrySetException(new Exception("Connect error\n" + t0.Exception, t0.Exception));
            yield break;
        }

        G.Communicator = communicator;

        // Login

        if (progressReport != null)
        {
            progressReport("Login");
        }

        channel = G.Communicator.Channels[0];
        var userLogin = channel.CreateRef <UserLoginRef>();
        var t1        = userLogin.Login(id, password);

        yield return(t1.WaitHandle);

        if (t1.Status != TaskStatus.RanToCompletion)
        {
            tcs.TrySetException(new Exception("Login Error\n" + t1.Exception, t1.Exception));
            yield break;
        }

        // Query User

        if (progressReport != null)
        {
            progressReport("Query User");
        }

        var userId        = t1.Result.Item1;
        var userInitiator = (UserInitiatorRef)t1.Result.Item2;

        if (userInitiator.IsChannelConnected() == false)
        {
            channel.Close();
            var t2 = userInitiator.ConnectChannelAsync();
            yield return(t2.WaitHandle);

            if (t2.Exception != null)
            {
                tcs.TrySetException(new Exception("ConnectToUser error\n" + t2.Exception, t2.Exception));
                yield break;
            }
        }

        var observer = communicator.ObserverRegistry.Create <IUserEventObserver>(UserEventProcessor.Instance, startPending: true);

        var t3 = userInitiator.Load(observer);

        yield return(t3.WaitHandle);

        if (t3.Exception != null)
        {
            if (t3.Exception is ResultException && ((ResultException)t3.Exception).ResultCode == ResultCodeType.UserNeedToBeCreated)
            {
                var inputDialog = UiInputBox.Show("Input your name:");
                yield return(inputDialog.WaitForHide());

                var userName = (string)inputDialog.ReturnValue;
                var t4       = userInitiator.Create(observer, userName);
                yield return(t4.WaitHandle);

                if (t4.Exception != null)
                {
                    tcs.TrySetException(new Exception("CreateUser error\n" + t4.Exception, t4.Exception));
                    communicator.ObserverRegistry.Remove(observer);
                    yield break;
                }
                G.UserContext = t4.Result;
            }
            else
            {
                tcs.TrySetException(new Exception("LoadUser error\n" + t3.Exception, t3.Exception));
                communicator.ObserverRegistry.Remove(observer);
                yield break;
            }
        }
        else
        {
            G.UserContext = t3.Result;
        }

        G.User   = userInitiator.Cast <UserRef>();
        G.UserId = userId;

        communicator.Channels.Last().StateChanged += (_, state) =>
        {
            if (state == ChannelStateType.Closed)
            {
                ChannelEventDispatcher.Post(OnChannelClose, _);
            }
        };

        tcs.TrySetResult(true);

        observer.GetEventDispatcher().Pending = false;
    }
Example #5
0
    private IEnumerator ProcessLogin(ChannelType channelType, string credential)
    {
        WriteLine(string.Format("ProcessLogin({0}, {1})", channelType, credential));

        // create communicator

        var communicator = UnityCommunicatorFactory.Create();
        {
            var channelFactory = communicator.ChannelFactory;
            channelFactory.Type                = channelType;
            channelFactory.ConnectEndPoint     = new IPEndPoint(IPAddress.Loopback, 9001);
            channelFactory.CreateChannelLogger = () => LogManager.GetLogger("Channel");
            channelFactory.PacketSerializer    = PacketSerializer.CreatePacketSerializer <DomainProtobufSerializer>();
        }

        var channel = communicator.CreateChannel();

        // connect to gateway

        var t0 = channel.ConnectAsync();

        yield return(t0.WaitHandle);

        if (t0.Exception != null)
        {
            WriteLine("Connection Failed: " + t0.Exception);
            yield break;
        }

        // login with an user-login actor

        var userLogin = channel.CreateRef <UserLoginRef>();
        var t1        = userLogin.Login("C123");

        yield return(t1.WaitHandle);

        if (t1.Exception != null)
        {
            WriteLine("Login Failed: " + t1.Exception);
            yield break;
        }

        // initiate user from user-initiator

        var userInitiator = (UserInitiatorRef)t1.Result.Item2;

        if (userInitiator.IsChannelConnected() == false)
        {
            yield return(userInitiator.ConnectChannelAsync().WaitHandle);
        }

        var observer = communicator.ObserverRegistry.Create <IUserEventObserver>(this);
        var t2       = userInitiator.Load(observer);

        yield return(t2.WaitHandle);

        if (t2.Exception != null)
        {
            WriteLine("Load Failed: " + t2.Exception);
            if (t2.Exception is ResultException && ((ResultException)t2.Exception).ResultCode == ResultCodeType.UserNeedToBeCreated)
            {
                var t3 = userInitiator.Create(observer, "Unity");
                yield return(t3.WaitHandle);

                if (t3.Exception != null)
                {
                    WriteLine("Create Failed: " + t3.Exception);
                    yield break;
                }
                _userContext = t3.Result;
            }
            else
            {
                yield break;
            }
        }
        else
        {
            _userContext = t2.Result;
        }

        _communicator = communicator;
        _user         = userInitiator.Cast <UserRef>();
        _userObserver = observer;

        var userJson = JsonConvert.SerializeObject(_userContext, s_jsonSerializerSettings);

        WriteLine(string.Format("UserLoaded: {0}]\n", userJson));
    }