Skip to content

Commit 95c7ef2

Browse files
committed
avoid using bytebuffer altogether
1 parent 4cb0517 commit 95c7ef2

File tree

4 files changed

+15
-26
lines changed

4 files changed

+15
-26
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ target/
88
.classpath
99
.project
1010

11+
# logging
12+
*.log

src/main/java/bwapi/Client.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ of this software and associated documentation files (the "Software"), to deal
3434
import com.sun.jna.win32.W32APIOptions;
3535

3636
import java.io.RandomAccessFile;
37-
import java.nio.ByteBuffer;
38-
import java.nio.ByteOrder;
3937

4038
class Client {
4139
interface MappingKernel extends Kernel32 {
@@ -59,7 +57,7 @@ public interface EventHandler {
5957
private boolean connected = false;
6058
private RandomAccessFile pipeObjectHandle = null;
6159
private WrappedBuffer mapFileHandle = null;
62-
private ByteBuffer gameTableFileHandle = null;
60+
private WrappedBuffer gameTableFileHandle = null;
6361

6462
private boolean debugConnection = false;
6563

@@ -128,9 +126,10 @@ boolean connect() {
128126
int gameTableIndex = -1;
129127

130128
try {
131-
gameTableFileHandle = Kernel32.INSTANCE.MapViewOfFile(
132-
MappingKernel.INSTANCE.OpenFileMapping(READ_WRITE, false, "Local\\bwapi_shared_memory_game_list"), READ_WRITE, 0, 0, GameTable.SIZE)
133-
.getByteBuffer(0, GameTable.SIZE);
129+
final Pointer gameTableView = Kernel32.INSTANCE.MapViewOfFile(MappingKernel.INSTANCE
130+
.OpenFileMapping(READ_WRITE, false, "Local\\bwapi_shared_memory_game_list"), READ_WRITE,
131+
0, 0, GameTable.SIZE);
132+
gameTableFileHandle = new WrappedBuffer(gameTableView, GameTable.SIZE);
134133
}
135134
catch (Exception e) {
136135
System.err.println("Game table mapping not found.");
@@ -281,13 +280,8 @@ int addString(final String string) {
281280
throw new IllegalStateException("Too many strings!");
282281
}
283282

284-
//truncate string if its size equals or exceeds 1024
285-
final String stringTruncated = string.length() >= MAX_STRING_SIZE
286-
? string.substring(0, MAX_STRING_SIZE - 1)
287-
: string;
288-
289283
gameData.setStringCount(stringCount + 1);
290-
gameData.setStrings(stringCount, stringTruncated);
284+
gameData.setStrings(stringCount, string);
291285
return stringCount;
292286
}
293287

src/main/java/bwapi/GameTable.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package bwapi;
22

3-
import java.nio.ByteBuffer;
4-
53
/**
64
* https://github.com/bwapi/bwapi/blob/456ad612abc84da4103162ba0bf8ec4f053a4b1d/bwapi/include/BWAPI/Client/GameTable.h
75
*/
@@ -29,12 +27,12 @@ class GameTable {
2927

3028
final GameInstance[] gameInstances;
3129

32-
GameTable(final ByteBuffer gameTableFileHandle) {
30+
GameTable(final WrappedBuffer gameTableFileHandle) {
3331
gameInstances = new GameInstance[MAX_GAME_INSTANCES];
3432

3533
for (int i = 0; i < MAX_GAME_INSTANCES; i++) {
3634
int serverProcessID = gameTableFileHandle.getInt(GameInstance.SIZE * i);
37-
boolean isConnected = gameTableFileHandle.get(GameInstance.SIZE * i + 4) != 0;
35+
boolean isConnected = gameTableFileHandle.getByte(GameInstance.SIZE * i + 4) != 0;
3836
int lastKeepAliveTime = gameTableFileHandle.getInt(GameInstance.SIZE * i + 4 + 4);
3937
gameInstances[i] = new GameInstance(serverProcessID, isConnected, lastKeepAliveTime);
4038
}

src/main/java/bwapi/WrappedBuffer.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
import java.nio.ByteBuffer;
99

1010
/**
11-
* Wrapper around ByteBuffer that makes use of sun.misc.Unsafe if available.
11+
* Wrapper around offheap memory that uses sun.misc.Unsafe to for fast access.
1212
*/
1313
class WrappedBuffer {
14-
private final Pointer pointer;
15-
private final int size;
14+
private final ByteBuffer buffer;
1615
private final long address;
1716

1817
private static Unsafe unsafe;
@@ -33,8 +32,7 @@ class WrappedBuffer {
3332
}
3433

3534
WrappedBuffer(final Pointer pointer, final int size) {
36-
this.pointer = pointer;
37-
this.size = size;
35+
this.buffer = pointer.getByteBuffer(0, size);
3836
this.address = Pointer.nativeValue(pointer);
3937
}
4038

@@ -83,18 +81,15 @@ String getString(final int offset, final int maxLen) {
8381
}
8482

8583
void putString(final int offset, final int maxLen, final String string) {
86-
if (string.length() >= maxLen) {
87-
throw new StringIndexOutOfBoundsException();
88-
}
8984
long pos = offset + address;
90-
for (int i = 0; i < string.length(); i++) {
85+
for (int i = 0; i < Math.min(string.length(), maxLen - 1); i++) {
9186
unsafe.putByte(pos, (byte) string.charAt(i));
9287
pos++;
9388
}
9489
unsafe.putByte(pos, (byte) 0);
9590
}
9691

9792
ByteBuffer getBuffer() {
98-
return pointer.getByteBuffer(0, size);
93+
return buffer;
9994
}
10095
}

0 commit comments

Comments
 (0)