@@ -25,14 +25,12 @@ of this software and associated documentation files (the "Software"), to deal
25
25
26
26
package bwapi ;
27
27
28
- import bwapi .ClientData .GameData ;
29
28
import com .sun .jna .Native ;
29
+ import com .sun .jna .Pointer ;
30
30
import com .sun .jna .platform .win32 .Kernel32 ;
31
31
import com .sun .jna .win32 .W32APIOptions ;
32
32
33
33
import java .io .RandomAccessFile ;
34
- import java .nio .ByteBuffer ;
35
- import java .nio .ByteOrder ;
36
34
37
35
class Client {
38
36
interface MappingKernel extends Kernel32 {
@@ -48,8 +46,8 @@ interface MappingKernel extends Kernel32 {
48
46
private BWClient bwClient ;
49
47
private boolean connected = false ;
50
48
private RandomAccessFile pipeObjectHandle = null ;
51
- private ByteBuffer gameTableFileHandle = null ;
52
- private ByteBuffer mapFileHandle = null ;
49
+ private WrappedBuffer gameTableFileHandle = null ;
50
+ private WrappedBuffer mapFileHandle = null ;
53
51
54
52
Client (BWClient bwClient ) {
55
53
this .bwClient = bwClient ;
@@ -58,7 +56,7 @@ interface MappingKernel extends Kernel32 {
58
56
/**
59
57
* For test purposes only
60
58
*/
61
- Client (ByteBuffer buffer ) {
59
+ Client (final WrappedBuffer buffer ) {
62
60
clientData = new ClientData ();
63
61
clientData .setBuffer (buffer );
64
62
}
@@ -67,7 +65,7 @@ ClientData liveClientData() {
67
65
return clientData ;
68
66
}
69
67
70
- ByteBuffer mapFile () {
68
+ WrappedBuffer mapFile () {
71
69
return mapFileHandle ;
72
70
}
73
71
@@ -90,11 +88,10 @@ private void disconnect() {
90
88
return ;
91
89
}
92
90
93
- if (pipeObjectHandle != null ) {
91
+ if (pipeObjectHandle != null ) {
94
92
try {
95
93
pipeObjectHandle .close ();
96
- }
97
- catch (Exception e ) {
94
+ } catch (Exception e ) {
98
95
e .printStackTrace ();
99
96
}
100
97
pipeObjectHandle = null ;
@@ -117,21 +114,19 @@ boolean connect() {
117
114
118
115
// Expose the BWAPI list of games from shared memory via a ByteBuffer
119
116
try {
120
- gameTableFileHandle = Kernel32 .INSTANCE .MapViewOfFile (
121
- MappingKernel .INSTANCE .OpenFileMapping (READ_WRITE , false , "Local\\ bwapi_shared_memory_game_list" ), READ_WRITE , 0 , 0 , GameTable .SIZE )
122
- .getByteBuffer (0 , GameTable .SIZE );
123
- gameTableFileHandle .order (ByteOrder .LITTLE_ENDIAN );
124
- }
125
- catch (Exception e ) {
117
+ final Pointer gameTableView = Kernel32 .INSTANCE .MapViewOfFile (MappingKernel .INSTANCE
118
+ .OpenFileMapping (READ_WRITE , false , "Local\\ bwapi_shared_memory_game_list" ), READ_WRITE ,
119
+ 0 , 0 , GameTable .SIZE );
120
+ gameTableFileHandle = new WrappedBuffer (gameTableView , GameTable .SIZE );
121
+ } catch (Exception e ) {
126
122
System .err .println ("Game table mapping not found." );
127
123
return false ;
128
124
}
129
125
130
126
GameTable gameTable ;
131
127
try {
132
128
gameTable = new GameTable (gameTableFileHandle );
133
- }
134
- catch (Exception e ) {
129
+ } catch (Exception e ) {
135
130
System .err .println ("Unable to map Game table." );
136
131
if (bwClient .getConfiguration ().getDebugConnection ()) {
137
132
e .printStackTrace ();
@@ -140,11 +135,11 @@ boolean connect() {
140
135
}
141
136
142
137
int latest = 0 ;
143
- for (int i = 0 ; i < GameTable .MAX_GAME_INSTANCES ; i ++) {
138
+ for (int i = 0 ; i < GameTable .MAX_GAME_INSTANCES ; i ++) {
144
139
GameInstance gameInstance = gameTable .gameInstances [i ];
145
140
System .out .println (i + " | " + gameInstance .serverProcessID + " | " + (gameInstance .isConnected ? 1 : 0 ) + " | " + gameInstance .lastKeepAliveTime );
146
141
if (gameInstance .serverProcessID != 0 && !gameInstance .isConnected ) {
147
- if ( gameTableIndex == -1 || latest == 0 || gameInstance .lastKeepAliveTime < latest ) {
142
+ if (gameTableIndex == -1 || latest == 0 || gameInstance .lastKeepAliveTime < latest ) {
148
143
latest = gameInstance .lastKeepAliveTime ;
149
144
gameTableIndex = i ;
150
145
}
@@ -163,9 +158,8 @@ boolean connect() {
163
158
final String sharedMemoryName = "Local\\ bwapi_shared_memory_" + serverProcID ;
164
159
final String communicationPipe = "\\ \\ .\\ pipe\\ bwapi_pipe_" + serverProcID ;
165
160
try {
166
- pipeObjectHandle = new RandomAccessFile (communicationPipe , "rw" );
167
- }
168
- catch (Exception e ) {
161
+ pipeObjectHandle = new RandomAccessFile (communicationPipe , "rw" );
162
+ } catch (Exception e ) {
169
163
System .err .println ("Unable to open communications pipe: " + communicationPipe );
170
164
if (bwClient .getConfiguration ().getDebugConnection ()) {
171
165
e .printStackTrace ();
@@ -177,11 +171,11 @@ boolean connect() {
177
171
178
172
// Expose the raw game data from shared memory via a ByteBuffer
179
173
try {
180
- mapFileHandle = Kernel32 .INSTANCE .MapViewOfFile (MappingKernel .INSTANCE
174
+ final Pointer mapFileView = Kernel32 .INSTANCE .MapViewOfFile (MappingKernel .INSTANCE
181
175
.OpenFileMapping (READ_WRITE , false , sharedMemoryName ), READ_WRITE ,
182
- 0 , 0 , GameData . SIZE ). getByteBuffer ( 0 , GameData .SIZE );
183
- }
184
- catch (Exception e ) {
176
+ 0 , 0 , ClientData . GameData .SIZE );
177
+ mapFileHandle = new WrappedBuffer ( mapFileView , ClientData . GameData . SIZE );
178
+ } catch (Exception e ) {
185
179
System .err .println ("Unable to open shared memory mapping: " + sharedMemoryName );
186
180
if (bwClient .getConfiguration ().getDebugConnection ()) {
187
181
e .printStackTrace ();
@@ -214,8 +208,7 @@ boolean connect() {
214
208
while (code != 2 ) {
215
209
try {
216
210
code = pipeObjectHandle .readByte ();
217
- }
218
- catch (Exception e ) {
211
+ } catch (Exception e ) {
219
212
System .err .println ("Unable to read pipe object." );
220
213
if (bwClient .getConfiguration ().getDebugConnection ()) {
221
214
e .printStackTrace ();
@@ -279,6 +272,7 @@ void sendFrameReceiveFrame() {
279
272
break ;
280
273
}
281
274
}
275
+
282
276
metrics .getCommunicationListenToReceive ().stopTiming ();
283
277
metrics .getCommunicationSendToReceive ().stopTiming ();
284
278
@@ -298,8 +292,7 @@ void sendFrameReceiveFrame() {
298
292
private void sleep (final int millis ) {
299
293
try {
300
294
Thread .sleep (millis );
301
- }
302
- catch (Exception ignored ) {
295
+ } catch (Exception ignored ) {
303
296
}
304
297
}
305
298
}
0 commit comments