Author Topic: Problem with Unity3D coroutine  (Read 19395 times)

GiangNV

  • Not-a-newbie
  • *
  • Posts: 6
  • Karma: 0
    • View Profile
Problem with Unity3D coroutine
« on: May 24, 2012, 05:40:45 AM »
Hi,

I have a problem with RakNet (SWIG wrapper) and Unity3D coroutine.

Code: [Select]
void Start() {
CoStart();
}

void CoStart() {
var peer = RakPeerInterface.GetInstance();
peer.Startup(
1,
new SocketDescriptor(),
1
);
peer.SetOccasionalPing(true);
peer.Connect(
"127.0.0.1",
24568,
null,
0
);
while (true) {
Packet packet = peer.Receive();
print("...");
if (packet != null) {
print("OK");
break;
}
}
}

And coroutine version.

Code: [Select]
void Start() {
StartCoroutine(CoStart());
}

void CoStart() {
var peer = RakPeerInterface.GetInstance();
peer.Startup(
1,
new SocketDescriptor(),
1
);
peer.SetOccasionalPing(true);
peer.Connect(
"127.0.0.1",
24568,
null,
0
);
while (true) {
Packet packet = peer.Receive();
print("...");
if (packet != null) {
print("Not reach here!!!");
break;
}
yield return null;
}
}

In coroutine version, I see "..." message so my loop still working but never receive any message.

What wrong?

Rak'kar

  • Administrator
  • Hero Member
  • *****
  • Posts: 6895
  • Karma: 291
    • View Profile
    • RakNet
Re: Problem with Unity3D coroutine
« Reply #1 on: May 24, 2012, 01:30:08 PM »
What is a Unity3D coroutine? Do they let you run native code now?

Your problem is you can't just pass new SocketDescriptor(). You have to start on a specific port so you know which port to connect to.

GiangNV

  • Not-a-newbie
  • *
  • Posts: 6
  • Karma: 0
    • View Profile
Re: Problem with Unity3D coroutine
« Reply #2 on: May 24, 2012, 09:04:06 PM »
What is a Unity3D coroutine? Do they let you run native code now?
It is the way you can suspend and resume execution of function so you can do multiple task without worry about threading issues (because it is just scheduler, not really concurrent). In my sample code I use yield statement to yield control and resume it in the next frame. I'm not sure it can work with native code, but in theory it is possible (code still sequential execute in main thread).

Your problem is you can't just pass new SocketDescriptor(). You have to start on a specific port so you know which port to connect to.
This is client code, and in non-coroutine version it work fine.
« Last Edit: May 24, 2012, 09:08:07 PM by GiangNV »

Rak'kar

  • Administrator
  • Hero Member
  • *****
  • Posts: 6895
  • Karma: 291
    • View Profile
    • RakNet
Re: Problem with Unity3D coroutine
« Reply #3 on: May 24, 2012, 10:03:18 PM »
But how are you running RakNet at all? Unity doesn't allow linking with a DLL, which is required to use SWIG and hence C#

GiangNV

  • Not-a-newbie
  • *
  • Posts: 6
  • Karma: 0
    • View Profile
Re: Problem with Unity3D coroutine
« Reply #4 on: May 24, 2012, 10:06:59 PM »
But how are you running RakNet at all? Unity doesn't allow linking with a DLL, which is required to use SWIG and hence C#
Yes, I use SWIG wrapper.

Rak'kar

  • Administrator
  • Hero Member
  • *****
  • Posts: 6895
  • Karma: 291
    • View Profile
    • RakNet
Re: Problem with Unity3D coroutine
« Reply #5 on: May 24, 2012, 10:07:36 PM »
And how do you link to the DLL that SWIG produces? Unity doesn't allow that.

GiangNV

  • Not-a-newbie
  • *
  • Posts: 6
  • Karma: 0
    • View Profile
Re: Problem with Unity3D coroutine
« Reply #6 on: May 24, 2012, 10:10:01 PM »
And how do you link to the DLL that SWIG produces? Unity doesn't allow that.
http://unity3d.com/support/documentation/Manual/Plugins.html

Mono-Unity allow you call native function.

Rak'kar

  • Administrator
  • Hero Member
  • *****
  • Posts: 6895
  • Karma: 291
    • View Profile
    • RakNet
Re: Problem with Unity3D coroutine
« Reply #7 on: May 24, 2012, 10:52:12 PM »
Regarding your original code, it's not going to connect because you didn't specify a port to use with Startup(). It's probably returning the packet ID_CONNECTION_ATTEMPT_FAILED. I don't know why a non-coroutine would work and a coroutine would, I know nothing about unity.

GiangNV

  • Not-a-newbie
  • *
  • Posts: 6
  • Karma: 0
    • View Profile
Re: Problem with Unity3D coroutine
« Reply #8 on: May 24, 2012, 11:00:54 PM »
Regarding your original code, it's not going to connect because you didn't specify a port to use with Startup(). It's probably returning the packet ID_CONNECTION_ATTEMPT_FAILED. I don't know why a non-coroutine would work and a coroutine would, I know nothing about unity.
I can't receive any messages with coroutine even ID_CONNECTION_ATTEMPT_FAILED, nothing.

About Startup() issues, it is your tutorial code. Follow it Startup() for client side need not specify the port to use (because we are connecting not listening?).

Code: [Select]
#include <stdio.h>
#include "RakPeerInterface.h"

#define MAX_CLIENTS 10
#define SERVER_PORT 60000

int main(void)
{
char str[512];
RakNet::RakPeerInterface *peer = RakNet::RakPeerInterface::GetInstance();
bool isServer;

printf("(C) or (S)erver?\n");
gets(str);
if ((str[0]=='c')||(str[0]=='C'))
{
SocketDescriptor sd;
peer->Startup(1,&sd, 1);
isServer = false;
} else {
SocketDescriptor sd(SERVER_PORT,0);
peer->Startup(MAX_CLIENTS, &sd, 1);
isServer = true;
}


// TODO - Add code body here

RakNet::RakPeerInterface::DestroyInstance(peer);

return 0;
}
« Last Edit: May 24, 2012, 11:03:41 PM by GiangNV »

Rak'kar

  • Administrator
  • Hero Member
  • *****
  • Posts: 6895
  • Karma: 291
    • View Profile
    • RakNet
Re: Problem with Unity3D coroutine
« Reply #9 on: May 25, 2012, 09:07:02 PM »
OK I didn't know that was client code. Anyway, I know nothing about unity so can't answer about coroutines. Is it possible to debug the RakNet dll when using it with Unity?

GiangNV

  • Not-a-newbie
  • *
  • Posts: 6
  • Karma: 0
    • View Profile
Re: Problem with Unity3D coroutine
« Reply #10 on: May 29, 2012, 10:40:27 PM »
OK I didn't know that was client code. Anyway, I know nothing about unity so can't answer about coroutines. Is it possible to debug the RakNet dll when using it with Unity?
I have workaround by using another busy thread to create and poll raknet, it work well.

I think the low frequency of coroutine (= FPS) cause this problem.