mirror of
https://github.com/GTNewHorizons/bdlib.git
synced 2026-10-26 14:23:54 +00:00
Made network message type bound to a specific channel, this should give compile-time errors for sending messages on the wrong channel. Messages should now inherit from YourChannelObject.Message
This commit is contained in:
1 parent
0976fb4acc
commit
5f01716310
7 files changed
+41
-65
No files matched your search
+1
-1
@@ -21,7 +21,7 @@ buildscript {
|
||||
apply plugin: 'scala'
|
||||
apply plugin: 'forge'
|
||||
|
||||
version = "1.4.4"
|
||||
version = "1.4.5"
|
||||
|
||||
if (project.hasProperty('buildnum')) {
|
||||
project.version = project.version + '.' + project.buildnum
|
||||
|
||||
@@ -9,8 +9,8 @@
|
||||
|
||||
package net.bdew.lib.multiblock.network
|
||||
|
||||
import net.bdew.lib.network.Message
|
||||
import net.bdew.lib.network.BaseMessage
|
||||
|
||||
abstract class MsgOutputCfg extends Message {
|
||||
abstract class MsgOutputCfg extends BaseMessage[NetHandler.type] {
|
||||
val output: Int
|
||||
}
|
||||
@@ -9,4 +9,4 @@
|
||||
|
||||
package net.bdew.lib.network
|
||||
|
||||
abstract class Message extends Serializable
|
||||
abstract class BaseMessage[CH <: NetChannel] extends Serializable
|
||||
@@ -13,18 +13,49 @@ import java.util
|
||||
|
||||
import cpw.mods.fml.common.network.{FMLEmbeddedChannel, FMLOutboundHandler, NetworkRegistry}
|
||||
import cpw.mods.fml.relauncher.Side
|
||||
import io.netty.channel.{ChannelFutureListener, ChannelHandler}
|
||||
import io.netty.channel.{ChannelFutureListener, ChannelHandler, ChannelHandlerContext, SimpleChannelInboundHandler}
|
||||
import net.bdew.lib.{BdLib, Misc}
|
||||
import net.minecraft.entity.player.EntityPlayerMP
|
||||
import net.minecraft.network.NetHandlerPlayServer
|
||||
|
||||
class NetChannel(val name: String) {
|
||||
var channels: util.EnumMap[Side, FMLEmbeddedChannel] = null
|
||||
|
||||
type Message = BaseMessage[this.type]
|
||||
|
||||
object ServerHandler extends SimpleChannelInboundHandler[Message] {
|
||||
def channelRead0(ctx: ChannelHandlerContext, msg: Message) {
|
||||
try {
|
||||
val player = ctx.channel().attr(NetworkRegistry.NET_HANDLER).get.asInstanceOf[NetHandlerPlayServer].playerEntity
|
||||
if (serverChain.isDefinedAt(msg, player))
|
||||
serverChain(msg, player)
|
||||
else
|
||||
BdLib.logWarn("Unable to handle message from user %s: %s", player.getDisplayName, msg)
|
||||
} catch {
|
||||
case e: Throwable => BdLib.logErrorException("Error handling packet", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
object ClientHandler extends SimpleChannelInboundHandler[Message] {
|
||||
def channelRead0(ctx: ChannelHandlerContext, msg: Message) {
|
||||
try {
|
||||
if (clientChain.isDefinedAt(msg))
|
||||
clientChain(msg)
|
||||
else
|
||||
BdLib.logWarn("Unable to handle message from server: %s", msg)
|
||||
} catch {
|
||||
case e: Throwable => BdLib.logErrorException("Error handling packet", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def init() {
|
||||
if (channels != null) sys.error("Attempted to initalize a channel twice (%s)".format(name))
|
||||
channels = NetworkRegistry.INSTANCE.newChannel(name, new SerializedMessageCodec)
|
||||
BdLib.logInfo("Initialized network channel '%s' for mod '%s'", name, Misc.getActiveModId)
|
||||
addHandler(Side.SERVER, new NetChannelServerHandler(this))
|
||||
addHandler(Side.CLIENT, new NetChannelClientHandler(this))
|
||||
addHandler(Side.SERVER, ServerHandler)
|
||||
addHandler(Side.CLIENT, ClientHandler)
|
||||
}
|
||||
|
||||
var clientChain = PartialFunction.empty[Message, Unit]
|
||||
@@ -38,7 +69,7 @@ class NetChannel(val name: String) {
|
||||
|
||||
private def addHandler(side: Side, handler: ChannelHandler) {
|
||||
val ch = channels.get(side)
|
||||
val name = ch.findChannelHandlerNameForType(classOf[SerializedMessageCodec])
|
||||
val name = ch.findChannelHandlerNameForType(classOf[SerializedMessageCodec[this.type]])
|
||||
ch.pipeline().addAfter(name, side + "Handler", handler)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* https://github.com/bdew/bdlib
|
||||
*
|
||||
* This mod is distributed under the terms of the Minecraft Mod Public
|
||||
* License 1.0, or MMPL. Please check the contents of the license located in
|
||||
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
|
||||
*/
|
||||
|
||||
package net.bdew.lib.network
|
||||
|
||||
import io.netty.channel.{ChannelHandlerContext, SimpleChannelInboundHandler}
|
||||
import net.bdew.lib.BdLib
|
||||
|
||||
class NetChannelClientHandler(ch: NetChannel) extends SimpleChannelInboundHandler[Message] {
|
||||
def channelRead0(ctx: ChannelHandlerContext, msg: Message) {
|
||||
try {
|
||||
if (ch.clientChain.isDefinedAt(msg))
|
||||
ch.clientChain(msg)
|
||||
else
|
||||
BdLib.logWarn("Unable to handle message from server: %s", msg)
|
||||
} catch {
|
||||
case e: Throwable => BdLib.logErrorException("Error handling packet", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* https://github.com/bdew/bdlib
|
||||
*
|
||||
* This mod is distributed under the terms of the Minecraft Mod Public
|
||||
* License 1.0, or MMPL. Please check the contents of the license located in
|
||||
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
|
||||
*/
|
||||
|
||||
package net.bdew.lib.network
|
||||
|
||||
import cpw.mods.fml.common.network.NetworkRegistry
|
||||
import io.netty.channel.{ChannelHandlerContext, SimpleChannelInboundHandler}
|
||||
import net.bdew.lib.BdLib
|
||||
import net.minecraft.network.NetHandlerPlayServer
|
||||
|
||||
class NetChannelServerHandler(ch: NetChannel) extends SimpleChannelInboundHandler[Message] {
|
||||
def channelRead0(ctx: ChannelHandlerContext, msg: Message) {
|
||||
try {
|
||||
val player = ctx.channel().attr(NetworkRegistry.NET_HANDLER).get.asInstanceOf[NetHandlerPlayServer].playerEntity
|
||||
if (ch.serverChain.isDefinedAt(msg, player))
|
||||
ch.serverChain(msg, player)
|
||||
else
|
||||
BdLib.logWarn("Unable to handle message from user %s: %s", player.getDisplayName, msg)
|
||||
} catch {
|
||||
case e: Throwable => BdLib.logErrorException("Error handling packet", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,8 +21,8 @@ import io.netty.handler.codec.MessageToMessageCodec
|
||||
import net.bdew.lib.BdLib
|
||||
|
||||
@Sharable
|
||||
class SerializedMessageCodec extends MessageToMessageCodec[FMLProxyPacket, Message] {
|
||||
def encode(ctx: ChannelHandlerContext, msg: Message, out: util.List[AnyRef]) {
|
||||
class SerializedMessageCodec[T <: NetChannel] extends MessageToMessageCodec[FMLProxyPacket, BaseMessage[T]] {
|
||||
def encode(ctx: ChannelHandlerContext, msg: BaseMessage[T], out: util.List[AnyRef]) {
|
||||
val buff = Unpooled.buffer()
|
||||
val writer = new ObjectOutputStream(new ByteBufOutputStream(buff))
|
||||
writer.writeObject(msg)
|
||||
|
||||
Reference in new issue
Block a user