Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use Newtonsoft #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ pomelo-unityclient-socket
This is the pomelo dotnet client, support pomelo 0.3 and the new communicate protocol.It is based on native socket.
The project is based on some libraries as follows:

* [simple-json](https://github.com/facebook-csharp-sdk/simple-json) An open source json library
* [simple-json](https://github.com/facebook-csharp-sdk/simple-json) An open source json library Delete !!
* [newtonsoft-json] [email protected] Add

## Demo

Expand Down
Binary file added lib/Newtonsoft.Json.dll
Binary file not shown.
Binary file removed lib/SimpleJson.dll
Binary file not shown.
27 changes: 14 additions & 13 deletions pomelo-dotnetClient/src/client/EventManager.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
using System;
using System.Collections.Generic;
using System.Text;
using SimpleJson;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Pomelo.DotNetClient
{
public class EventManager : IDisposable
{
private Dictionary<uint, Action<JsonObject>> callBackMap;
private Dictionary<string, List<Action<JsonObject>>> eventMap;
private Dictionary<uint, Action<JObject>> callBackMap;
private Dictionary<string, List<Action<JObject>>> eventMap;

public EventManager()
{
this.callBackMap = new Dictionary<uint, Action<JsonObject>>();
this.eventMap = new Dictionary<string, List<Action<JsonObject>>>();
this.callBackMap = new Dictionary<uint, Action<JObject>>();
this.eventMap = new Dictionary<string, List<Action<JObject>>>();
}

//Adds callback to callBackMap by id.
public void AddCallBack(uint id, Action<JsonObject> callback)
public void AddCallBack(uint id, Action<JObject> callback)
{
if (id > 0 && callback != null)
{
Expand All @@ -31,23 +32,23 @@ public void AddCallBack(uint id, Action<JsonObject> callback)
/// <param name='pomeloMessage'>
/// Pomelo message.
/// </param>
public void InvokeCallBack(uint id, JsonObject data)
public void InvokeCallBack(uint id, JObject data)
{
if (!callBackMap.ContainsKey(id)) return;
callBackMap[id].Invoke(data);
}

//Adds the event to eventMap by name.
public void AddOnEvent(string eventName, Action<JsonObject> callback)
public void AddOnEvent(string eventName, Action<JObject> callback)
{
List<Action<JsonObject>> list = null;
List<Action<JObject>> list = null;
if (this.eventMap.TryGetValue(eventName, out list))
{
list.Add(callback);
}
else
{
list = new List<Action<JsonObject>>();
list = new List<Action<JObject>>();
list.Add(callback);
this.eventMap.Add(eventName, list);
}
Expand All @@ -60,12 +61,12 @@ public void AddOnEvent(string eventName, Action<JsonObject> callback)
/// <param name="value"></param>
/// <returns></returns>
///
public void InvokeOnEvent(string route, JsonObject msg)
public void InvokeOnEvent(string route, JObject msg)
{
if (!this.eventMap.ContainsKey(route)) return;

List<Action<JsonObject>> list = eventMap[route];
foreach (Action<JsonObject> action in list) action.Invoke(msg);
List<Action<JObject>> list = eventMap[route];
foreach (Action<JObject> action in list) action.Invoke(msg);
}

// Dispose() calls Dispose(true)
Expand Down
19 changes: 10 additions & 9 deletions pomelo-dotnetClient/src/client/PomeloClient.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using SimpleJson;
using Newtonsoft.Json;
using System;
using System.ComponentModel;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Newtonsoft.Json.Linq;

namespace Pomelo.DotNetClient
{
Expand Down Expand Up @@ -150,17 +151,17 @@ public void connect()
connect(null, null);
}

public void connect(JsonObject user)
public void connect(JObject user)
{
connect(user, null);
}

public void connect(Action<JsonObject> handshakeCallback)
public void connect(Action<JObject> handshakeCallback)
{
connect(null, handshakeCallback);
}

public bool connect(JsonObject user, Action<JsonObject> handshakeCallback)
public bool connect(JObject user, Action<JObject> handshakeCallback)
{
try
{
Expand All @@ -174,26 +175,26 @@ public bool connect(JsonObject user, Action<JsonObject> handshakeCallback)
}
}

private JsonObject emptyMsg = new JsonObject();
public void request(string route, Action<JsonObject> action)
private JObject emptyMsg = new JObject();
public void request(string route, Action<JObject> action)
{
this.request(route, emptyMsg, action);
}

public void request(string route, JsonObject msg, Action<JsonObject> action)
public void request(string route, JObject msg, Action<JObject> action)
{
this.eventManager.AddCallBack(reqId, action);
protocol.send(route, reqId, msg);

reqId++;
}

public void notify(string route, JsonObject msg)
public void notify(string route, JObject msg)
{
protocol.send(route, msg);
}

public void on(string eventName, Action<JsonObject> action)
public void on(string eventName, Action<JObject> action)
{
eventManager.AddOnEvent(eventName, action);
}
Expand Down
17 changes: 10 additions & 7 deletions pomelo-dotnetClient/src/client/test/ClientTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using SimpleJson;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;


namespace Pomelo.DotNetClient.Test
{
Expand All @@ -23,14 +25,15 @@ public static void loginTest(string host, int port)
{

Console.WriteLine("on data back" + data.ToString());
JsonObject msg = new JsonObject();

JObject msg = new JObject();
msg["uid"] = 111;
pc.request("gate.gateHandler.queryEntry", msg, OnQuery);
});
});
}

public static void OnQuery(JsonObject result)
public static void OnQuery(JObject result)
{
if (Convert.ToInt32(result["code"]) == 200)
{
Expand All @@ -49,11 +52,11 @@ public static void OnQuery(JsonObject result)
{
pc.connect(null, (data) =>
{
JsonObject userMessage = new JsonObject();
JObject userMessage = new JObject();
Console.WriteLine("on connect to connector!");

//Login
JsonObject msg = new JsonObject();
JObject msg = new JObject();
msg["username"] = "test";
msg["rid"] = "pomelo";

Expand All @@ -63,12 +66,12 @@ public static void OnQuery(JsonObject result)
}
}

public static void OnEnter(JsonObject result)
public static void OnEnter(JObject result)
{
Console.WriteLine("on login " + result.ToString());
}

public static void onDisconnect(JsonObject result)
public static void onDisconnect(JObject result)
{
Console.WriteLine("on sockect disconnected!");
}
Expand Down
71 changes: 36 additions & 35 deletions pomelo-dotnetClient/src/protobuf/MsgDecoder.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
using System;
using System.Text;
using SimpleJson;
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;

namespace Pomelo.Protobuf
{
public class MsgDecoder
{
private JsonObject protos { set; get; }//The message format(like .proto file)
private JObject protos { set; get; }//The message format(like .proto file)
private int offset { set; get; }
private byte[] buffer { set; get; }//The binary message from server.
private Util util { set; get; }

public MsgDecoder(JsonObject protos)
public MsgDecoder(JObject protos)
{
if (protos == null) protos = new JsonObject();
if (protos == null) protos = new JObject();

this.protos = protos;
this.util = new Util();
Expand All @@ -28,17 +29,17 @@ public MsgDecoder(JsonObject protos)
/// Route.
/// </param>
/// <param name='buf'>
/// JsonObject.
/// JObject.
/// </param>
public JsonObject decode(string route, byte[] buf)
public JObject decode(string route, byte[] buf)
{
this.buffer = buf;
this.offset = 0;
object proto = null;
JToken proto = null;
if (this.protos.TryGetValue(route, out proto))
{
JsonObject msg = new JsonObject();
return this.decodeMsg(msg, (JsonObject)proto, this.buffer.Length);
JObject msg = new JObject();
return this.decodeMsg(msg, (JObject)proto, this.buffer.Length);
}
return null;
}
Expand All @@ -51,54 +52,54 @@ public JsonObject decode(string route, byte[] buf)
/// The message.
/// </returns>
/// <param name='msg'>
/// JsonObject.
/// JObject.
/// </param>
/// <param name='proto'>
/// JsonObject.
/// JObject.
/// </param>
/// <param name='length'>
/// int.
/// </param>
private JsonObject decodeMsg(JsonObject msg, JsonObject proto, int length)
private JObject decodeMsg(JObject msg, JObject proto, int length)
{
while (this.offset < length)
{
Dictionary<string, int> head = this.getHead();
int tag;
if (head.TryGetValue("tag", out tag))
{
object _tags = null;
JToken _tags = null;
if (proto.TryGetValue("__tags", out _tags))
{
object name;
if (((JsonObject)_tags).TryGetValue(tag.ToString(), out name))
JToken name;
if (((JObject)_tags).TryGetValue(tag.ToString(), out name))
{
object value;
JToken value;
if (proto.TryGetValue(name.ToString(), out value))
{
object option;
if (((JsonObject)(value)).TryGetValue("option", out option))
JToken option;
if (((JObject)(value)).TryGetValue("option", out option))
{
switch (option.ToString())
{
case "optional":
case "required":
object type;
if (((JsonObject)(value)).TryGetValue("type", out type))
JToken type;
if (((JObject)(value)).TryGetValue("type", out type))
{
msg.Add(name.ToString(), this.decodeProp(type.ToString(), proto));
msg.Add(name.ToString(), proto);
}
break;
case "repeated":
object _name;
JToken _name;
if (!msg.TryGetValue(name.ToString(), out _name))
{
msg.Add(name.ToString(), new List<object>());
msg.Add(name.ToString(), new JArray());
}
object value_type;
if (msg.TryGetValue(name.ToString(), out _name) && ((JsonObject)(value)).TryGetValue("type", out value_type))
JToken value_type;
if (msg.TryGetValue(name.ToString(), out _name) && ((JObject)(value)).TryGetValue("type", out value_type))
{
decodeArray((List<object>)_name, value_type.ToString(), proto);
decodeArray((JArray)_name, value_type.ToString(), proto);
}
break;
}
Expand All @@ -114,7 +115,7 @@ private JsonObject decodeMsg(JsonObject msg, JsonObject proto, int length)
/// <summary>
/// Decode array in message.
/// </summary>
private void decodeArray(List<object> list, string type, JsonObject proto)
private void decodeArray(JArray list, string type, JObject proto)
{
if (this.util.isSimpleType(type))
{
Expand All @@ -133,7 +134,7 @@ private void decodeArray(List<object> list, string type, JsonObject proto)
/// <summary>
/// Decode each simple type in message.
/// </summary>
private object decodeProp(string type, JsonObject proto)
private object decodeProp(string type, JObject proto)
{
switch (type)
{
Expand All @@ -154,23 +155,23 @@ private object decodeProp(string type, JsonObject proto)
}

//Decode the user-defined object type in message.
private JsonObject decodeObject(string type, JsonObject proto)
private JObject decodeObject(string type, JObject proto)
{
if (proto != null)
{
object __messages;
JToken __messages;
if (proto.TryGetValue("__messages", out __messages))
{
object _type;
if (((JsonObject)__messages).TryGetValue(type, out _type) || protos.TryGetValue("message " + type, out _type))
JToken _type;
if (((JObject)__messages).TryGetValue(type, out _type) || protos.TryGetValue("message " + type, out _type))
{
int l = (int)Decoder.decodeUInt32(this.getBytes());
JsonObject msg = new JsonObject();
return this.decodeMsg(msg, (JsonObject)_type, this.offset + l);
JObject msg = new JObject();
return this.decodeMsg(msg, (JObject)_type, this.offset + l);
}
}
}
return new JsonObject();
return new JObject();
}

//Decode string type.
Expand Down
Loading