@@ -25,14 +25,12 @@ of this software and associated documentation files (the "Software"), to deal
2525
2626package bwapi ;
2727
28- import bwapi .ClientData .GameData ;
2928import com .sun .jna .Native ;
29+ import com .sun .jna .Pointer ;
3030import com .sun .jna .platform .win32 .Kernel32 ;
3131import com .sun .jna .win32 .W32APIOptions ;
3232
3333import java .io .RandomAccessFile ;
34- import java .nio .ByteBuffer ;
35- import java .nio .ByteOrder ;
3634
3735class Client {
3836 interface MappingKernel extends Kernel32 {
@@ -48,8 +46,8 @@ interface MappingKernel extends Kernel32 {
4846 private BWClient bwClient ;
4947 private boolean connected = false ;
5048 private RandomAccessFile pipeObjectHandle = null ;
51- private ByteBuffer gameTableFileHandle = null ;
52- private ByteBuffer mapFileHandle = null ;
49+ private WrappedBuffer gameTableFileHandle = null ;
50+ private WrappedBuffer mapFileHandle = null ;
5351
5452 Client (BWClient bwClient ) {
5553 this .bwClient = bwClient ;
@@ -58,7 +56,7 @@ interface MappingKernel extends Kernel32 {
5856 /**
5957 * For test purposes only
6058 */
61- Client (ByteBuffer buffer ) {
59+ Client (final WrappedBuffer buffer ) {
6260 clientData = new ClientData ();
6361 clientData .setBuffer (buffer );
6462 }
@@ -67,7 +65,7 @@ ClientData liveClientData() {
6765 return clientData ;
6866 }
6967
70- ByteBuffer mapFile () {
68+ WrappedBuffer mapFile () {
7169 return mapFileHandle ;
7270 }
7371
@@ -90,11 +88,10 @@ private void disconnect() {
9088 return ;
9189 }
9290
93- if (pipeObjectHandle != null ) {
91+ if (pipeObjectHandle != null ) {
9492 try {
9593 pipeObjectHandle .close ();
96- }
97- catch (Exception e ) {
94+ } catch (Exception e ) {
9895 e .printStackTrace ();
9996 }
10097 pipeObjectHandle = null ;
@@ -117,21 +114,19 @@ boolean connect() {
117114
118115 // Expose the BWAPI list of games from shared memory via a ByteBuffer
119116 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 ) {
126122 System .err .println ("Game table mapping not found." );
127123 return false ;
128124 }
129125
130126 GameTable gameTable ;
131127 try {
132128 gameTable = new GameTable (gameTableFileHandle );
133- }
134- catch (Exception e ) {
129+ } catch (Exception e ) {
135130 System .err .println ("Unable to map Game table." );
136131 if (bwClient .getConfiguration ().getDebugConnection ()) {
137132 e .printStackTrace ();
@@ -140,11 +135,11 @@ boolean connect() {
140135 }
141136
142137 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 ++) {
144139 GameInstance gameInstance = gameTable .gameInstances [i ];
145140 System .out .println (i + " | " + gameInstance .serverProcessID + " | " + (gameInstance .isConnected ? 1 : 0 ) + " | " + gameInstance .lastKeepAliveTime );
146141 if (gameInstance .serverProcessID != 0 && !gameInstance .isConnected ) {
147- if ( gameTableIndex == -1 || latest == 0 || gameInstance .lastKeepAliveTime < latest ) {
142+ if (gameTableIndex == -1 || latest == 0 || gameInstance .lastKeepAliveTime < latest ) {
148143 latest = gameInstance .lastKeepAliveTime ;
149144 gameTableIndex = i ;
150145 }
@@ -163,9 +158,8 @@ boolean connect() {
163158 final String sharedMemoryName = "Local\\ bwapi_shared_memory_" + serverProcID ;
164159 final String communicationPipe = "\\ \\ .\\ pipe\\ bwapi_pipe_" + serverProcID ;
165160 try {
166- pipeObjectHandle = new RandomAccessFile (communicationPipe , "rw" );
167- }
168- catch (Exception e ) {
161+ pipeObjectHandle = new RandomAccessFile (communicationPipe , "rw" );
162+ } catch (Exception e ) {
169163 System .err .println ("Unable to open communications pipe: " + communicationPipe );
170164 if (bwClient .getConfiguration ().getDebugConnection ()) {
171165 e .printStackTrace ();
@@ -177,11 +171,11 @@ boolean connect() {
177171
178172 // Expose the raw game data from shared memory via a ByteBuffer
179173 try {
180- mapFileHandle = Kernel32 .INSTANCE .MapViewOfFile (MappingKernel .INSTANCE
174+ final Pointer mapFileView = Kernel32 .INSTANCE .MapViewOfFile (MappingKernel .INSTANCE
181175 .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 ) {
185179 System .err .println ("Unable to open shared memory mapping: " + sharedMemoryName );
186180 if (bwClient .getConfiguration ().getDebugConnection ()) {
187181 e .printStackTrace ();
@@ -214,8 +208,7 @@ boolean connect() {
214208 while (code != 2 ) {
215209 try {
216210 code = pipeObjectHandle .readByte ();
217- }
218- catch (Exception e ) {
211+ } catch (Exception e ) {
219212 System .err .println ("Unable to read pipe object." );
220213 if (bwClient .getConfiguration ().getDebugConnection ()) {
221214 e .printStackTrace ();
@@ -279,6 +272,7 @@ void sendFrameReceiveFrame() {
279272 break ;
280273 }
281274 }
275+
282276 metrics .getCommunicationListenToReceive ().stopTiming ();
283277 metrics .getCommunicationSendToReceive ().stopTiming ();
284278
@@ -298,8 +292,7 @@ void sendFrameReceiveFrame() {
298292 private void sleep (final int millis ) {
299293 try {
300294 Thread .sleep (millis );
301- }
302- catch (Exception ignored ) {
295+ } catch (Exception ignored ) {
303296 }
304297 }
305298}
0 commit comments