microsoft / MixedRealityToolkit

The MixedRealityToolkit is a collection of scripts and components intended to accelerate the development of mixed reality applications targeting Windows Mixed Reality.
MIT License
860 stars 277 forks source link

XSocketManager::GetLocalMachineAddresses() returns an empty list #101

Open valente-illogic opened 7 years ago

valente-illogic commented 7 years ago

Hello, I'm trying to implement some network functionalities in my UWP holographic app. My plan is to use RakNet API so your CommonUniversal project looks exactly what I need.

I statically linked XTools.Common.lib and RakNet_UAP_x86_Release.lib to my project. Then I tried to run a RakNet example application but it fails on retrieving the local ip addresses: both RakPeerInterface::GetInstance()->GetNumberOfAddresses() and XTools::XSocketManager::GetLocalMachineAddresses() return an empty list.

I managed to send 255.255.255.255 broadcast packets so I guess that the RakNet setting is ok. However I cannot receive broadcast packets or, as I said, get the local IPs.

Is this a limitation of the UWP? Should I give special permissions to my app?

Thanks

belveder79 commented 7 years ago

Hi,

it's not an answer to your question, but nice to know someone else also wants to work with an UWP build of Raknet (for whatever reason). I just discovered today that there is a Raknet build included in this toolkit, and I tried to make my own sample code work with that. A word of caution - I tried to use the Raknet DLL with a managed DLL and call some functions based on sample code from the official Raknet repo, and the Socket implementation for UWP does not work with these... I'm still investigating what the actual problem is, but it seems that not everything works the same way...

Best regards ...

valente-illogic commented 7 years ago

Thank you @belveder79. I'd like to share with you my current test, maybe you'll find it interesting. I'm creating a simple local server+client application: using XTools::XSocketManager API the client connection works just fine; using RakNet API directly instead, my peers cannot receive any packet.

#define uwplog(msg) { std::wstringstream ss; ss << msg; OutputDebugStringW(ss.str().c_str()); }

class SimpleConnectionListener : public XTools::IncomingXSocketListener
{
public:
    void OnNewConnection(const XTools::XSocketPtr& newConnection)
    {
        uwplog("New connection: " << newConnection->GetID()); // this works!
    }
};

// The main function creates an IFrameworkViewSource for our app, and runs the app.
[Platform::MTAThread]
int main(Platform::Array<Platform::String^>^)
{

    char* host = "localhost";
    int port = 1234;

#if true // change this to false if you want to test RakNet API

    XTools::XSocketManagerPtr server = XTools::XSocketManager::Create();
    SimpleConnectionListener *listener = new SimpleConnectionListener();
    XTools::ReceiptPtr listenerReceipt = server->AcceptConnections(port, 2, listener);
    server->Update();

    XTools::XSocketManagerPtr client = XTools::XSocketManager::Create();
    XTools::XSocketPtr connection = client->OpenConnection(host, port);

    while (true)
    {
        client->Update();
        server->Update();
        std::this_thread::sleep_for(std::chrono::milliseconds(5));
    }

#else

    RakNet::RakPeerInterface* server = RakNet::RakPeerInterface::GetInstance();
    RakNet::SocketDescriptor serverSocket(port, NULL);
    server->Startup(2, &serverSocket, 1);
    server->SetMaximumIncomingConnections(2);

    RakNet::RakPeerInterface* client = RakNet::RakPeerInterface::GetInstance();
    RakNet::SocketDescriptor clientSocket(0, NULL);
    client->Startup(1, &clientSocket, 1);
    client->Connect(host, serverSocket.port, NULL, 0);

    while (true)
    {
        while (RakNet::Packet *p = server->Receive())
            uwplog("Server received packet " << (int) p->data[0] << "\n");  // this doesn't work!

        while (RakNet::Packet *p = client->Receive())
            uwplog("Client received packet " << (int) p->data[0] << "\n");  // this doesn't work!

        client->AdvertiseSystem("255.255.255.255", port, "hello", 5); // this works! tested with Wireshark
    }

#endif

    return 0;
}
belveder79 commented 7 years ago

Hi, thanks a lot for sharing that code snippet. I'm encountering exactly that problem right now, so it seems that I will give the XTools a try and ping you then... Best, Clemens

valente-illogic commented 7 years ago

I solved it! :) In UWP the main RakNet thread is disabled (you can see that RAKPEER_USER_THREADED is defined in RakNetDefinesOverrides.h). This means that the user has to manually call the RakPeer::RunUpdateCycle() method in the update cycle, as this comment says. I tried it and now I can receive packets correctly. However, RakPeerInterface::GetInstance()->GetNumberOfAddresses() still returns an empty list...

belveder79 commented 7 years ago

you are my hero of the day! It seems to work so far... - did noth check the GetNumberofAddresses() stuff however...