Skip to content

Commit

Permalink
update (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
tvd12 authored Jul 10, 2023
1 parent cfe5777 commit 542a9bb
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 24 deletions.
2 changes: 1 addition & 1 deletion ezyfox-server-core/settings/ssl-config.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
ssl.key_store="ssl/ssl-key-store.txt"
ssl.keystore=ssl/ssl-keystore.txt
ssl.keystore_password=ssl/ssl-keystore-password.txt
ssl.certificate_password=ssl/ssl-certificate-password.txt
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
package com.tvd12.ezyfoxserver.setting;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.Map;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Setter
@Getter
@ToString
@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "udp")
public class EzySimpleUdpSetting
extends EzyAbstractSocketSetting
implements EzyUdpSetting {
public class EzySimpleUdpSetting implements EzyUdpSetting {

@XmlElement(name = "port")
protected int port;

@XmlElement(name = "address")
protected String address;

@XmlElement(name = "active")
protected boolean active;

@XmlElement(name = "max-request-size")
protected int maxRequestSize;
Expand All @@ -31,16 +40,19 @@ public class EzySimpleUdpSetting
public EzySimpleUdpSetting() {
super();
setPort(2611);
setAddress("0.0.0.0");
setActive(false);
setMaxRequestSize(1024);
setChannelPoolSize(16);
setHandlerThreadPoolSize(5);
setCodecCreator("com.tvd12.ezyfox.codec.MsgPackCodecCreator");
}

@Override
public Map<Object, Object> toMap() {
Map<Object, Object> map = super.toMap();
Map<Object, Object> map = new HashMap<>();
map.put("port", port);
map.put("address", address);
map.put("active", active);
map.put("maxRequestSize", maxRequestSize);
map.put("channelPoolSize", channelPoolSize);
map.put("handlerThreadPoolSize", handlerThreadPoolSize);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
package com.tvd12.ezyfoxserver.setting;

import com.tvd12.ezyfoxserver.constant.SslType;

public class EzySocketSettingBuilder extends
EzyAbstractSocketSettingBuilder<
EzySimpleSocketSetting,
EzySocketSettingBuilder> {

protected SslType sslType;
protected int maxRequestSize;
protected boolean tcpNoDelay;
protected int writerThreadPoolSize;

public EzySocketSettingBuilder() {
this.port = 3005;
this.sslType = SslType.L7;
this.maxRequestSize = 32768;
this.writerThreadPoolSize = 8;
this.codecCreator = "com.tvd12.ezyfox.codec.MsgPackCodecCreator";
}

public EzySocketSettingBuilder sslType(SslType sslType) {
this.sslType = sslType;
return this;
}

public EzySocketSettingBuilder maxRequestSize(int maxRequestSize) {
this.maxRequestSize = maxRequestSize;
return this;
Expand All @@ -34,6 +43,7 @@ public EzySocketSettingBuilder writerThreadPoolSize(int writerThreadPoolSize) {
@Override
protected EzySimpleSocketSetting newSetting() {
EzySimpleSocketSetting setting = new EzySimpleSocketSetting();
setting.setSslType(sslType);
setting.setTcpNoDelay(tcpNoDelay);
setting.setSslActive(sslActive);
setting.setMaxRequestSize(maxRequestSize);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
package com.tvd12.ezyfoxserver.setting;

public interface EzyUdpSetting extends EzyBaseSocketSetting {
import com.tvd12.ezyfox.util.EzyToMap;

public interface EzyUdpSetting extends EzyToMap {

int getPort();

String getAddress();

boolean isActive();

int getMaxRequestSize();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
package com.tvd12.ezyfoxserver.setting;

public class EzyUdpSettingBuilder extends EzyAbstractSocketSettingBuilder<
EzySimpleUdpSetting, EzyUdpSettingBuilder> {
import com.tvd12.ezyfox.builder.EzyBuilder;

public class EzyUdpSettingBuilder implements EzyBuilder<EzySimpleUdpSetting> {
protected int port;
protected String address;
protected boolean active;
protected int maxRequestSize;
protected int channelPoolSize;
protected int handlerThreadPoolSize;

public EzyUdpSettingBuilder() {
this.port = 2611;
this.address = "0.0.0.0";
this.active = false;
this.maxRequestSize = 1024;
this.channelPoolSize = 16;
this.handlerThreadPoolSize = 5;
this.codecCreator = "com.tvd12.ezyfox.codec.MsgPackCodecCreator";
}

public EzyUdpSettingBuilder port(int port) {
this.port = port;
return this;
}

public EzyUdpSettingBuilder address(String address) {
this.address = address;
return this;
}

public EzyUdpSettingBuilder active(boolean active) {
this.active = active;
return this;
}

public EzyUdpSettingBuilder maxRequestSize(int maxRequestSize) {
Expand All @@ -32,8 +50,11 @@ public EzyUdpSettingBuilder handlerThreadPoolSize(int handlerThreadPoolSize) {
}

@Override
public EzySimpleUdpSetting newSetting() {
public EzySimpleUdpSetting build() {
EzySimpleUdpSetting p = new EzySimpleUdpSetting();
p.setPort(port);
p.setAddress(address);
p.setActive(active);
p.setMaxRequestSize(maxRequestSize);
p.setChannelPoolSize(channelPoolSize);
p.setHandlerThreadPoolSize(handlerThreadPoolSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ protected char[] getPassword(String file) throws Exception {
InputStream stream = newInputStreamLoader().load(file);
char[] answer;
try {
answer = newInputStreamReader().readChars(stream, "UTF-8");
answer = newInputStreamReader()
.readString(stream, "UTF-8")
.trim()
.toCharArray();
} finally {
stream.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public void test() {
.active(true)
.address("2.2.2.2")
.channelPoolSize(3)
.codecCreator(TestCodecCreator.class)
.handlerThreadPoolSize(3)
.maxRequestSize(2048)
.port(23456)
Expand Down Expand Up @@ -119,7 +118,6 @@ public void test() {
udpSetting = settings.getUdp();
assertTrue(udpSetting.isActive());
assertEquals(udpSetting.getAddress(), "2.2.2.2");
assertEquals(udpSetting.getCodecCreator(), TestCodecCreator.class.getName());
assertEquals(udpSetting.getMaxRequestSize(), 2048);
assertEquals(udpSetting.getPort(), 23456);
assertEquals(udpSetting.getChannelPoolSize(), 3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ public static void main(String[] args) throws Exception {
.active(true) // active or not
.address("0.0.0.0") // set loopback IP
.channelPoolSize(16) // set number of udp channel for socket writing, default 16
.codecCreator(MsgPackCodecCreator.class) // encoder/decoder creator, default MsgPackCodecCreator
.handlerThreadPoolSize(5) // set number of handler's thread, default 5
.maxRequestSize(1024) // set max request's size
.port(2611) // set listen port
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.tvd12.ezyfoxserver.socket.EzySocketWritingLoopHandler;

import javax.net.ssl.SSLContext;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.channels.SelectionKey;
Expand Down Expand Up @@ -67,21 +69,29 @@ private void newAndConfigServerSocketChannel() throws Exception {

private void getBindAndConfigServerSocket() throws Exception {
this.serverSocket = createServerSocket();
this.serverSocket.setReuseAddress(true);
this.serverSocket.bind(new InetSocketAddress(getSocketAddress(), getSocketPort()));
this.serverSocketChannel.register(acceptSelector, SelectionKey.OP_ACCEPT);
}

private ServerSocket createServerSocket() throws Exception {
ServerSocket server;
EzySocketSetting socketSetting = getSocketSetting();
boolean sslActive = socketSetting.isSslActive();
SslType sslType = socketSetting.getSslType();
if (sslActive && sslType == SslType.L4) {
return sslContext
server = sslContext
.getServerSocketFactory()
.createServerSocket(getSocketPort());
.createServerSocket(
getSocketPort(),
0,
InetAddress.getByName(getSocketAddress())
);
server.setReuseAddress(true);
} else {
server = serverSocketChannel.socket();
server.setReuseAddress(true);
server.bind(new InetSocketAddress(getSocketAddress(), getSocketPort()));
}
return serverSocketChannel.socket();
return server;
}

private void startSocketHandlers() throws Exception {
Expand Down

0 comments on commit 542a9bb

Please sign in to comment.