-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#3776: Expose ChannelInitializerHolder in protocol module
- Loading branch information
1 parent
4dad940
commit 0070421
Showing
8 changed files
with
184 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
protocol/src/main/java/net/md_5/bungee/protocol/channel/BungeeChannelInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package net.md_5.bungee.protocol.channel; | ||
|
||
import io.netty.channel.Channel; | ||
import io.netty.channel.ChannelInitializer; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
/** | ||
* This class hold a netty channel initializer that calls the given {@link ChannelAcceptor}. | ||
* Use {@link BungeeChannelInitializer#create(ChannelAcceptor)} to create a new instance. | ||
* <p> | ||
* Please note that this API is unsafe and doesn't provide any guarantees about | ||
* the stability of the channel pipeline or the API itself. Use at your own | ||
* risk. | ||
*/ | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public abstract class BungeeChannelInitializer | ||
{ | ||
|
||
public abstract ChannelAcceptor getChannelAcceptor(); | ||
|
||
public abstract ChannelInitializer<Channel> getChannelInitializer(); | ||
|
||
/** | ||
* Creates a new instance of BungeeChannelInitializer | ||
* | ||
* @param acceptor the {@link ChannelAcceptor} that will accept the channel | ||
* and initializer the pipeline | ||
* @return {@link BungeeChannelInitializer} containing a cached | ||
* {@link ChannelInitializer} that will call the acceptor | ||
*/ | ||
public static BungeeChannelInitializer create(ChannelAcceptor acceptor) | ||
{ | ||
return new BungeeChannelInitializer() | ||
{ | ||
@Getter | ||
private final ChannelAcceptor channelAcceptor = acceptor; | ||
|
||
@Getter // cache the ChannelInitializer | ||
private final ChannelInitializer<Channel> channelInitializer = new ChannelInitializer<Channel>() | ||
{ | ||
@Override | ||
protected void initChannel(Channel channel) throws Exception | ||
{ | ||
if ( !getChannelAcceptor().accept( channel ) ) | ||
{ | ||
channel.close(); | ||
} | ||
} | ||
}; | ||
}; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
protocol/src/main/java/net/md_5/bungee/protocol/channel/ChannelAcceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package net.md_5.bungee.protocol.channel; | ||
|
||
import io.netty.channel.Channel; | ||
|
||
@FunctionalInterface | ||
public interface ChannelAcceptor | ||
{ | ||
|
||
/** | ||
* Inside this method the pipeline should be initialized. | ||
* | ||
* @param channel the channel to be accepted and initialized | ||
* @return if the channel was accepted | ||
*/ | ||
boolean accept(Channel channel); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters