Progress on Rewriting Packet Handling
Continued building a system for transferring the complete internal states of our dimensions from the server to the client. However, Steven suggested that clients only need minimal data to operate properly, as opposed to the server. My motivation for this more complicated system was the concern that minimal information wouldn't be enough. I'm going to commit my progress, then tear it down and write a much simpler version.
This commit is contained in:
116
StevenDimDoors/mod_pocketDim/messages/DimMessageBuilder.java
Normal file
116
StevenDimDoors/mod_pocketDim/messages/DimMessageBuilder.java
Normal file
@@ -0,0 +1,116 @@
|
||||
package StevenDimDoors.mod_pocketDim.messages;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import StevenDimDoors.mod_pocketDim.core.DimLink;
|
||||
import StevenDimDoors.mod_pocketDim.core.NewDimData;
|
||||
import StevenDimDoors.mod_pocketDim.core.NewDimData.InnerDimLink;
|
||||
import StevenDimDoors.mod_pocketDim.dungeon.DungeonData;
|
||||
import StevenDimDoors.mod_pocketDim.util.Point4D;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
public class DimMessageBuilder implements IMessageBuilder<NewDimData>
|
||||
{
|
||||
public static class DimMessage implements IDataMessage
|
||||
{
|
||||
//We'll use public fields here since this is a data container object and all the fields are immutable
|
||||
//We will not transfer dungeon, link data, or any data on child dimensions
|
||||
//As far as I can tell, the children will handle updating their parents anyway
|
||||
|
||||
public final int ID;
|
||||
public final boolean IsDungeon;
|
||||
public final boolean IsFilled;
|
||||
public final int Depth;
|
||||
public final int PackDepth;
|
||||
public final Integer ParentID;
|
||||
public final int RootID;
|
||||
public final Point4D Origin;
|
||||
public final int Orientation;
|
||||
|
||||
private DimMessage(NewDimData dimension)
|
||||
{
|
||||
ID = dimension.id();
|
||||
IsDungeon = dimension.isDungeon();
|
||||
IsFilled = dimension.isFilled();
|
||||
Depth = dimension.depth();
|
||||
PackDepth = dimension.packDepth();
|
||||
ParentID = (dimension.parent() != null) ? dimension.parent().id() : null;
|
||||
RootID = dimension.root().id();
|
||||
Origin = dimension.origin();
|
||||
Orientation = dimension.orientation();
|
||||
}
|
||||
|
||||
private DimMessage(DataInputStream stream) throws IOException
|
||||
{
|
||||
ID = stream.readInt();
|
||||
IsDungeon = stream.readBoolean();
|
||||
IsFilled = stream.readBoolean();
|
||||
Depth = stream.readInt();
|
||||
PackDepth = stream.readInt();
|
||||
ParentID = stream.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToStream(DataOutputStream stream) throws IOException
|
||||
{
|
||||
//Write a flag indicating that this is a full message and not a key
|
||||
stream.writeBoolean(true);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static class DimKeyMessage implements IDataMessage
|
||||
{
|
||||
//We'll use public fields here since this is a data container object and all the fields are immutable
|
||||
public final int ID;
|
||||
|
||||
private DimKeyMessage(NewDimData dimension)
|
||||
{
|
||||
ID = dimension.id();
|
||||
}
|
||||
|
||||
private DimKeyMessage(DataInputStream stream) throws IOException
|
||||
{
|
||||
ID = stream.readInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToStream(DataOutputStream stream) throws IOException
|
||||
{
|
||||
//Write a flag indicating that this is a key
|
||||
stream.writeBoolean(false);
|
||||
stream.writeInt(ID);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IDataMessage createKey(NewDimData target)
|
||||
{
|
||||
return new DimKeyMessage(target);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IDataMessage createMessage(NewDimData target)
|
||||
{
|
||||
return new DimMessage(target);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IDataMessage read(DataInputStream source) throws IOException
|
||||
{
|
||||
//Check whether the message is a full message or just a key
|
||||
if (source.readBoolean())
|
||||
{
|
||||
return new DimMessage(source);
|
||||
}
|
||||
else
|
||||
{
|
||||
return new DimKeyMessage(source);
|
||||
}
|
||||
}
|
||||
}
|
||||
9
StevenDimDoors/mod_pocketDim/messages/IDataMessage.java
Normal file
9
StevenDimDoors/mod_pocketDim/messages/IDataMessage.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package StevenDimDoors.mod_pocketDim.messages;
|
||||
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public interface IDataMessage
|
||||
{
|
||||
public void writeToStream(DataOutputStream stream) throws IOException;
|
||||
}
|
||||
11
StevenDimDoors/mod_pocketDim/messages/IMessageBuilder.java
Normal file
11
StevenDimDoors/mod_pocketDim/messages/IMessageBuilder.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package StevenDimDoors.mod_pocketDim.messages;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public interface IMessageBuilder<T>
|
||||
{
|
||||
public IDataMessage createKey(T target);
|
||||
public IDataMessage createMessage(T target);
|
||||
public IDataMessage read(DataInputStream source) throws IOException;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package StevenDimDoors.mod_pocketDim.messages;
|
||||
|
||||
public interface IUpdateWatcher
|
||||
{
|
||||
public void onCreated(IDataMessage message);
|
||||
public void onUpdated(IDataMessage message);
|
||||
public void onDeleted(IDataMessage message);
|
||||
}
|
||||
134
StevenDimDoors/mod_pocketDim/messages/LinkMessageBuilder.java
Normal file
134
StevenDimDoors/mod_pocketDim/messages/LinkMessageBuilder.java
Normal file
@@ -0,0 +1,134 @@
|
||||
package StevenDimDoors.mod_pocketDim.messages;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import StevenDimDoors.mod_pocketDim.core.DimLink;
|
||||
import StevenDimDoors.mod_pocketDim.util.Point4D;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
public class LinkMessageBuilder implements IMessageBuilder<DimLink>
|
||||
{
|
||||
public static class LinkMessage implements IDataMessage
|
||||
{
|
||||
//We'll use public fields here since this is a data container object and all the fields are immutable
|
||||
public final Point4D Source;
|
||||
public final Point4D Destination;
|
||||
public final int LinkType;
|
||||
public final Point4D Parent;
|
||||
public final ImmutableList<Point4D> Children;
|
||||
|
||||
private LinkMessage(DimLink link)
|
||||
{
|
||||
// TODO: In the case that a child's parent has been removed but the rest of the group still exists,
|
||||
// this group bond will be lost to this link on the client side. Currently, that's not a problem since
|
||||
// destination data and groups don't matter to the client, but it's something to think about later.
|
||||
|
||||
Source = link.source();
|
||||
Destination = link.destination();
|
||||
LinkType = link.linkType();
|
||||
Parent = link.parent().source();
|
||||
ImmutableList.Builder<Point4D> builder = new ImmutableList.Builder<Point4D>();
|
||||
for (DimLink child : link.children())
|
||||
{
|
||||
builder.add(child.source());
|
||||
}
|
||||
Children = builder.build();
|
||||
}
|
||||
|
||||
private LinkMessage(DataInputStream stream) throws IOException
|
||||
{
|
||||
Source = Point4D.read(stream);
|
||||
Parent = Point4D.read(stream);
|
||||
if (Parent == null)
|
||||
{
|
||||
Destination = Point4D.read(stream);
|
||||
LinkType = stream.readInt();
|
||||
}
|
||||
else
|
||||
{
|
||||
Destination = null;
|
||||
LinkType = -1;
|
||||
}
|
||||
int childCount = stream.readInt();
|
||||
ImmutableList.Builder<Point4D> builder = new ImmutableList.Builder<Point4D>();
|
||||
for (int k = 0; k < childCount; k++)
|
||||
{
|
||||
builder.add(Point4D.read(stream));
|
||||
}
|
||||
Children = builder.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToStream(DataOutputStream stream) throws IOException
|
||||
{
|
||||
//Write a flag indicating that this is a full message and not a key
|
||||
stream.writeBoolean(true);
|
||||
Point4D.write(Source, stream);
|
||||
Point4D.write(Parent, stream);
|
||||
//A link only has its own destination information if it has no parent to provide it
|
||||
if (Parent == null)
|
||||
{
|
||||
Point4D.write(Destination, stream);
|
||||
stream.writeInt(LinkType);
|
||||
}
|
||||
stream.writeInt(Children.size());
|
||||
for (Point4D child : Children)
|
||||
{
|
||||
Point4D.write(child, stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class LinkKeyMessage implements IDataMessage
|
||||
{
|
||||
//We'll use public fields here since this is a data container object and all the fields are immutable
|
||||
public final Point4D Source;
|
||||
|
||||
private LinkKeyMessage(DimLink link)
|
||||
{
|
||||
Source = link.source();
|
||||
}
|
||||
|
||||
private LinkKeyMessage(DataInputStream stream) throws IOException
|
||||
{
|
||||
Source = Point4D.read(stream);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToStream(DataOutputStream stream) throws IOException
|
||||
{
|
||||
//Write a flag indicating that this is a key
|
||||
stream.writeBoolean(false);
|
||||
Point4D.write(Source, stream);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IDataMessage createKey(DimLink target)
|
||||
{
|
||||
return new LinkKeyMessage(target);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IDataMessage createMessage(DimLink target)
|
||||
{
|
||||
return new LinkMessage(target);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IDataMessage read(DataInputStream source) throws IOException
|
||||
{
|
||||
//Check whether the message is a full message or just a key
|
||||
if (source.readBoolean())
|
||||
{
|
||||
return new LinkMessage(source);
|
||||
}
|
||||
else
|
||||
{
|
||||
return new LinkKeyMessage(source);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package StevenDimDoors.mod_pocketDim.messages;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class UpdateWatcherProxy implements IUpdateWatcher
|
||||
{
|
||||
private List<IUpdateWatcher> watchers;
|
||||
|
||||
public UpdateWatcherProxy()
|
||||
{
|
||||
watchers = new ArrayList<IUpdateWatcher>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreated(IDataMessage message)
|
||||
{
|
||||
for (IUpdateWatcher receiver : watchers)
|
||||
{
|
||||
receiver.onCreated(message);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpdated(IDataMessage message)
|
||||
{
|
||||
for (IUpdateWatcher receiver : watchers)
|
||||
{
|
||||
receiver.onUpdated(message);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDeleted(IDataMessage message)
|
||||
{
|
||||
for (IUpdateWatcher receiver : watchers)
|
||||
{
|
||||
receiver.onDeleted(message);
|
||||
}
|
||||
}
|
||||
|
||||
public void registerReceiver(IUpdateWatcher receiver)
|
||||
{
|
||||
watchers.add(receiver);
|
||||
}
|
||||
|
||||
public boolean unregisterReceiver(IUpdateWatcher receiver)
|
||||
{
|
||||
return watchers.remove(receiver);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user