Skip to content

Commit

Permalink
Add chanel close handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
veblush committed Jul 13, 2016
1 parent 3dc1a51 commit 4cf3c44
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 29 deletions.
71 changes: 42 additions & 29 deletions src/GameClient/Assets/Scripts/Base/LoginProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using Akka.Interfaced;
Expand All @@ -8,6 +9,7 @@
using Common.Logging;
using Domain;
using UnityEngine;
using UnityEngine.SceneManagement;

public static class LoginProcessor
{
Expand All @@ -20,39 +22,33 @@ public static Task Login(MonoBehaviour owner, IPEndPoint endPoint, string id, st
}

private static IEnumerator LoginCoroutine(IPEndPoint endPoint, string id, string password,
ISlimTaskCompletionSource<bool> task, Action<string> progressReport)
ISlimTaskCompletionSource<bool> tcs, Action<string> progressReport)
{
// Connect

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

IChannel channel = null;
if (G.Communicator == null || G.Communicator.Channels.Count == 0)
var communicator = UnityCommunicatorFactory.Create();
{
var communicator = UnityCommunicatorFactory.Create();
{
var channelFactory = communicator.ChannelFactory;
channelFactory.Type = ChannelType.Tcp;
channelFactory.ConnectEndPoint = endPoint;
channelFactory.CreateChannelLogger = () => LogManager.GetLogger("Channel");
channelFactory.PacketSerializer = PacketSerializer.CreatePacketSerializer<DomainProtobufSerializer>();
}
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;
}
var channelFactory = communicator.ChannelFactory;
channelFactory.Type = ChannelType.Tcp;
channelFactory.ConnectEndPoint = endPoint;
channelFactory.CreateChannelLogger = () => LogManager.GetLogger("Channel");
channelFactory.PacketSerializer = PacketSerializer.CreatePacketSerializer<DomainProtobufSerializer>();
}

G.Communicator = communicator;
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)
Expand All @@ -65,12 +61,15 @@ private static IEnumerator LoginCoroutine(IPEndPoint endPoint, string id, string

if (t1.Status != TaskStatus.RanToCompletion)
{
task.TrySetException(new Exception("Login Error\n" + t1.Exception, t1.Exception));
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;

Expand All @@ -81,12 +80,12 @@ private static IEnumerator LoginCoroutine(IPEndPoint endPoint, string id, string
yield return t2.WaitHandle;
if (t2.Exception != null)
{
UiMessageBox.Show("ConnectToUser error:\n" + t2.Exception.ToString());
tcs.TrySetException(new Exception("ConnectToUser error\n" + t2.Exception, t2.Exception));
yield break;
}
}

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

var t3 = userInitiator.Load(observer);
yield return t3.WaitHandle;
Expand All @@ -101,14 +100,16 @@ private static IEnumerator LoginCoroutine(IPEndPoint endPoint, string id, string
yield return t4.WaitHandle;
if (t4.Exception != null)
{
UiMessageBox.Show("CreateUser error:\n" + t4.Exception.ToString());
tcs.TrySetException(new Exception("CreateUser error\n" + t4.Exception, t4.Exception));
communicator.ObserverRegistry.Remove(observer);
yield break;
}
G.UserContext = t4.Result;
}
else
{
UiMessageBox.Show("Load error:\n" + t3.Exception.ToString());
tcs.TrySetException(new Exception("LoadUser error\n" + t3.Exception, t3.Exception));
communicator.ObserverRegistry.Remove(observer);
yield break;
}
}
Expand All @@ -120,11 +121,23 @@ private static IEnumerator LoginCoroutine(IPEndPoint endPoint, string id, string
G.User = userInitiator.Cast<UserRef>();
G.UserId = userId;

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

tcs.TrySetResult(true);

observer.GetEventDispatcher().Pending = false;
}

private static void OnChannelClose(object channel)
{
G.User = null;
SceneManager.LoadScene("MainScene");
}

public static IPEndPoint GetEndPointAddress(string address)
{
var a = address.Trim();
Expand Down
6 changes: 6 additions & 0 deletions src/GameClient/Assets/Scripts/Scenes/MainScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ private void StartLogin()
}
else
{
if (G.Communicator != null)
{
UiMessageBox.Show("Connection Closed!");
G.Communicator = null;
}

LoginPanel.gameObject.SetActive(true);

var loginServer = PlayerPrefs.GetString("LoginServer");
Expand Down

0 comments on commit 4cf3c44

Please sign in to comment.