- Home /
LLAPI Networking: NetworkConnection.TransportRecieve not being used
I'm trying to create my own network interface using the ability to override the virtual functions NetworkConnection.TransportSend and NetworkConnection.TransportRecieve (btw this is a typo in the API, it's spelled receive!), here's my code:
void CreateNetworkClient(ConnectionId _connectionId) {
Debug.Log("creating new network client with connectionId " + _connectionId);
DummyConnection newDummyConnection = new DummyConnection ();
newDummyConnection.Initialize ( _connectionId.ToString (), 0, _connectionId.id, new HostTopology(connectionConfig, 100)); //create a new dummy connection, which we'll assign to the high level API networkClient from its constructor
networkClient = new NetworkClient(newDummyConnection);
networkClient.SetNetworkConnectionClass<DummyConnection> (); //not sure how to use this, because it's supposed to apply this class to /new/ connections
networkClient.connection.logNetworkMessages = true;
Debug.Log ("networkClient class: " + networkClient.networkConnectionClass.Name); //shows DummyConnection
Debug.Log ("networkClient connection Id: " + networkClient.connection.connectionId);
networkManager.client = networkClient;
ClientScene.Ready (networkClient.connection); //tell server we're ready to spawn
if (!networkClient.connection.isConnected)
Debug.LogError ("network client failed to connect");
}
public class DummyConnection : NetworkConnection {
//override the sending function called every time a networkClient or networkServer wants to send data.
public override bool TransportSend(byte[] bytes, int numBytes, int channelId, out byte error) {
Debug.Log ("using dummy connection to send data to " + connectionId);
error = 0;
if (channelId == Channels.DefaultReliable)
SendData (bytes, (short)connectionId, true);
else if (channelId == Channels.DefaultUnreliable)
SendData (bytes, (short)connectionId, false);
else
Debug.LogError ("error in DummyConnection: unknown channel! Try using default reliable or unreliable or adding support");
return true; //probably not good. This might trick it into thinking there's no errors, ToDo: fix
}
//just a debug function
public override void TransportRecieve(byte[] bytes, int numBytes, int channelId)
{
StringBuilder msg = new StringBuilder();
for (int i = 0; i < numBytes; i++)
{
var s = String.Format("{0:X2}", bytes[i]);
msg.Append(s);
if (i > 50) break;
}
Debug.Log("TransportRecieve h:" + hostId + " con:" + connectionId + " bytes:" + numBytes + " " + msg);
HandleBytes(bytes, numBytes, channelId);
}
}
Basically I'm trying to make the networkClient and Server have their NetworkConnection use this class DummyConnection, which forcefully sends the data through my own networking solution. This is sort of an experiment. I "abuse" the parameters in NetworkConnection.Initialize simply putting my own custom connectionId generated by my networking solution, since I'm not using the NetworkConnection for anything else but to feign an actual connection. I'm doing this because I want to still be able to use the high level API with functions such as [SyncVar] and [Command]. So I connect with my own networking solution then add a dummy connection, and I'm trying to get the networkClient and networkServer to use this DummyConnection, but neither side shows the debug log s that should appear from sending through the DummyConnection.
However, setting the connections to use NetworkConnection.logNetworkMessages does indeed indicate that the client is sending data, logging in the console the first message (ClientScene.Ready()).
So my question is, am I setting this up correctly? Because I can't get it to even log errors through the DummyConnection at all.
Answer by hydrix · Jul 06, 2016 at 07:27 AM
It took DAYS to arrive to this solution and the poor documentation was almost entirely to blame: It doesn't actually use TransportSend() and TransportRecieve() to send and receive data all data to the application layer as the documentation implies, or at least most of it. It actually uses simply
Send() which I suspected after noticing that this function was also a virtual function within NetworkConnection. There was nothing wrong with the config at all-
adding a few more override functions within the DummyConnection- one for Send() SendUnreliable() and all the rest solved the problem, now when my code runs ClientScene.Ready() it successfully uses my custom override function!