Skip to content

Commit a945cd0

Browse files
committed
fix bug for handshake and protobuf decode
1 parent 8a5ef90 commit a945cd0

File tree

9 files changed

+81
-26
lines changed

9 files changed

+81
-26
lines changed

pomelo-dotnetClient.userprefs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
<Properties>
2-
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug|x86" PreferredExecutionTarget="MonoDevelop.Default" />
2+
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug|x86" />
33
<MonoDevelop.Ide.Workbench ActiveDocument="pomelo-dotnetClient\src\test\Test.cs">
44
<Files>
5-
<File FileName="pomelo-dotnetClient\src\client\PomeloClient.cs" Line="53" Column="45" />
6-
<File FileName="pomelo-dotnetClient\src\client\test\ClientTest.cs" Line="38" Column="4" />
7-
<File FileName="pomelo-dotnetClient\src\test\Test.cs" Line="14" Column="6" />
8-
<File FileName="pomelo-dotnetClient\src\transport\Transporter.cs" Line="53" Column="3" />
9-
<File FileName="d:\workspace\pomelo-unityclient-socket\pomelo-dotnetClient\src\protocol\HandShakeService.cs" Line="34" Column="1" />
10-
<File FileName="d:\workspace\pomelo-unityclient-socket\pomelo-dotnetClient\src\protocol\Protocol.cs" Line="132" Column="1" />
11-
<File FileName="d:\workspace\pomelo-unityclient-socket\pomelo-dotnetClient\src\protocol\MessageProtocol.cs" Line="23" Column="30" />
12-
<File FileName="d:\workspace\pomelo-unityclient-socket\pomelo-dotnetClient\src\protocol\HeartBeatService.cs" Line="33" Column="1" />
5+
<File FileName="pomelo-dotnetClient\src\client\test\ClientTest.cs" Line="45" Column="4" />
6+
<File FileName="pomelo-dotnetClient\src\test\Test.cs" Line="12" Column="37" />
7+
<File FileName="d:\workspace\pomelo-unityclient-socket\pomelo-dotnetClient\src\protocol\HandShakeService.cs" Line="29" Column="4" />
8+
<File FileName="d:\workspace\pomelo-unityclient-socket\pomelo-dotnetClient\src\protocol\Protocol.cs" Line="17" Column="19" />
9+
<File FileName="d:\workspace\pomelo-unityclient-socket\pomelo-dotnetClient\src\protocol\MessageProtocol.cs" Line="160" Column="25" />
10+
<File FileName="d:\workspace\pomelo-unityclient-socket\pomelo-dotnetClient\src\protocol\HeartBeatService.cs" Line="34" Column="21" />
11+
<File FileName="d:\workspace\pomelo-unityclient-socket\pomelo-dotnetClient\src\test\transportTest\TransportTest.cs" Line="63" Column="4" />
12+
<File FileName="d:\workspace\pomelo-unityclient-socket\pomelo-dotnetClient\src\transport\Transporter.cs" Line="88" Column="1" />
13+
<File FileName="d:\workspace\pomelo-unityclient-socket\pomelo-dotnetClient\src\protobuf\test\ProtobufTest.cs" Line="21" Column="39" />
14+
<File FileName="pomelo-dotnetClient\src\protobuf\MsgDecoder.cs" Line="61" Column="1" />
15+
<File FileName="pomelo-dotnetClient\src\protobuf\MsgEncoder.cs" Line="178" Column="24" />
16+
<File FileName="d:\workspace\pomelo-unityclient-socket\pomelo-dotnetClient\src\protobuf\Protobuf.cs" Line="22" Column="1" />
17+
<File FileName="pomelo-dotnetClient\src\client\PomeloClient.cs" Line="101" Column="17" />
1318
</Files>
1419
<Pads>
1520
<Pad Id="ProjectPad">
@@ -37,9 +42,10 @@
3742
</Pad>
3843
<Pad Id="MonoDevelop.Debugger.WatchPad">
3944
<State>
40-
<Value>buffer[0]</Value>
41-
<Value>buffer[1]</Value>
42-
<Value>name</Value>
45+
<Value>bytes[0]</Value>
46+
<Value>bytes[1]</Value>
47+
<Value>m</Value>
48+
<Value>n</Value>
4349
</State>
4450
</Pad>
4551
</Pads>
512 Bytes
Binary file not shown.

