Skip to content

Commit 63c4991

Browse files
In progress refactor
1 parent 7d63ba9 commit 63c4991

20 files changed

+212
-54
lines changed

CoroutineHost.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
public class CoroutineHost : MonoSingleton<CoroutineHost>
4+
{
5+
public CoroutineHost ()
6+
{
7+
}
8+
9+
void OnApplicationQuit() {
10+
try {
11+
Meteor.LiveData.LiveData.Instance.Close();
12+
} catch (Exception e) {
13+
14+
}
15+
}
16+
}
17+

DDP/DDPConnector.cs.meta renamed to CoroutineHost.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

DDP/ILiveData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Meteor.LiveData
99
public interface ILiveData
1010
{
1111
void QueueMessage(string jsonItem);
12-
Coroutine Connect(string url);
12+
Coroutine Connect(string url, float timeout);
1313
Method Call (string methodName, params object[] arguments);
1414
Method<TResponseType> Call<TResponseType> (string methodName, params object[] arguments)
1515
where TResponseType : new();

DDP/IMethod.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
using System;
2+
using Meteor;
23

34
namespace Meteor.LiveData
45
{
5-
public delegate void MethodHandler(Meteor.Error error, object response);
6-
public delegate void MethodHandler<TResponseType>(Meteor.Error error, TResponseType response);
6+
public delegate void MethodHandler(Error error, object response);
7+
public delegate void MethodHandler<TResponseType>(Error error, TResponseType response);
78

89
public interface IMethod
910
{
10-
void Callback(Meteor.Error error, object response);
11+
void Callback(Error error, object response);
1112
object UntypedResponse { get; }
12-
Meteor.Error Error { get; }
13+
Error Error { get; }
1314
Type ResponseType { get; }
1415
}
1516
}

DDP/DDPConnector.cs renamed to DDP/LiveConnection.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public void Connect(string url)
2323
_socket.MessageReceived += new EventHandler<MessageReceivedEventArgs>(socket_MessageReceived);
2424
_socket.Opened += new EventHandler(_socket_Opened);
2525
_socket.Open();
26+
2627
_isWait = 1;
2728
this.Wait();
2829
}
@@ -37,7 +38,6 @@ public void Close()
3738

3839
public void Send(string message)
3940
{
40-
Debug.Log (message);
4141
_socket.Send(message);
4242
}
4343

@@ -49,6 +49,7 @@ void _socket_Opened(object sender, EventArgs e)
4949

5050
void socket_MessageReceived(object sender, MessageReceivedEventArgs e)
5151
{
52+
System.Console.WriteLine ("test");
5253
this._client.QueueMessage(e.Message);
5354
}
5455

DDP/LiveConnection.cs.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

