Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

breaking: removes obsolete message api and NetworkMessage #526

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -19,24 +19,6 @@ public static class MessagePacker
// avoid large amounts of allocations.
static NetworkWriter packWriter = new NetworkWriter();

// pack message before sending
// -> pass writer instead of byte[] so we can reuse it
[Obsolete("Use Pack<T> instead")]
public static byte[] PackMessage(int msgType, MessageBase msg)
{
// reset cached writer length and position
packWriter.SetLength(0);

// write message type
packWriter.Write((short)msgType);

// serialize message into writer
msg.Serialize(packWriter);

// return byte[]
return packWriter.ToArray();
}

// pack message before sending
public static byte[] Pack<T>(T message) where T : MessageBase
{
@@ -24,15 +24,15 @@ public abstract class MessageBase

// ---------- General Typed Messages -------------------

public class StringMessage : MessageBase
public abstract class StringMessage : MessageBase
{
public string value;

public StringMessage()
protected StringMessage()
{
}

public StringMessage(string v)
protected StringMessage(string v)
{
value = v;
}
@@ -48,15 +48,15 @@ public override void Serialize(NetworkWriter writer)
}
}

public class ByteMessage : MessageBase
public abstract class ByteMessage : MessageBase
{
public byte value;

public ByteMessage()
protected ByteMessage()
{
}

public ByteMessage(byte v)
protected ByteMessage(byte v)
{
value = v;
}
@@ -72,15 +72,15 @@ public override void Serialize(NetworkWriter writer)
}
}

public class BytesMessage : MessageBase
public abstract class BytesMessage : MessageBase
{
public byte[] value;

public BytesMessage()
protected BytesMessage()
{
}

public BytesMessage(byte[] v)
protected BytesMessage(byte[] v)
{
value = v;
}
@@ -96,15 +96,15 @@ public override void Serialize(NetworkWriter writer)
}
}

public class IntegerMessage : MessageBase
public abstract class IntegerMessage : MessageBase
{
public int value;

public IntegerMessage()
protected IntegerMessage()
{
}

public IntegerMessage(int v)
protected IntegerMessage(int v)
{
value = v;
}
@@ -120,15 +120,15 @@ public override void Serialize(NetworkWriter writer)
}
}

public class DoubleMessage : MessageBase
public abstract class DoubleMessage : MessageBase
{
public double value;

public DoubleMessage()
protected DoubleMessage()
{
}

public DoubleMessage(double v)
protected DoubleMessage(double v)
{
value = v;
}
@@ -144,7 +144,7 @@ public override void Serialize(NetworkWriter writer)
}
}

public class EmptyMessage : MessageBase
public abstract class EmptyMessage : MessageBase
{
public override void Deserialize(NetworkReader reader)
{
@@ -141,22 +141,6 @@ void RemoveTransportHandlers()
Transport.activeTransport.OnClientError.RemoveListener(OnError);
}

[Obsolete("Use SendMessage<T> instead with no message id instead")]
public bool Send(short msgType, MessageBase msg)
{
if (connection != null)
{
if (connectState != ConnectState.Connected)
{
Debug.LogError("NetworkClient Send when not connected to a server");
return false;
}
return connection.Send(msgType, msg);
}
Debug.LogError("NetworkClient Send with no connection");
return false;
}

public bool Send<T>(T message) where T : MessageBase
{
if (connection != null)
@@ -267,47 +251,21 @@ internal void RegisterSystemHandlers(bool localClient)
RegisterHandler<SyncEventMessage>(ClientScene.OnSyncEventMessage);
}

[Obsolete("Use RegisterHandler<T> instead")]
public void RegisterHandler(int msgType, NetworkMessageDelegate handler)
{
if (handlers.ContainsKey(msgType))
{
if (LogFilter.Debug) { Debug.Log("NetworkClient.RegisterHandler replacing " + msgType); }
}
handlers[msgType] = handler;
}

[Obsolete("Use RegisterHandler<T> instead")]
public void RegisterHandler(MsgType msgType, NetworkMessageDelegate handler)
{
RegisterHandler((int)msgType, handler);
}

public void RegisterHandler<T>(Action<NetworkConnection, T> handler) where T : MessageBase, new()
{
int msgType = MessageBase.GetId<T>();
if (handlers.ContainsKey(msgType))
{
if (LogFilter.Debug) { Debug.Log("NetworkClient.RegisterHandler replacing " + msgType); }
}
handlers[msgType] = (networkMessage) =>
handlers[msgType] = (conn, reader) =>
{
handler(networkMessage.conn, networkMessage.ReadMessage<T>());
T message = new T();
message.Deserialize(reader);
handler(conn, message);
};
}

[Obsolete("Use UnregisterHandler<T> instead")]
public void UnregisterHandler(int msgType)
{
handlers.Remove(msgType);
}

[Obsolete("Use UnregisterHandler<T> instead")]
public void UnregisterHandler(MsgType msgType)
{
UnregisterHandler((int)msgType);
}

public void UnregisterHandler<T>() where T : MessageBase
{
// use int to minimize collisions
@@ -125,14 +125,6 @@ internal void RemovePlayerController()
playerController = null;
}

[Obsolete("use Send<T> instead")]
public virtual bool Send(int msgType, MessageBase msg, int channelId = Channels.DefaultReliable)
{
// pack message and send
byte[] message = MessagePacker.PackMessage(msgType, msg);
return SendBytes(message, channelId);
}

public virtual bool Send<T>(T msg, int channelId = Channels.DefaultReliable) where T: MessageBase
{
// pack message and send
@@ -204,14 +196,7 @@ public bool InvokeHandler(int msgType, NetworkReader reader)
{
if (m_MessageHandlers.TryGetValue(msgType, out NetworkMessageDelegate msgDelegate))
{
NetworkMessage message = new NetworkMessage
{
msgType = msgType,
reader = reader,
conn = this
};

msgDelegate(message);
msgDelegate(this, reader);
return true;
}
Debug.LogError("Unknown message ID " + msgType + " connId:" + connectionId);

This file was deleted.

This file was deleted.

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.