pomelo-dotnetClient/src/client/test/ClientTest.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,21 @@ public static void OnQuery(JsonObject result){
3333
pc.connect(null, (data)=>{
3434
JsonObject userMessage = new JsonObject();
3535
Console.WriteLine("on connect to connector!");
36+
37+
//Login
38+
JsonObject msg = new JsonObject();
39+
msg["username"] = "test";
40+
msg["rid"] = "pomelo";
41+
42+
pc.request("connector.entryHandler.enter", msg, OnEnter);
3643
});
3744
}
3845
}
3946

47+
public static void OnEnter(JsonObject result){
48+
Console.WriteLine("on login " + result.ToString());
49+
}
50+
4051
public static void Run(){
4152
string host = "127.0.0.1";
4253
int port = 3014;

pomelo-dotnetClient/src/protobuf/MsgEncoder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ private int encodeProp(object value, string type, int offset, byte [] buffer, Js
179179
object __message_type;
180180
if (proto.TryGetValue("__messages", out __messages)) {
181181
if (((JsonObject)__messages).TryGetValue(type, out __message_type)) {
182-
byte [] tembuff = new byte[Encoder.byteLength(value.ToString())];
182+
byte [] tembuff = new byte[Encoder.byteLength(value.ToString())*2];
183183
int length = 0;
184184
length = this.encodeMsg(tembuff, length, (JsonObject)__message_type, (JsonObject)value);
185185
offset = writeBytes(buffer, offset, Encoder.encodeUInt32((uint)length));

pomelo-dotnetClient/src/protocol/HeartBeatService.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,29 @@ public class HeartBeatService
88
int interval;
99
public int timeout;
1010
Timer timer;
11+
DateTime lastTime;
1112

1213
Protocol protocol;
1314

14-
public HeartBeatService(int timeout, Protocol protocol){
15-
this.interval = timeout*1000;
15+
public HeartBeatService(int interval, Protocol protocol){
16+
this.interval = interval*1000;
1617
this.protocol = protocol;
1718
}
1819

1920
internal void resetTimeout(){
2021
this.timeout = 0;
22+
lastTime = DateTime.Now;
2123
}
2224

2325
public void sendHeartBeat(object source, ElapsedEventArgs e){
2426
//check timeout
2527
if(timeout > interval*2){
26-
protocol.close();
28+
protocol.getPomeloClient().disconnect();
2729
}
2830

31+
TimeSpan span = DateTime.Now - lastTime;
32+
timeout += (int)span.TotalMilliseconds;
33+
2934
//Send heart beat
3035
protocol.send(PackageType.PKG_HEARTBEAT);
3136
}
@@ -41,6 +46,7 @@ public void start(){
4146

4247
//Set timeout
4348
timeout = 0;
49+
lastTime = DateTime.Now;
4450
}
4551

4652
public void stop(){

pomelo-dotnetClient/src/protocol/MessageProtocol.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public Message decode(byte[] buffer){
151151

152152
JsonObject msg;
153153
if (decodeProtos.ContainsKey(route)) {
154-
msg = protobuf.decode (route, buffer);
154+
msg = protobuf.decode (route, body);
155155
} else {
156156
msg = (JsonObject)SimpleJson.SimpleJson.DeserializeObject(Encoding.UTF8.GetString(body));
157157
}

pomelo-dotnetClient/src/protocol/Protocol.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ public class Protocol
1313
private HeartBeatService heartBeatService;
1414
private PomeloClient pc;
1515

16+
public PomeloClient getPomeloClient(){
17+
return this.pc;
18+
}
19+
1620
public Protocol(PomeloClient pc, System.Net.Sockets.Socket socket){
1721
this.pc = pc;
1822
this.transporter = new Transporter (socket, this.processMessage);

pomelo-dotnetClient/src/test/Test.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ public class Test
88
{
99
public static void Main()
1010
{
11+
byte[] bytes = Pomelo.Protobuf.Encoder.encodeUInt32(112321);
12+
Console.WriteLine(Pomelo.Protobuf.Decoder.decodeUInt32(bytes));
1113
CodecTest.Run();
12-
ProtobufTest.Run();
14+
//ProtobufTest.Run();
1315
TransportTest.Run();
1416
//ClientTest.Run();
1517
}

pomelo-dotnetClient/src/test/transportTest/TransportTest.cs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Pomelo.DotNetClient;
22
using System;
33
using System.Collections.Generic;
4+
using SimpleJson;
45

56
namespace Pomelo.DotNetClient.Test
67
{
@@ -9,24 +10,38 @@ public class TransportTest
910
static List<byte[]> result = new List<byte[]>();
1011

1112
public static byte[] genBuffer(int count){
12-
byte[] buffer = new byte[count + 4];
13+
int size = count + 6;
14+
byte[] buffer = new byte[count + 10];
1315

1416
//Transporter tp = new Transporter(new Object(), protocol);
1517
buffer[0] = (byte)PackageType.PKG_DATA;
16-
buffer[1] = Convert.ToByte(count>>16 & 0xFF);
17-
buffer[2] = Convert.ToByte(count>>8 & 0xFF);
18-
buffer[3] = Convert.ToByte(count & 0xFF);
19-
18+
buffer[1] = Convert.ToByte(size>>16 & 0xFF);
19+
buffer[2] = Convert.ToByte(size>>8 & 0xFF);
20+
buffer[3] = Convert.ToByte(size & 0xFF);
21+
22+
//Generate message head
23+
//Response
24+
buffer[4] = 4;
25+
buffer[5] = 128;
26+
buffer[6] = 1;
27+
28+
//Route length
29+
buffer[7] = 2;
30+
31+
//Route
32+
buffer[8] = 62;
33+
buffer[9] = 72;
34+
2035
Random random = new Random();
21-
for(var i = 0; i < count; i++) buffer[4 + i] = (byte)random.Next(255);
36+
for(var i = 0; i < count; i++) buffer[10 + i] = (byte)random.Next(255);
2237

2338
return buffer;
2439
}
2540

2641
public static byte[] generateBuffers(int num, out List<byte[]> list){
2742
int length = 100;
2843
int index = 0;
29-
byte[] result = new byte[(length + 4)*num];
44+
byte[] result = new byte[(length + 10)*num];
3045
list = new List<byte[]>();
3146

3247
for(int i = 0; i < num; i++){
@@ -50,7 +65,7 @@ public static void Run(){
5065

5166
int offset = 0;
5267
while(offset < buffer.Length){
53-
int length = 1;
68+
int length = 200;
5469
length = (offset + length)> buffer.Length? buffer.Length - offset: length;
5570

5671
tc.processBytes(buffer, offset, offset + length);
@@ -68,6 +83,17 @@ public static void process(byte[] bytes){
6883
result.Add (bytes);
6984
//Console.WriteLine("add bytes : {0}", result.Count);
7085
}
86+
87+
public static void protocolProcess(byte[] bytes){
88+
JsonObject dict = new JsonObject();
89+
JsonObject serverProtos = new JsonObject();
90+
JsonObject clientProtos = new JsonObject();
91+
92+
MessageProtocol messageProtocol = new MessageProtocol (dict, serverProtos, clientProtos);
93+
Package pkg = PackageProtocol.decode(bytes);
94+
95+
messageProtocol.decode (pkg.body);
96+
}
7197

7298
public static bool check(List<byte[]> list){
7399
byte[][] origin = list.ToArray();

0 commit comments

Comments
 (0)