Skip to content

Commit

Permalink
Merge branch 'release/v0.2.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
gazer2kanlin committed Aug 22, 2018
2 parents fc20207 + 91788aa commit b3aa713
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 0 deletions.
12 changes: 12 additions & 0 deletions uia.comm/src/main/java/uia/comm/SocketClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public class SocketClient implements ProtocolEventHandler<SocketDataController>,

private int port;

private int maxCache;

/**
* The constructor.
*
Expand Down Expand Up @@ -103,6 +105,15 @@ public SocketClient(final Protocol<SocketDataController> protocol, final Message
this.callIns = new HashMap<String, MessageCallIn<SocketDataController>>();
this.callOuts = new HashMap<String, MessageCallOut>();
this.started = false;
this.maxCache = 10 * 1000; // 10K
}

public int getMaxCache() {
return this.maxCache;
}

public void setMaxCache(int maxCache) {
this.maxCache = Math.min(2000000, Math.max(16, maxCache)); // 2M
}

@Override
Expand Down Expand Up @@ -212,6 +223,7 @@ public synchronized boolean tryConnect() {
this.ch,
this.manager,
this.protocol.createMonitor(this.aliasName));
this.controller.setMaxCache(this.maxCache);
this.controller.start();

if (this.clientPort > 0) {
Expand Down
14 changes: 14 additions & 0 deletions uia.comm/src/main/java/uia/comm/SocketDataController.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public class SocketDataController implements DataController {

private long lastUpdate;

private int maxCache;

/**
*
* @param name Name.
Expand All @@ -71,6 +73,15 @@ public class SocketDataController implements DataController {
this.monitor = monitor;
this.monitor.setController(this);
this.lastUpdate = System.currentTimeMillis();
this.maxCache = 10 * 1000; // 10K
}

public int getMaxCache() {
return this.maxCache;
}

public void setMaxCache(int maxCache) {
this.maxCache = Math.min(2000000, Math.max(16, maxCache)); // 2M
}

@Override
Expand Down Expand Up @@ -182,6 +193,9 @@ synchronized boolean receive() throws IOException {
byte[] value = (byte[]) buffer.flip().array();
value = Arrays.copyOf(value, len);
for (byte b : value) {
if (this.monitor.getDataLength() > this.maxCache) {
this.monitor.reset();
}
this.monitor.read(b);
}
}
Expand Down
12 changes: 12 additions & 0 deletions uia.comm/src/main/java/uia/comm/SocketServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public enum ConnectionStyle {

private int idleTime;

private int maxCache;

public SocketServer(Protocol<SocketDataController> protocol, int port, MessageManager manager, String aliasName) throws Exception {
this(protocol, port, manager, aliasName, ConnectionStyle.NORMAL);
}
Expand Down Expand Up @@ -111,6 +113,15 @@ public SocketServer(Protocol<SocketDataController> protocol, int port, MessageMa

this.idleTime = 60000;
this.port = port;
this.maxCache = 10 * 1000; // 10K
}

public int getMaxCache() {
return this.maxCache;
}

public void setMaxCache(int maxCache) {
this.maxCache = Math.min(2000000, Math.max(16, maxCache)); // 2M
}

public int getClientCount() {
Expand Down Expand Up @@ -656,6 +667,7 @@ private void clientConnected(SocketChannel client) {
client,
this.manager,
this.protocol.createMonitor(clientId));
controller.setMaxCache(this.maxCache);

synchronized (this.controllers) {
this.controllers.put(clientId, controller);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public void setProtocol(Protocol<C> protocol) {
this.protocol = protocol;
}

@Override
public int getDataLength() {
return this.data.size();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,13 @@ void reset(ProtocolMonitor<MultiProtocolMonitor<T>> monitor) {
}
}
}

@Override
public int getDataLength() {
int dataLength = 0;
for (ProtocolMonitor<MultiProtocolMonitor<T>> pm : this.monitors) {
dataLength = Math.max(dataLength, pm.getDataLength());
}
return dataLength;
}
}
6 changes: 6 additions & 0 deletions uia.comm/src/main/java/uia/comm/protocol/ProtocolMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
*/
public interface ProtocolMonitor<C> {

/**
* Get data count in this monitor.
* @return Data count.
*/
public int getDataLength();

/**
* Get protocol which creates this monitor.
*
Expand Down

0 comments on commit b3aa713

Please sign in to comment.