DDP/LiveData.cs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,9 @@ namespace Meteor.LiveData
99
{
1010
public class LiveData : ILiveData
1111
{
12-
protected sealed class LiveDataHost : MonoBehaviour {
13-
private static LiveDataHost _instance;
14-
15-
public static LiveDataHost Instance {
16-
get {
17-
if (((object)_instance) == null) {
18-
_instance = (new GameObject ("LiveData Host")).AddComponent<LiveDataHost> ();
19-
}
20-
21-
return _instance;
22-
}
23-
}
24-
}
25-
2612
public LiveConnection Connector;
2713
int uniqueId;
28-
Dictionary<string, Meteor.LiveData.ICollection> collections;
14+
Dictionary<string, ICollection> collections;
2915
Dictionary<string, List<string>> subscriptionsToCollections;
3016
Dictionary<string, IMethod> methods;
3117
string serverId;
@@ -51,7 +37,7 @@ public LiveData()
5137
{
5238
Connector = new LiveConnection(this);
5339
jsonItemsQueue = new Queue<string>();
54-
collections = new Dictionary<string, Meteor.LiveData.ICollection>();
40+
collections = new Dictionary<string, ICollection>();
5541
subscriptionsToCollections = new Dictionary<string, List<string>>();
5642
methods = new Dictionary<string, IMethod>();
5743

@@ -66,23 +52,38 @@ public LiveData()
6652
/// Connect to the specified Meteor server.
6753
/// </summary>
6854
/// <param name="url">URL.</param>
69-
public Coroutine Connect(string url)
55+
public Coroutine Connect(string url, float timeout = 2f)
7056
{
7157
OnConnected += HandleOnConnected;
58+
TimedOut = false;
59+
Connector.Connect(url);
7260

73-
return LiveDataHost.Instance.StartCoroutine (ConnectCoroutine (url));
61+
CoroutineHost.Instance.StartCoroutine (TimeoutCoroutine (timeout));
62+
return CoroutineHost.Instance.StartCoroutine (ConnectCoroutine (url));
7463
}
7564

7665
void HandleOnConnected (string obj)
7766
{
78-
connected = true;
67+
Connected = true;
68+
TimedOut = false;
7969
}
8070

81-
bool connected;
82-
private IEnumerator ConnectCoroutine(string url) {
83-
Connector.Connect(url);
71+
public bool Connected;
72+
73+
public bool TimedOut;
74+
75+
private IEnumerator TimeoutCoroutine(float timeout) {
76+
yield return new WaitForSeconds (timeout);
77+
if (!Connected) {
78+
TimedOut = true;
79+
}
80+
}
8481

85-
while (!connected) {
82+
private IEnumerator ConnectCoroutine(string url) {
83+
while (!Connected) {
84+
if (TimedOut) {
85+
yield break;
86+
}
8687
yield return null;
8788
}
8889

@@ -237,10 +238,9 @@ public void RestartThread()
237238
private void ProcessQueue()
238239
{
239240
while (Dequeue()) {
240-
Debug.Log(currentJsonItem);
241241
IDictionary message = currentJsonItem.Deserialize() as IDictionary;
242242
if (message == null) {
243-
return;
243+
continue;
244244
}
245245

246246
Message m = message.Coerce<Message>();
@@ -289,14 +289,14 @@ private void ProcessQueue()
289289
if (methods.ContainsKey(resultm.id)) {
290290
methods[resultm.id].Callback(resultm.error, resultm.methodResult);
291291
} else {
292-
Debug.LogError ("DDPClient.ProcessQueue: Result ID not found.");
292+
// Debug.LogError ("DDPClient.ProcessQueue: Result ID not found.");
293293
}
294294
break;
295295
case "updated":
296296
break;
297297
default:
298298
if (!message.Contains("server_id")) {
299-
Debug.Log(string.Format("DDPClient.ProcessQueue: Unhandled message.\nMessage:\n{0}",message.Serialize()));
299+
// Debug.Log(string.Format("DDPClient.ProcessQueue: Unhandled message.\nMessage:\n{0}",message.Serialize()));
300300
}
301301
break;
302302
}

DDP/Messages/ConnectMessage.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ public class ConnectMessage : Message
88
const string pre = "pre1";
99
public static string connectMessage;
1010
public string version;
11+
public string[] support;
1112

1213
public ConnectMessage()
1314
{
1415
msg = connect;
1516
version = pre;
17+
support = new[] { "pre1" };
1618
}
1719

1820
static ConnectMessage()

DDP/Messages/ResultMessage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class ResultMessage : Message
66
{
77
[JsonFx.Json.JsonIgnore]
88
public const string result = "result";
9-
public Meteor.Error error;
9+
public Error error;
1010

1111
public string id;
1212

DDP/Method.cs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class Method : IMethod
1010
public MethodMessage Message;
1111
public event MethodHandler OnUntypedResponse;
1212

13-
public virtual void Callback(Meteor.Error error, object response)
13+
public virtual void Callback(Error error, object response)
1414
{
1515
if (OnUntypedResponse != null)
1616
{
@@ -31,26 +31,14 @@ public object UntypedResponse {
3131
protected set;
3232
}
3333

34-
public Meteor.Error Error {
34+
public Error Error {
3535
get;
3636
protected set;
3737
}
3838

3939
#endregion
4040

41-
protected sealed class MethodHost : MonoBehaviour {
42-
private static MethodHost _instance;
43-
44-
public static MethodHost Instance {
45-
get {
46-
if (((object)_instance) == null) {
47-
_instance = (new GameObject ("Method Host")).AddComponent<MethodHost> ();
48-
}
49-
50-
return _instance;
51-
}
52-
}
53-
}
41+
protected sealed class MethodHost : MonoSingleton<MethodHost> {}
5442
}
5543

5644
public class Method<TResponseType> : Method
@@ -71,7 +59,7 @@ public TResponseType Response
7159

7260
#region IMethod implementation
7361

74-
public override void Callback(Meteor.Error error, object response)
62+
public override void Callback(Error error, object response)
7563
{
7664
TResponseType r = response.Coerce<TResponseType>();
7765

@@ -91,10 +79,10 @@ public override Type ResponseType {
9179

9280
public static implicit operator Coroutine(Method<TResponseType> method) {
9381
method.OnResponse += method.completed;
94-
return MethodHost.Instance.StartCoroutine (method.Execute ());
82+
return CoroutineHost.Instance.StartCoroutine (method.Execute ());
9583
}
9684

97-
private void completed(Meteor.Error error, TResponseType response) {
85+
private void completed(Error error, TResponseType response) {
9886
Response = response;
9987
Error = error;
10088
complete = true;

Extensions/MonoSingleton.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using UnityEngine;
2+
3+
/// <summary>
4+
/// Mono singleton. From http://wiki.unity3d.com/index.php?title=Singleton#Generic_Based_Singleton_for_MonoBehaviours
5+
/// </summary>
6+
public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
7+
{
8+
private static T m_Instance = null;
9+
public static T Instance
10+
{
11+
get
12+
{
13+
if(m_Instance) {
14+
return m_Instance;
15+
}
16+
// Instance requiered for the first time, we look for it
17+
else {
18+
m_Instance = GameObject.FindObjectOfType(typeof(T)) as T;
19+
20+
// Object not found, we create a temporary one
21+
if( m_Instance == null )
22+
{
23+
m_Instance = new GameObject(typeof(T).ToString(), typeof(T)).GetComponent<T>();
24+
25+
// Problem during the creation, this should not happen
26+
if( m_Instance == null )
27+
{
28+
Debug.LogError("MonoSingleton: Problem during the creation of " + typeof(T).ToString());
29+
}
30+
}
31+
}
32+
33+
return m_Instance;
34+
}
35+
}
36+
37+
/// <summary>
38+
/// Awake function. Override when necessary and call base.Awake() first.
39+
/// </summary>
40+
protected virtual void Awake()
41+
{
42+
if( m_Instance == null )
43+
{
44+
m_Instance = this as T;
45+
DontDestroyOnLoad(gameObject);
46+
}
47+
}
48+
49+
/// <summary>
50+
/// Clear the reference when the application quits. Override when necessary and call base.OnApplicationQuit() last.
51+
/// </summary>
52+
protected virtual void OnApplicationQuit()
53+
{
54+
m_Instance = null;
55+
}
56+
}
57+

Extensions/MonoSingleton.cs.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Services/Accounts/Accounts.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ public static Collection<Meteor.MongoDocument> Users {
1212
private set;
1313
}
1414

15+
public static string UserId {
16+
get;
17+
private set;
18+
}
19+
1520
static Accounts() {
1621
// FacebookManager.sessionOpenedEvent += HandleFacebookSessionOpened;
1722
//
@@ -75,6 +80,7 @@ static void HandleOnLogin (Meteor.Error error, LoginUserResult response)
7580
{
7681
if (error == null) {
7782
PlayerPrefs.SetString (TokenKey, response.token);
83+
UserId = response.id;
7884
SubscribeToUsers ();
7985
}
8086
}

Services/Meteor.meta

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Services/Meteor/Meteor.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using Meteor.LiveData;
3+
using UnityEngine;
4+
5+
namespace Meteor
6+
{
7+
public static class Meteor
8+
{
9+
public static bool IsConnected {
10+
get {
11+
return LiveData.LiveData.Instance.Connected;
12+
}
13+
}
14+
15+
public static Coroutine Connect(string url) {
16+
return LiveData.LiveData.Instance.Connect (url);
17+
}
18+
}
19+
}
20+

0 commit comments

Comments
 (0)