-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameClient.java
More file actions
162 lines (138 loc) · 4.17 KB
/
Copy pathGameClient.java
File metadata and controls
162 lines (138 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package httpTest;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import com.google.protobuf.MessageLite;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
public class GameClient {
private String uri;
private String sessionId;
private Map<Integer, ResponseHandler> handlers = new HashMap<>();
private ResponseHandler errHandler;
public final int MaxDataLen = 8 * 1024 * 1024;
public final int MaxMessageLen = 4 * 1024 * 1024;
public GameClient(String ip, int port) {
this.uri = "http://" + ip + ":" + port;
}
public GameClient(String uri) {
this.uri = uri;
}
public void send(int headId, MessageLite proto) {
try {
send0(headId, proto);
} catch (Exception e) {
e.printStackTrace();
}
}
private void send0(int headId, MessageLite proto) throws Exception {
// 创建一个byteBuf
ByteBuf buf = Unpooled.buffer();
if (sessionId != null) {
byte[] sessionData = sessionId.getBytes("utf-8");
buf.writeByte(sessionData.length);
buf.writeBytes(sessionData);
} else {
buf.writeByte(0);
}
buf.writeInt(headId);
if (proto != null) {
byte[] protodata = proto.toByteArray();
ResponseHandler handler = handlers.get(headId);
if (handler != null) {
if (handler.isSystem()) {
protodata = RSAUtils.encryptByPublicKey(protodata);
} else if (handler.isEncrypted() && sessionId != null) {
byte[] sessionIdEncrypt = sessionId.getBytes();
MsgUtils.encrypt(sessionIdEncrypt, MsgUtils.otherKey);
MsgUtils.encrypt(protodata, sessionIdEncrypt);
}
}
buf.writeBytes(protodata);
}
int len = buf.writerIndex();
byte[] data = new byte[len];
buf.getBytes(0, data); // 复制了一份 buf 的数据到 data
HttpUtils.create(uri).post(data, new HttpResponseHandler() {
@Override
public void handler(HttpResponse resp) {
HttpEntity entity = resp.getEntity();
if (!entity.isStreaming())
return;
int totalLen = (int) entity.getContentLength();
if (totalLen == 0)
return;
if (totalLen > MaxDataLen) {
throw new RuntimeException("");
}
byte[] data = new byte[totalLen];
try {
InputStream is = entity.getContent();
BufferedInputStream bis = new BufferedInputStream(is);
int offset = 0;
byte[] temp = new byte[1024];
while (true) {
int size = bis.read(temp);
if (size <= 0) {
break;
}
System.arraycopy(temp, 0, data, offset, size);
offset += size;
}
ByteBuf buf = Unpooled.wrappedBuffer(data);
while (true) {
int partLen = buf.readInt();
int code = buf.readByte();
int headId = buf.readInt();
if (partLen > MaxMessageLen) {
throw new RuntimeException();
}
byte[] protodata = new byte[partLen - 5];
buf.readBytes(protodata);// 包含了headId
ResponseHandler handler = handlers.get(headId);
if (handler != null) {
if (handler.isSystem()) {
} else if (handler.isEncrypted() && sessionId != null) {
byte[] sessionIdEncrypt = sessionId.getBytes();
MsgUtils.encrypt(sessionIdEncrypt, MsgUtils.otherKey);
MsgUtils.encrypt(protodata, sessionIdEncrypt);
}
}
Response r = new Response(code == 0, headId, protodata);
if (code == 0) {
if (handler != null) {
try {
handler.handle(r);
} catch (Exception e) {
e.printStackTrace();
}
}
} else {
if (errHandler != null) {
errHandler.handle(r);
}
}
if (buf.readerIndex() == totalLen) {
break;
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
}
public static class Response {
public boolean isSuccess;
public int headId;
public byte[] protobufData;
public Response(boolean isSuccess, int headId, byte[] protobufData) {
this.isSuccess = isSuccess;
this.headId = headId;
this.protobufData = protobufData;
}
}
}