Skip to content

Commit

Permalink
Adapted to current dev branch.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bytekeeper committed Oct 2, 2020
1 parent 72ad7e5 commit e3faf46
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 44 deletions.
9 changes: 4 additions & 5 deletions src/main/java/bwapi/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ of this software and associated documentation files (the "Software"), to deal
import bwapi.ClientData.GameData;
import bwapi.ClientData.Shape;

import java.nio.ByteBuffer;

class Client {
public interface EventHandler {
void operation(ClientData.Event event);
Expand Down Expand Up @@ -124,13 +122,13 @@ boolean connect() {
return false;
}

int latest = 0;
int oldest = Integer.MAX_VALUE;
for (int i = 0; i < GameTable.MAX_GAME_INSTANCES; i++) {
GameInstance gameInstance = gameTable.gameInstances[i];
System.out.println(i + " | " + gameInstance.serverProcessID + " | " + (gameInstance.isConnected ? 1 : 0) + " | " + gameInstance.lastKeepAliveTime);
if (gameInstance.serverProcessID != 0 && !gameInstance.isConnected) {
if (gameTableIndex == -1 || gameInstance.lastKeepAliveTime > latest) {
latest = gameInstance.lastKeepAliveTime;
if (gameTableIndex == -1 || gameInstance.lastKeepAliveTime < oldest) {
oldest = gameInstance.lastKeepAliveTime;
gameTableIndex = i;
}
}
Expand Down Expand Up @@ -192,6 +190,7 @@ boolean connect() {
try {
clientConnector.waitForServerReady();
} catch (Exception e) {
System.err.println(e.getMessage());
if (debugConnection) {
e.printStackTrace();
}
Expand Down
17 changes: 8 additions & 9 deletions src/main/java/bwapi/ClientConnection.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
package bwapi;

import java.io.IOException;
import java.nio.ByteBuffer;

interface ClientConnection {
void disconnect();

ByteBuffer getGameTable();
WrappedBuffer getGameTable();

ByteBuffer getSharedMemory(int serverProcID);
WrappedBuffer getSharedMemory(int serverProcID);

void connectSharedLock(int serverProcID) throws Exception;
void connectSharedLock(int serverProcID) throws IOException;

void waitForServerReady() throws IOException;

void waitForServerData() throws Exception;
void waitForServerData() throws IOException;

void submitClientData() throws IOException;
}

class SharedMemoryConnectionError extends RuntimeException {
public SharedMemoryConnectionError(String message, Throwable cause) {
class SharedMemoryConnectionException extends RuntimeException {
public SharedMemoryConnectionException(String message, Throwable cause) {
super(message, cause);
}
}

class SharedLockConnectionError extends RuntimeException {
public SharedLockConnectionError(String message, Throwable cause) {
class SharedLockConnectionException extends RuntimeException {
public SharedLockConnectionException(String message, Throwable cause) {
super(message, cause);
}
}
23 changes: 10 additions & 13 deletions src/main/java/bwapi/ClientConnectionPosix.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import static com.sun.jna.platform.linux.Mman.*;

Expand All @@ -38,35 +36,34 @@ public void disconnect() {
}

@Override
public ByteBuffer getGameTable() {
public WrappedBuffer getGameTable() {
int fd = LibRT.INSTANCE.shm_open("/bwapi_shared_memory_game_list", Fcntl.O_RDWR, 0);
if (fd < 0) throw new IllegalStateException("SHM not found");
ByteBuffer gameTable = LibCExt.INSTANCE.mmap(Pointer.NULL, GameTable.SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)
.getByteBuffer(0, GameTable.SIZE);
gameTable.order(ByteOrder.LITTLE_ENDIAN);
return gameTable;
Pointer gameTableView = LibCExt.INSTANCE.mmap(Pointer.NULL, GameTable.SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
return new WrappedBuffer(gameTableView, GameTable.SIZE);
}

@Override
public ByteBuffer getSharedMemory(int serverProcID) {
public WrappedBuffer getSharedMemory(int serverProcID) {
String sharedMemoryName = "/bwapi_shared_memory_" + serverProcID;
try {
return LibCExt.INSTANCE.mmap(Pointer.NULL, ClientData.GameData.SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, LibRT.INSTANCE.shm_open(sharedMemoryName, Fcntl.O_RDWR, 0),
0).getByteBuffer(0, ClientData.GameData.SIZE);
Pointer mapFileView = LibCExt.INSTANCE.mmap(Pointer.NULL, ClientData.GameData.SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, LibRT.INSTANCE.shm_open(sharedMemoryName, Fcntl.O_RDWR, 0),
0);
return new WrappedBuffer(mapFileView, ClientData.GameData.SIZE);
} catch (Exception e) {
throw new SharedMemoryConnectionError(sharedMemoryName, e);
throw new SharedMemoryConnectionException(sharedMemoryName, e);
}
}

@Override
public void connectSharedLock(int serverProcID) throws Exception {
public void connectSharedLock(int serverProcID) {
final String communicationSocket = "/tmp/bwapi_socket_" + serverProcID;
try {
syncSocket = AFUNIXSocket.newInstance();
syncSocket.connect(new AFUNIXSocketAddress(new File(communicationSocket)));
} catch (IOException e) {
syncSocket = null;
throw new SharedLockConnectionError("Unable to open communications socket: " + communicationSocket, e);
throw new SharedLockConnectionException("Unable to open communications socket: " + communicationSocket, e);
}
}

Expand Down
33 changes: 16 additions & 17 deletions src/main/java/bwapi/ClientConnectionW32.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package bwapi;

import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.win32.W32APIOptions;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.io.UncheckedIOException;

class ClientConnectionW32 implements ClientConnection {
private static final int READ_WRITE = 0x1 | 0x2 | 0x4;
Expand All @@ -35,23 +35,23 @@ public void disconnect() {
}

@Override
public ByteBuffer getGameTable() {
ByteBuffer gameTable = Kernel32.INSTANCE.MapViewOfFile(
MappingKernel.INSTANCE.OpenFileMapping(READ_WRITE, false, "Local\\bwapi_shared_memory_game_list"), READ_WRITE, 0, 0, GameTable.SIZE)
.getByteBuffer(0, GameTable.SIZE);
gameTable.order(ByteOrder.LITTLE_ENDIAN);
return gameTable;
public WrappedBuffer getGameTable() {
final Pointer gameTableView = Kernel32.INSTANCE.MapViewOfFile(MappingKernel.INSTANCE
.OpenFileMapping(READ_WRITE, false, "Local\\bwapi_shared_memory_game_list"), READ_WRITE,
0, 0, GameTable.SIZE);
return new WrappedBuffer(gameTableView, GameTable.SIZE);
}

@Override
public ByteBuffer getSharedMemory(int serverProcID) {
public WrappedBuffer getSharedMemory(int serverProcID) {
String sharedMemoryName = "Local\\bwapi_shared_memory_" + serverProcID;
try {
return Kernel32.INSTANCE.MapViewOfFile(MappingKernel.INSTANCE
final Pointer mapFileView = Kernel32.INSTANCE.MapViewOfFile(MappingKernel.INSTANCE
.OpenFileMapping(READ_WRITE, false, sharedMemoryName), READ_WRITE,
0, 0, ClientData.GameData.SIZE).getByteBuffer(0, ClientData.GameData.SIZE);
0, 0, ClientData.GameData.SIZE);
return new WrappedBuffer(mapFileView, ClientData.GameData.SIZE);
} catch (Exception e) {
throw new SharedMemoryConnectionError(sharedMemoryName, e);
throw new SharedMemoryConnectionException(sharedMemoryName, e);
}
}

Expand All @@ -61,7 +61,7 @@ public void connectSharedLock(int serverProcID) {
try {
pipeObjectHandle = new RandomAccessFile(communicationPipe, "rw");
} catch (FileNotFoundException e) {
throw new SharedLockConnectionError("Unable to open communications pipe: " + communicationPipe, e);
throw new SharedLockConnectionException("Unable to open communications pipe: " + communicationPipe, e);
}
}

Expand All @@ -71,15 +71,14 @@ public void waitForServerReady() throws IOException {
while (code != 2) {
try {
code = pipeObjectHandle.readByte();
} catch (Exception e) {
System.err.println("Unable to read pipe object.");
throw e;
} catch (IOException e) {
throw new UncheckedIOException("Unable to read pipe object.", e);
}
}
}

@Override
public void waitForServerData() throws Exception {
public void waitForServerData() throws IOException {
byte code = 1;
pipeObjectHandle.writeByte(code);
while (code != 2) {
Expand Down

0 comments on commit e3faf46

Please sign in to comment.