NetworkConnection.SendBytes is now protected so that outside classes can't send random bytes to the client anymore. This is better & safer design and allows us to remove the redundant SendBytesToReady function

This commit is contained in:
vis2k 2018-07-26 21:09:59 +02:00
parent 3ceebaec78
commit 0ed342ecb8
4 changed files with 5 additions and 71 deletions

View File

@ -27,7 +27,7 @@ public override bool SendByChannel(short msgType, MessageBase msg, int channelId
public override bool Send(short msgType, MessageBase msg) { return SendByChannel(msgType, msg, Channels.DefaultReliable); } public override bool Send(short msgType, MessageBase msg) { return SendByChannel(msgType, msg, Channels.DefaultReliable); }
public override bool SendUnreliable(short msgType, MessageBase msg) { return SendByChannel(msgType, msg, Channels.DefaultUnreliable); } public override bool SendUnreliable(short msgType, MessageBase msg) { return SendByChannel(msgType, msg, Channels.DefaultUnreliable); }
public override bool SendBytes(byte[] bytes, int channelId) protected override bool SendBytes(byte[] bytes, int channelId)
{ {
m_LocalClient.InvokeBytesOnClient(bytes, channelId); m_LocalClient.InvokeBytesOnClient(bytes, channelId);
return true; return true;
@ -52,7 +52,7 @@ public override bool SendByChannel(short msgType, MessageBase msg, int channelId
public override bool Send(short msgType, MessageBase msg) { return SendByChannel(msgType, msg, Channels.DefaultReliable); } public override bool Send(short msgType, MessageBase msg) { return SendByChannel(msgType, msg, Channels.DefaultReliable); }
public override bool SendUnreliable(short msgType, MessageBase msg) { return SendByChannel(msgType, msg, Channels.DefaultUnreliable); } public override bool SendUnreliable(short msgType, MessageBase msg) { return SendByChannel(msgType, msg, Channels.DefaultUnreliable); }
public override bool SendBytes(byte[] bytes, int channelId) protected override bool SendBytes(byte[] bytes, int channelId)
{ {
if (bytes.Length == 0) if (bytes.Length == 0)
{ {

View File

@ -302,21 +302,6 @@ public virtual void Disconnect()
} }
} }
public bool SendBytes(byte[] data, int channelId)
{
if (m_Connection != null)
{
if (m_AsyncConnect != ConnectState.Connected)
{
if (LogFilter.logError) { Debug.LogError("NetworkClient SendBytes when not connected to a server"); }
return false;
}
return m_Connection.SendBytes(data, channelId);
}
if (LogFilter.logError) { Debug.LogError("NetworkClient SendBytes with no connection"); }
return false;
}
public bool SendByChannel(short msgType, MessageBase msg, int channelId) public bool SendByChannel(short msgType, MessageBase msg, int channelId)
{ {
#if UNITY_EDITOR #if UNITY_EDITOR

View File

@ -188,7 +188,9 @@ public virtual bool SendByChannel(short msgType, MessageBase msg, int channelId)
public virtual bool Send(short msgType, MessageBase msg) { return SendByChannel(msgType, msg, Channels.DefaultReliable); } public virtual bool Send(short msgType, MessageBase msg) { return SendByChannel(msgType, msg, Channels.DefaultReliable); }
public virtual bool SendUnreliable(short msgType, MessageBase msg) { return SendByChannel(msgType, msg, Channels.DefaultUnreliable); } public virtual bool SendUnreliable(short msgType, MessageBase msg) { return SendByChannel(msgType, msg, Channels.DefaultUnreliable); }
public virtual bool SendBytes(byte[] bytes, int channelId) // protected because no one except NetworkConnection should ever send bytes directly to the client, as they
// would be detected as some kind of message. send messages instead.
protected virtual bool SendBytes(byte[] bytes, int channelId)
{ {
if (logNetworkMessages) if (logNetworkMessages)
{ {

View File

@ -283,59 +283,6 @@ static bool SendToObservers(GameObject contextObj, short msgType, MessageBase ms
return false; return false;
} }
// End users should not send random bytes
// because the other side might interpret them as messages
static internal void SendBytesToReady(GameObject contextObj, byte[] buffer, int channelId)
{
if (contextObj == null)
{
// no context.. send to all ready connections
bool success = true;
for (int i = 0; i < connections.Count; i++)
{
NetworkConnection conn = connections[i];
if (conn != null && conn.isReady)
{
if (!conn.SendBytes(buffer, channelId))
{
success = false;
}
}
}
if (!success)
{
if (LogFilter.logWarn) { Debug.LogWarning("SendBytesToReady failed"); }
}
return;
}
var uv = contextObj.GetComponent<NetworkIdentity>();
try
{
bool success = true;
for (int i = 0; i < uv.observers.Count; ++i)
{
var conn = uv.observers[i];
if (conn.isReady)
{
if (!conn.SendBytes(buffer, channelId))
{
success = false;
}
}
}
if (!success)
{
if (LogFilter.logWarn) { Debug.LogWarning("SendBytesToReady failed for " + contextObj); }
}
}
catch (NullReferenceException)
{
// observers may be null if object has not been spawned
if (LogFilter.logWarn) { Debug.LogWarning("SendBytesToReady object " + contextObj + " has not been spawned"); }
}
}
static public bool SendByChannelToAll(short msgType, MessageBase msg, int channelId) static public bool SendByChannelToAll(short msgType, MessageBase msg, int channelId)
{ {
if (LogFilter.logDev) { Debug.Log("Server.SendByChannelToAll id:" + msgType); } if (LogFilter.logDev) { Debug.Log("Server.SendByChannelToAll id:" + msgType); }