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

Protobuf BUG + 可选是否堵塞 #25

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion pomelo-dotnetClient/pomelo-dotnetClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<OutputPath>..\dist\</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
Expand Down
26 changes: 18 additions & 8 deletions pomelo-dotnetClient/src/client/PomeloClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class PomeloClient : IDisposable
public event Action<NetWorkState> NetWorkStateChangedEvent;


private NetWorkState netWorkState = NetWorkState.CLOSED; //current network state
public NetWorkState netWorkState {get; private set;} //current network state

private EventManager eventManager;
private Socket socket;
Expand All @@ -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;
}

/// <summary>
Expand All @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down
6 changes: 4 additions & 2 deletions pomelo-dotnetClient/src/protobuf/MsgDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<object>());
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<object>)_name, value_type.ToString(), proto);
decodeArray((JsonArray)_name, value_type.ToString(), proto);
}
break;
}
Expand Down Expand Up @@ -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);
}
Expand Down
7 changes: 4 additions & 3 deletions pomelo-dotnetClient/src/protobuf/MsgEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<object>)msg_key).Count > 0)
if (((JsonArray)msg_key).Count > 0)
{
offset = encodeArray((List<object>)msg_key, (JsonObject)value, offset, buffer, proto);
offset = encodeArray((JsonArray)msg_key, (JsonObject)value, offset, buffer, proto);
}
}
break;
Expand All @@ -163,7 +163,7 @@ private int encodeMsg(byte[] buffer, int offset, JsonObject proto, JsonObject ms
/// <summary>
/// Encode the array type.
/// </summary>
private int encodeArray(List<object> 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))
Expand Down Expand Up @@ -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;
Expand Down