diff --git a/pomelo-dotnetClient/pomelo-dotnetClient.csproj b/pomelo-dotnetClient/pomelo-dotnetClient.csproj index f6c9bd4..acb23b4 100755 --- a/pomelo-dotnetClient/pomelo-dotnetClient.csproj +++ b/pomelo-dotnetClient/pomelo-dotnetClient.csproj @@ -14,7 +14,7 @@ true full false - bin\Debug + ..\dist\ DEBUG; prompt 4 diff --git a/pomelo-dotnetClient/src/client/PomeloClient.cs b/pomelo-dotnetClient/src/client/PomeloClient.cs index 3b10495..873cce1 100644 --- a/pomelo-dotnetClient/src/client/PomeloClient.cs +++ b/pomelo-dotnetClient/src/client/PomeloClient.cs @@ -39,7 +39,7 @@ public class PomeloClient : IDisposable public event Action NetWorkStateChangedEvent; - private NetWorkState netWorkState = NetWorkState.CLOSED; //current network state + public NetWorkState netWorkState {get; private set;} //current network state private EventManager eventManager; private Socket socket; @@ -48,10 +48,15 @@ public class PomeloClient : IDisposable private uint reqId = 1; private ManualResetEvent timeoutEvent = new ManualResetEvent(false); + private int timeoutMSec = 8000; //connect timeout count in millisecond - public PomeloClient() + private bool _keepWaitWhenConnect = true; + + public PomeloClient(bool keepWaitWhenConnect = true) { + _keepWaitWhenConnect = keepWaitWhenConnect; + netWorkState = NetWorkState.CLOSED; } /// @@ -70,15 +75,20 @@ public void initClient(string host, int port, Action callback = null) try { - IPAddress[] addresses = Dns.GetHostEntry(host).AddressList; - foreach (var item in addresses) + if (!IPAddress.TryParse(host, out ipAddress)) { - if (item.AddressFamily == AddressFamily.InterNetwork) + var addresses = Dns.GetHostEntry(host).AddressList; + + foreach (var item in addresses) { - ipAddress = item; - break; + if (item.AddressFamily == AddressFamily.InterNetwork) + { + ipAddress = item; + break; + } } } + } catch (Exception e) { @@ -121,7 +131,7 @@ public void initClient(string host, int port, Action callback = null) } }), this.socket); - if (timeoutEvent.WaitOne(timeoutMSec, false)) + if (_keepWaitWhenConnect && timeoutEvent.WaitOne(timeoutMSec, false)) { if (netWorkState != NetWorkState.CONNECTED && netWorkState != NetWorkState.ERROR) { diff --git a/pomelo-dotnetClient/src/protobuf/MsgDecoder.cs b/pomelo-dotnetClient/src/protobuf/MsgDecoder.cs index 18a7b47..8d4e93f 100755 --- a/pomelo-dotnetClient/src/protobuf/MsgDecoder.cs +++ b/pomelo-dotnetClient/src/protobuf/MsgDecoder.cs @@ -93,12 +93,12 @@ private JsonObject decodeMsg(JsonObject msg, JsonObject proto, int length) object _name; if (!msg.TryGetValue(name.ToString(), out _name)) { - msg.Add(name.ToString(), new List()); + msg.Add(name.ToString(), new JsonArray()); } object value_type; if (msg.TryGetValue(name.ToString(), out _name) && ((JsonObject)(value)).TryGetValue("type", out value_type)) { - decodeArray((List)_name, value_type.ToString(), proto); + decodeArray((JsonArray)_name, value_type.ToString(), proto); } break; } @@ -148,6 +148,8 @@ private object decodeProp(string type, JsonObject proto) return this.decodeDouble(); case "string": return this.decodeString(); + case "object": + return SimpleJson.SimpleJson.DeserializeObject(this.decodeString()); default: return this.decodeObject(type, proto); } diff --git a/pomelo-dotnetClient/src/protobuf/MsgEncoder.cs b/pomelo-dotnetClient/src/protobuf/MsgEncoder.cs index 4bba9df..83cbe40 100755 --- a/pomelo-dotnetClient/src/protobuf/MsgEncoder.cs +++ b/pomelo-dotnetClient/src/protobuf/MsgEncoder.cs @@ -146,9 +146,9 @@ private int encodeMsg(byte[] buffer, int offset, JsonObject proto, JsonObject ms object msg_key; if (msg.TryGetValue(key, out msg_key)) { - if (((List)msg_key).Count > 0) + if (((JsonArray)msg_key).Count > 0) { - offset = encodeArray((List)msg_key, (JsonObject)value, offset, buffer, proto); + offset = encodeArray((JsonArray)msg_key, (JsonObject)value, offset, buffer, proto); } } break; @@ -163,7 +163,7 @@ private int encodeMsg(byte[] buffer, int offset, JsonObject proto, JsonObject ms /// /// Encode the array type. /// - private int encodeArray(List msg, JsonObject value, int offset, byte[] buffer, JsonObject proto) + private int encodeArray(JsonArray msg, JsonObject value, int offset, byte[] buffer, JsonObject proto) { object value_type, value_tag; if (value.TryGetValue("type", out value_type) && value.TryGetValue("tag", out value_tag)) @@ -209,6 +209,7 @@ private int encodeProp(object value, string type, int offset, byte[] buffer, Jso case "double": this.writeDouble(buffer, ref offset, value); break; + case "object": case "string": this.writeString(buffer, ref offset, value); break;