Context.updateServerAddress

This commit is contained in:
redkale
2023-02-02 17:42:33 +08:00
parent c6ef28c358
commit 3c6b15ee9d
3 changed files with 21 additions and 3 deletions

View File

@@ -167,6 +167,10 @@ public class Context {
conn.updateWriteIOThread(ioWriteThread); conn.updateWriteIOThread(ioWriteThread);
} }
protected void updateServerAddress(InetSocketAddress addr) {
this.serverAddress = addr;
}
public ResourceFactory getResourceFactory() { public ResourceFactory getResourceFactory() {
return resourceFactory; return resourceFactory;
} }

View File

@@ -329,7 +329,7 @@ public abstract class Server<K extends Serializable, C extends Context, R extend
Objects.requireNonNull(addr); Objects.requireNonNull(addr);
final InetSocketAddress oldAddress = context.serverAddress; final InetSocketAddress oldAddress = context.serverAddress;
final ProtocolServer oldServerChannel = this.serverChannel; final ProtocolServer oldServerChannel = this.serverChannel;
context.serverAddress = addr; context.updateServerAddress(addr);
ProtocolServer newServerChannel = null; ProtocolServer newServerChannel = null;
try { try {
newServerChannel = ProtocolServer.create(this.netprotocol, context, this.serverClassLoader); newServerChannel = ProtocolServer.create(this.netprotocol, context, this.serverClassLoader);
@@ -337,7 +337,7 @@ public abstract class Server<K extends Serializable, C extends Context, R extend
newServerChannel.bind(addr, backlog); newServerChannel.bind(addr, backlog);
newServerChannel.accept(application, this); newServerChannel.accept(application, this);
} catch (IOException e) { } catch (IOException e) {
context.serverAddress = oldAddress; context.updateServerAddress(oldAddress);
throw e; throw e;
} }
this.address = context.serverAddress; this.address = context.serverAddress;

View File

@@ -5,7 +5,8 @@
*/ */
package org.redkale.net.sncp; package org.redkale.net.sncp;
import org.redkale.net.*; import java.net.InetSocketAddress;
import org.redkale.net.Context;
/** /**
* <p> * <p>
@@ -15,8 +16,21 @@ import org.redkale.net.*;
*/ */
public class SncpContext extends Context { public class SncpContext extends Context {
protected byte[] serverAddressBytes;
protected int serverAddressPort;
public SncpContext(SncpContextConfig config) { public SncpContext(SncpContextConfig config) {
super(config); super(config);
this.serverAddressBytes = serverAddress.getAddress().getAddress();
this.serverAddressPort = serverAddress.getPort();
}
@Override
protected void updateServerAddress(InetSocketAddress addr) {
super.updateServerAddress(addr);
this.serverAddressBytes = addr.getAddress().getAddress();
this.serverAddressPort = addr.getPort();
} }
public static class SncpContextConfig extends ContextConfig { public static class SncpContextConfig extends ContextConfig {