Chromanoid hat geschrieben:Hast du schon mal SDL_Net angeschaut?
Ja. Das is so in etwa das, was ich benutzen will um nen NetworkCommunikator zu implementieren. Ich denke das das, was ich vorhab etwas unintuitiv für die meisten Programmierer sein wird, weil ich versuche Threads und Entfernte Prozesse gleich zu behandeln, also insbesondere vor dem Nutzer der Bibliothek völlig verstecke, dass es sowas wie ein Netzwerk überhaupt gibt. Das ist zumindest mal das Ziel.
Ich denk ich werd das mit boost::asio Implementieren, weil ich sowieso schon relativ viel boost libs verwende unds dann auf eine mehr auch nicht ankommt. Ist auf jeden Fall schöner als SDL_net einzubauen. Zumal ich glaube, dass die asio Entwickler fähiger und aktiver sind.
EDIT:
So hab mich jetzt ma zwei Tage hingesetz und mir ist jetzt selbst auch wesentlich klarer, wie so ne Bibliothek eigentlich funktionieren sollte. Für locale Kommunikation hab ichs im Prinzip schon gemacht, benutzung sähe dann in etwa so aus:
- Code: Alles auswählen
#include <iostream>
#include "../libmpl/include/mpl.h"
#include <boost/thread.hpp>
#include <cstring>
struct test
{
mpl::LocalCommunicator* comm;
test(mpl::LocalCommunicator* comm)
:comm(comm){};
test(const test& t)
:comm(t.comm){};
void operator()()
{
mpl::Message m;
mpl::Client c(comm);
while(!c.recv(m));
if(!strncmp(m.get<const char*>(), "Load1", m.getLength()))
{
while(!c.recv(m));
std::cout << "Load file " << m.get<const char*>() << std::endl;
c.send("success", m.getSource());
}
else
c.send("unknown command!", m.getSource());
}
};
using namespace std;
int main()
{
cout << "Creating local Communicator..." << endl;
mpl::LocalCommunicator comm;
{
cout << "Creating local Clients..." << endl;
mpl::Client c1(&comm), c2(&comm);
boost::thread* slave = 0;
{
test t(&comm);
slave = new boost::thread(t);
}
cout << "Clients connected." << endl;
c1.send("Test", c2.id());
c1.send("Load1", 3);
c1.send("random", 3);
mpl::Message m;
while(!c2.recv(m));
cout << "c2 got " << m.get<const char*>() << endl;
while(!c1.recv(m));
slave->join();
cout << "thread answered: " << m.get<const char*>() << endl;
}
cout << "Wait for Communicator to die.." << endl;
return 0;
}
Ausgabe ist:
- Code: Alles auswählen
Creating local Communicator...
Creating local Clients...
Clients connected.
Load file random
c2 got Test
thread answered: success
Wait for Communicator to die..
stub @ /home/lorddelvin/libmpl/src/mpl/LocalCommunicator.cpp line: 117
LCD just halts; this may lead to problems
Für Netzwerk sollte es im Prinzip ausreichen noch sowas wie mpl::NetworkCommunicator comm_world( irgendeine adresse ) hinzuschreiben und comm_world im Konstruktor von comm mitzugeben. Dann sollte das automatisch alles richtig gemachte werden.
Falls es irgendjemand haben oder weiterentwickeln will schreibt einfach.