Author Topic: No ReplicaManager3  (Read 13964 times)

Inverness

  • Not-a-newbie
  • *
  • Posts: 5
  • Karma: 0
    • View Profile
No ReplicaManager3
« on: June 16, 2012, 01:39:10 PM »
I just discovered that ReplicaManager3 is not included in the SWIG bindings for C#. I assume this is because Unity does not use it, but I'm not using Unity and I need it for my project. I'd like to know what changes I need to make to have it work successfully. I'd also recommend including this in the future or at least making it optional.

Edit: It seems GetWorldCount() and GetWorldIdAtIndex() in ReplicaManager3 do not have definitions in 4.04.
« Last Edit: June 16, 2012, 01:52:17 PM by Inverness »

Rak'kar

  • Administrator
  • Hero Member
  • *****
  • Posts: 6895
  • Karma: 291
    • View Profile
    • RakNet
Re: No ReplicaManager3
« Reply #1 on: June 17, 2012, 12:44:15 AM »
I'm not positive but I think SWIG did this automatically because you have to derive from ReplicaManager3 for it to work. But how can you derive a C++ class in C#? The SwigInterfaceFiles directory does not say to exclude ReplicaManager3 and I see that SWIG didn't generate it either.

Inverness

  • Not-a-newbie
  • *
  • Posts: 5
  • Karma: 0
    • View Profile
Re: No ReplicaManager3
« Reply #2 on: June 17, 2012, 07:42:15 AM »
But how can you derive a C++ class in C#?
SWIG has a feature just for this: http://www.swig.org/Doc1.3/CSharp.html#csharp_directors

Rak'kar

  • Administrator
  • Hero Member
  • *****
  • Posts: 6895
  • Karma: 291
    • View Profile
    • RakNet
Re: No ReplicaManager3
« Reply #3 on: June 18, 2012, 09:28:24 AM »
I'll get to this when I can. If you don't want to wait, the SWIG interface files are at DependentExtensions\Swig\SwigInterfaceFiles . I'm not all that familiar with SWIG myself so it takes me a while unfortunately.

Inverness

  • Not-a-newbie
  • *
  • Posts: 5
  • Karma: 0
    • View Profile
Re: No ReplicaManager3
« Reply #4 on: June 22, 2012, 11:42:38 AM »
I managed to get this added and compiled, but I realized a problem after getting access violations several times. I can't subclass Connection_RM3 in C# but then have the C++ code manage that derived class, it only handles the base class.

I'm not sure what the best solution to this issue is.

Edit: I also looked at the how SWIG handles the whole overriding thing and it seems like there is a way too much overhead, at least for Replica3 instances. Swig adds 172 bytes to the size of Replica3 in order to fit in callbacks (function pointers) for all the virtuals.

I think a solution will be needed that involves subclassing Replica3 and anything else in order to redirect their requests to ReplicaManager3 or something while providing an ID that the managed code can use to retrieve the appropriate instance. Something similar would be done for Connection_RM3::AllocReplica() too. There shouldn't be any need to change any existing RakNet code.

Example of what I mean from the C# end:
Code: [Select]
public class ReplicaManager : RakNet.ReplicaManager3
{
    private readonly Dictionary<ulong, MyReplica> _replicas = new Dictionary<ulong, MyReplica>();

    public override void OnReplicaSerializeConstruction(ulong replicaID, Replica3 base, BitStream bs, Connection_RM3 destination)
    {
        var replica = _replicas[replicaID];
        // do stuff
    }

    public override void OnReplicaSerialize(ulong replicaID, Replica3 base, SerializeParameters sp)
    {
        var replica = _replicas[replicaID];
        // do stuff
    }
}
« Last Edit: June 22, 2012, 12:04:37 PM by Inverness »

Rak'kar

  • Administrator
  • Hero Member
  • *****
  • Posts: 6895
  • Karma: 291
    • View Profile
    • RakNet
Re: No ReplicaManager3
« Reply #5 on: June 22, 2012, 10:08:16 PM »
The C++ code would call into the virtual functions on the base class. But you would need to pass an instance of the derived class when you allocate it. You are saying that's not possible then?

Inverness

  • Not-a-newbie
  • *
  • Posts: 5
  • Karma: 0
    • View Profile
Re: No ReplicaManager3
« Reply #6 on: June 23, 2012, 12:00:32 AM »
The C++ code would call into the virtual functions on the base class. But you would need to pass an instance of the derived class when you allocate it. You are saying that's not possible then?
Well the C++ functions only work with the underlying C++ object. And if don't save a reference to that derived C# object somewhere it will be garbage collected and dispose of the underlying C++ object unexpectedly, which is what happened to me and caused access violations. You can fix that by keeping a reference to the object somewhere in C#.

The other problem is that when the C++ code gives you a Replica3 or Connection_RM3, you can't cast it to your derived C# class or anything because that part isn't managed by the C++ code. You would need some way of re-associating the C++ object with the derived C# object.

Rak'kar

  • Administrator
  • Hero Member
  • *****
  • Posts: 6895
  • Karma: 291
    • View Profile
    • RakNet
Re: No ReplicaManager3
« Reply #7 on: June 23, 2012, 10:58:03 AM »
What about porting the ReplicaManager code to C# directly? You could derive it from PluginInterface2 so you can still attach it to an instance of RakPeer. I imagine the hardest part would be changing the poitners to the references

Inverness

  • Not-a-newbie
  • *
  • Posts: 5
  • Karma: 0
    • View Profile
Re: No ReplicaManager3
« Reply #8 on: June 24, 2012, 12:27:42 AM »
What about porting the ReplicaManager code to C# directly? You could derive it from PluginInterface2 so you can still attach it to an instance of RakPeer. I imagine the hardest part would be changing the poitners to the references
That is a solution. A simple one, but maybe a little time consuming. I don't know when I might be able to get to it, but if I decide to do that I'll make sure to upload the code here when I'm done.

Rak'kar

  • Administrator
  • Hero Member
  • *****
  • Posts: 6895
  • Karma: 291
    • View Profile
    • RakNet
Re: No ReplicaManager3
« Reply #9 on: June 24, 2012, 12:30:21 AM »
Thanks in advance if you do so. I want to be more helpful but my experience with C# is limited to the SWIG wrapper.

gallexme

  • Not-a-newbie
  • *
  • Posts: 2
  • Karma: 0
    • View Profile
Re: No ReplicaManager3
« Reply #10 on: October 12, 2012, 04:51:38 PM »
so is there now a (easy) way to use the replicamanager?