1
1
package org .wowtools .hppt .common .client ;
2
2
3
- import com .google .protobuf .ByteString ;
4
3
import lombok .extern .slf4j .Slf4j ;
5
4
import org .wowtools .hppt .common .pojo .SessionBytes ;
6
- import org .wowtools .hppt .common .protobuf . ProtoMessage ;
5
+ import org .wowtools .hppt .common .pojo . TalkMessage ;
7
6
import org .wowtools .hppt .common .util .AesCipherUtil ;
8
- import org .wowtools .hppt .common .util .BytesUtil ;
9
7
import org .wowtools .hppt .common .util .CommonConfig ;
10
8
import org .wowtools .hppt .common .util .Constant ;
9
+ import org .wowtools .hppt .common .util .DebugConfig ;
11
10
12
11
import java .nio .charset .StandardCharsets ;
13
12
import java .util .LinkedList ;
@@ -56,7 +55,7 @@ public static byte[] buildSendToServerBytes(CommonConfig config, long maxSendBod
56
55
wait = false ;
57
56
}
58
57
//bytes
59
- List <ProtoMessage . BytesPb > bytesPbList = new LinkedList <>();
58
+ List <SessionBytes > bytesPbList = new LinkedList <>();
60
59
do {
61
60
if (sendBodySize >= maxSendBodySize ) {
62
61
break ;
@@ -72,24 +71,23 @@ public static byte[] buildSendToServerBytes(CommonConfig config, long maxSendBod
72
71
break ;
73
72
}
74
73
sendBodySize += bytes .getBytes ().length ;
75
- bytesPbList .add (ProtoMessage .BytesPb .newBuilder ()
76
- .setBytes (ByteString .copyFrom (bytes .getBytes ()))
77
- .setSessionId (bytes .getSessionId ())
78
- .build ());
74
+ bytesPbList .add (bytes );
79
75
} while (true );
80
76
81
77
if (sendBodySize == 0 ) {
82
78
return null ;
83
79
}
84
- ProtoMessage .MessagePb .Builder rBuilder = ProtoMessage .MessagePb .newBuilder ();
85
- if (!commands .isEmpty ()) {
86
- rBuilder .addAllCommandList (commands );
87
- }
88
- if (!bytesPbList .isEmpty ()) {
89
- rBuilder .addAllBytesPbList (bytesPbList );
80
+ if (DebugConfig .OpenSerialNumber ) {
81
+ for (SessionBytes sessionBytes : bytesPbList ) {
82
+ log .debug ("ClientTalker收集 >sessionBytes-SerialNumber {}" , sessionBytes .getSerialNumber ());
83
+ }
90
84
}
91
85
92
- byte [] bytes = rBuilder .build ().toByteArray ();
86
+ TalkMessage talkMessage = new TalkMessage (bytesPbList , commands );
87
+ if (DebugConfig .OpenSerialNumber ) {
88
+ log .debug ("ClientTalker组装 >talkMessage-SerialNumber {}" , talkMessage .getSerialNumber ());
89
+ }
90
+ byte [] bytes = talkMessage .toProto ().build ().toByteArray ();
93
91
//加密
94
92
if (config .enableEncrypt ) {
95
93
bytes = aesCipherUtil .encryptor .encrypt (bytes );
@@ -106,14 +104,17 @@ public static boolean receiveServerBytes(CommonConfig config, byte[] responseBod
106
104
if (null == responseBody ) {
107
105
return true ;
108
106
}
109
- ProtoMessage . MessagePb rMessagePb ;
107
+ TalkMessage talkMessage ;
110
108
try {
111
109
//解密
112
110
if (config .enableEncrypt ) {
113
111
responseBody = aesCipherUtil .descriptor .decrypt (responseBody );
114
112
}
115
113
log .debug ("收到服务端发回字节数 {}" , responseBody .length );
116
- rMessagePb = ProtoMessage .MessagePb .parseFrom (responseBody );
114
+ talkMessage = new TalkMessage (responseBody );
115
+ if (DebugConfig .OpenSerialNumber ) {
116
+ log .debug ("ClientTalker收到服务端发回 <talkMessage-SerialNumber {}" , talkMessage .getSerialNumber ());
117
+ }
117
118
} catch (Exception e ) {
118
119
log .warn ("服务端响应错误 {}" , new String (responseBody , StandardCharsets .UTF_8 ), e );
119
120
Thread .sleep (10000 );
@@ -122,51 +123,54 @@ public static boolean receiveServerBytes(CommonConfig config, byte[] responseBod
122
123
123
124
boolean isEmpty = true ;
124
125
//收命令
125
- for (String command : rMessagePb .getCommandListList ()) {
126
- log .debug ("收到服务端命令 {} " , command );
127
- char type = command .charAt (0 );
128
- switch (type ) {
129
- case Constant .ScCommands .InitSession -> {
130
- //sessionId,initFlag
131
- String [] params = command .substring (1 ).split (Constant .sessionIdJoinFlag );
132
- int sessionId = Integer .parseInt (params [0 ]);
133
- int initFlag = Integer .parseInt (params [1 ]);
134
- ClientBytesSender .SessionIdCallBack sessionIdCallBack = sessionIdCallBackMap .remove (initFlag );
135
- if (null != sessionIdCallBack ) {
136
- sessionIdCallBack .cb (sessionId );
137
- } else {
138
- log .warn ("没有对应的SessionIdCallBack {}" , sessionIdCallBack );
126
+ if (null != talkMessage .getCommands () && !talkMessage .getCommands ().isEmpty ()) {
127
+ for (String command : talkMessage .getCommands ()) {
128
+ log .debug ("收到服务端命令 {} " , command );
129
+ char type = command .charAt (0 );
130
+ switch (type ) {
131
+ case Constant .ScCommands .InitSession -> {
132
+ //sessionId,initFlag
133
+ String [] params = command .substring (1 ).split (Constant .sessionIdJoinFlag );
134
+ int sessionId = Integer .parseInt (params [0 ]);
135
+ int initFlag = Integer .parseInt (params [1 ]);
136
+ ClientBytesSender .SessionIdCallBack sessionIdCallBack = sessionIdCallBackMap .remove (initFlag );
137
+ if (null != sessionIdCallBack ) {
138
+ sessionIdCallBack .cb (sessionId );
139
+ } else {
140
+ log .warn ("没有对应的SessionIdCallBack {}" , sessionIdCallBack );
141
+ }
139
142
}
140
- }
141
- case Constant . ScCommands . CloseSession -> {
142
- int sessionId = Integer . parseInt ( command . substring ( 1 ) );
143
- ClientSession session = clientSessionManager . getClientSessionBySessionId ( sessionId );
144
- if ( null != session ) {
145
- clientSessionManager . disposeClientSession ( session , "服务端发送关闭命令" );
143
+ case Constant . ScCommands . CloseSession -> {
144
+ int sessionId = Integer . parseInt ( command . substring ( 1 ));
145
+ ClientSession session = clientSessionManager . getClientSessionBySessionId ( sessionId );
146
+ if ( null != session ) {
147
+ clientSessionManager . disposeClientSession ( session , "服务端发送关闭命令" );
148
+ }
146
149
}
147
- }
148
- case Constant . ScCommands . CheckSessionActive -> {
149
- int sessionId = Integer . parseInt ( command . substring ( 1 ) );
150
- ClientSession session = clientSessionManager . getClientSessionBySessionId ( sessionId );
151
- if ( null != session ) {
152
- //session存在,则发送存活消息
153
- sendCommandQueue . add ( String . valueOf ( Constant . SsCommands . ActiveSession ) + sessionId );
154
- } else {
155
- //否则发送关闭消息
156
- sendCommandQueue . add ( String . valueOf ( Constant . SsCommands . CloseSession ) + sessionId );
150
+ case Constant . ScCommands . CheckSessionActive -> {
151
+ int sessionId = Integer . parseInt ( command . substring ( 1 ));
152
+ ClientSession session = clientSessionManager . getClientSessionBySessionId ( sessionId );
153
+ if ( null != session ) {
154
+ //session存在,则发送存活消息
155
+ sendCommandQueue . add ( String . valueOf ( Constant . SsCommands . ActiveSession ) + sessionId );
156
+ } else {
157
+ //否则发送关闭消息
158
+ sendCommandQueue . add ( String . valueOf ( Constant . SsCommands . CloseSession ) + sessionId );
159
+ }
157
160
}
158
161
}
159
162
}
160
163
}
161
164
165
+
162
166
//收字节
163
- List <ProtoMessage . BytesPb > rBytesPbListList = rMessagePb . getBytesPbListList ();
164
- if (! rBytesPbListList .isEmpty ()) {
167
+ List <SessionBytes > sessionBytes = talkMessage . getSessionBytes ();
168
+ if (null != sessionBytes && ! sessionBytes .isEmpty ()) {
165
169
isEmpty = false ;
166
- for (ProtoMessage . BytesPb bytesPb : rBytesPbListList ) {
167
- ClientSession clientSession = clientSessionManager .getClientSessionBySessionId (bytesPb .getSessionId ());
170
+ for (SessionBytes sessionByte : sessionBytes ) {
171
+ ClientSession clientSession = clientSessionManager .getClientSessionBySessionId (sessionByte .getSessionId ());
168
172
if (clientSession != null ) {
169
- clientSession .sendToUser (bytesPb .getBytes (). toByteArray ());
173
+ clientSession .sendToUser (sessionByte .getBytes ());
170
174
} else {
171
175
//客户端没有这个session,异步等待一下看是否是未初始化完成
172
176
Thread .startVirtualThread (() -> {
@@ -178,9 +182,9 @@ public static boolean receiveServerBytes(CommonConfig config, byte[] responseBod
178
182
} catch (InterruptedException e ) {
179
183
continue ;
180
184
}
181
- clientSession1 = clientSessionManager .getClientSessionBySessionId (bytesPb .getSessionId ());
185
+ clientSession1 = clientSessionManager .getClientSessionBySessionId (sessionByte .getSessionId ());
182
186
if (null != clientSession1 ) {
183
- clientSession1 .sendToUser (bytesPb .getBytes (). toByteArray ());
187
+ clientSession1 .sendToUser (sessionByte .getBytes ());
184
188
return ;
185
189
}
186
190
}
0 commit comments