c++ ptlib PNotifier use in OPAL - c++

I'm using OpalVoip for making a softphone application and i got stuck with following:
void MyManager::main()
{
//variables
PNotifier notify; // this is the problem
//it is the template PNotifierFunctionTemplate<INT>
//more stuff
listener->Open(notify);
//listener is of type OpalListenerUDP
//it should start the listener and wait for incoming connection
//but however i need to pass this NOTIFY which should be the INT parameter to the
//new OpalTransport instance created by the listener...
//however OpalListenerUDP only creates OpalTransport after the connection is
//accepted as so in following: OpalListenerUDP::Accept(/*time interval*/)
//
//So far I think that this notifier should be a pointer to an object of type
//OpalListenerUDP but i have no idea how to get it done
}
Any help will be appreciated, Red.

After deeper research into opal source code, I've found the solution.
It turns out that
OpalConnection::StartListener(OpalListener&);
Will do the job of opening listener, so there is no need of calling OpalListener::Open() so after it is only needed to call OpalListener::Accept to accept a connection.

Related

Server RPC is not called by client for some reason

So I have just started working on a new project in Unreal which is intended to be a simple networked multiplayer game. What I have done so far is make a Weapon class that handles the spawning of projectiles, and give the player character an instance of a weapon on BeginPlay.
The weapon class is simple, but the problem that I am having is that the Server RPC I have for spawning projectiles is not being called from clients, only servers.
Here is how I am calling this RPC:
The Player Character has an OnFire method, that is bound to an input action.
This then calls the Fire method on the current weapon that the player has.
// Bind fire event
PlayerInputComponent->BindAction("Fire", IE_Pressed, this, &AMultiplayerFPSCharacter::OnFire);
...
void AMultiplayerFPSCharacter::OnFire()
{
// Call our weapon's fire method
if (CurrentWeapon)
{
CurrentWeapon->Fire(GetControlRotation());
}
}
Now, the weapon's Fire method simply calls the Server RPC to actually handle spawning the projectiles.
void AWeapon::Fire(const FRotator SpawnRotation)
{
UE_LOG(LogTemp, Warning, TEXT("LOCAL Fire"));
Server_Fire(SpawnRotation);
}
void AWeapon::Server_Fire_Implementation(const FRotator SpawnRotation)
{
UE_LOG(LogTemp, Warning, TEXT("SERVER RPC: Called RPC"));
// try and fire a projectile
if (ProjectileClass != NULL)
{
// Spawn Projectile
....
}
}
I also have the validation method for the Server RPC, which simply returns true for this so I can make sure it actually works.
This implementation is all fine and good on the server or Listen-Server, but when I call it from the client I only get the local Fire method on the Weapon. The client never even calls the server RPC.
One work around that I think I have found is to make the Player Character's OnFire method a Server RPC as well, but this feels like it is not the best way to go about doing this.
Could anyone explain why this is happening?
A client can only call a server RPC on a net-owned actor. For example, a client net-owns his PlayerController, so you can add a RPC to that, and call it. If you call it on a server-owned object, like I'm assuming your AWeapon actor is, the log will show something like "can't call RPC on non-owned object".

Can i use QTcpSocket again for another connection after deleteLater?

I try to use QTcpSserver, which would keep connection with one and only one client at a time, until the client disconnects. So, I keep the client with a member pointer in my class.
The problem arises here: In the examples I see on the internet, after disconnected(), it is called deleteLater(). Good, but I would use this class-member pointer again for another connection. Remember that the server keeps one and only one client at a time. So, what if the socket object is deleted after another connection assigned on it?
What I mean is:
class TcpServer(QObject* o) : public QTcpServer {
...
private:
QTcpSocket* client;
}
void TcpServer::connected() {
client = this->nextPendingConnection();
this->pauseAccepting();
connect(client, SIGNAL(disconnected()), client, SLOT(clientDisconnected()));
}
void TcpServer::clientDisconnected() {
client->deleteLater();
this->resumeAccepting();
}
Scenario is this:
Client connected. So, client = nextPendingConnection();
Server paused listening. Does not accept new connection.
Client is disconnected. client needs to be released. So, client->deleteLater() is calleed.
Server continues listening.
New connection comes. So, I need to client = nextPendingConnection();
But, previous client object was deleted? Maybe? Maybe not? What if event loop tries to delete client, after I have assigned the new connection to it in step 5?
So, how would I keep one and only one client, while deleting previous disconnected ones?
Would it be safe if I do this?
void TcpServer::clientDisconnected()
{
QSocket* ptr = client;
ptr->deleteLater();
...
}
I will cite Qt documentation about it:
The object will be deleted when control returns to the event loop.
So deleteLater() is a delayed delete. The object is to be regarded as deleted as soon as the call deleteLater() was made.
Your nextPendingConnection() call will create another object that need to be deleted some time later.
However in your case you only allow one pending connection as you said and disallow accepting until client gets disconnected. I this case it should be safe, in other cases you could overwrite your client pointer and will lose control over it (memory leak).
Even in your case, I would prefer this solution:
void TcpServer::clientDisconnected()
{
if (qobject_cast<QAbstractSocket*>(sender())) {
sender()->deleteLater();
}
...
}
This would also be safe if more than one connection is allowed in future changes of your application.
As i understand nextPendingConnection(); will return pointer to new QTcpSocket class object so you have nothing to worry about.
deleteLater() will scheduled for deletion only your old object. QTcpSocket* client contains only pointer to QTcpSocket class object. When you calling deleteLater() Qt will delete only object to which client was pointed at time of calling this function.

Blackberry MediaPlayer retrieves empty metaData

I have a cascades project where I use the MediaPlayer class in cpp.
I have defined a handler class, which handles metaDataChanged event, but when I set the source url and call mediaPlayer.prepare() method, it doesn't retrieve anything in metadata, so it's simply empty QVariantMap.
What's interesting is that defined event handler for metaDataChaned event is not even called.
I think there could be something that I can add here to be able to get the metadata, however prepare() method workds sucessfully, so I don't know what's the problem
here is a piece of code I've tried.
bb::multimedia::MediaPlayer* mp = new bb::multimedia::MediaPlayer();
mp->setSourceUrl(resultString);
mp->prepare();
MetaDataReader metaDataReader(mp);
and a class
MetaDataReader::MetaDataReader(bb::multimedia::MediaPlayer* mediaPlayer) : QObject(NULL)
{
connect(mediaPlayer, SIGNAL(metaDataChanged(const QVariantMap&)), this, SLOT(onMetaDataChanged(const QVariantMap&)));
}
void MetaDataReader::onMetaDataChanged(const QVariantMap& metaData)
{
someCode
// It doesn't reach this SLOT
}
How can I get the metadata here?
thanks in advance
It's a bit odd, but you may not get metadata until you start playback for the file. Try starting playback, and you should see the metaDataChanged signal get fired shortly after.

Connecting signals/slots on separate thread using QtConcurrent::run

In my application I have the following code in a dialog:
connect(drive, SIGNAL(FileProgressChanged(Progress)), SLOT(OnFileProgressChanged(Progress)));
QtConcurrent::run(this, &ProgressDialog::PerformOperation, Operation, *Path, OutPath, drive);
The PerformOperation function eventually calls to a function in drive which emits the signal FileProgressChanged, and my OnFileProgressChanged function is as follows:
void ProgressDialog::OnFileProgressChanged(Progress p)
{
if (ui->progressCurrent->maximum() != p.Maximium)
ui->progressCurrent->setMaximum(p.Maximium);
ui->progressCurrent->setValue(p.Current);
if (ui->groupBoxCurrent->title().toStdString() != p.FilePath)
ui->groupBoxCurrent->setTitle(QString::fromStdString(p.FilePath));
}
I was doing some reading and saw that QFuture and QFutureWatcher support monitoring progress values (which would work great in this situation!), but those cannot be used in conjunction with QtConcurrent::run.
How would I go about connecting the signal that gets moved emitted on the separate thread to the slot on my main thread so I can monitor the progress of the function called on the emitter thread?
*Edit -- * I actually found an error with my code, but it doesn't seem to have an affect. I forgot to add this as an argument after the signal
connect(drive, SIGNAL(FileProgressChanged(Progress)), this, SLOT(OnFileProgressChanged(Progress)));
Try using connect() with QueuedConnection, like:
connect(drive, SIGNAL(FileProgressChanged(Progress)), this, SLOT(OnFileProgressChanged(Progress)), Qt::QueuedConnection);
The connection should already be queued by default (since the emitter and receiver are in different threads), but this just makes it more explicit.
EDIT: The problem was that the Progress type wasn't registered with Qt's meta-object system. Adding qRegisterMetaType<Progress>("Progress"); fixed the problem.
It appears as though the problem isn't with the cross-thread signal/slot, but instead with the parameter Progress. This question's answer goes into further detail, but the solution was found by doing the following in the header file in which Progress was declared:
struct Progress
{
int Current;
int Maximium;
std::string FilePath;
std::string FolderPath;
int TotalMinimum;
int TotalMaximum;
};
Q_DECLARE_METATYPE(Progress)
And in my form class:
qRegisterMetaType<Progress>();
connect(Drive, SIGNAL(FileProgressChanged(const Progress&)), this, SLOT(OnFileProgressChanged(const Progress&)), Qt::QueuedConnection);
Changing Progress to const Progress& most likely isn't needed but I left it while testing.

Exception Handling in Qt Script with C++?

I have the following action which is executed when a certain
button is pressed in a Qt application:
#include <shape.h>
void computeOperations()
{
polynomial_t p1("x^2-x*y+1"),p2("x^2+2*y-1");
BoundingBox bx(-4.01, 4.01,-6.01,6.01,-6.01,6.01);
Topology3d g(bx);
AlgebraicCurve* cv= new AlgebraicCurve(p1,p2);
g.push_back(cv);
g.run();
//Other operations on g.
}
Topology3d(...), AlgebraicCurve(..), BoundingBox(...),
polynomial_t(...) are user defined types defined in the
corresponding header file .
Now for some values of p1 and p2, the method g.run() works perfectly.
Thus for some other values of p1 and p2, g.run() it is not
working anymore as the method gets blocked somehow and the
message "Application Not Responding" appears and I have to
kill the Application.
I would want to have the following behavior: whenever
g.run() is taking too long, gets blocked for some particular
values of p1, p2, I would want to display an warning box
using QMessageBox::Warning.
I try to do this with try{...} and catch{...}:
#include <shape.h>
class topologyException : public std::runtime_error
{
public:
topologyException::topologyException(): std::runtime_error( "topology fails" ) {}
};
void computeOperations()
{
try
{
polynomial_t p1("x^2-x*y+1"),p2("x^2+2*y-1");
BoundingBox bx(-4.01, 4.01,-6.01,6.01,-6.01,6.01);
Topology3d g(bx);
AlgebraicCurve* cv= new AlgebraicCurve(p1,p2);
g.push_back(cv);
g.run();
//other operations on g
throw topologyException();
}
catch(topologyException& topException)
{
QMessageBox errorBox;
errorBox.setIcon(QMessageBox::Warning);
errorBox.setText("The parameters are incorrect.");
errorBox.setInformativeText("Please insert another polynomial.");
errorBox.exec();
}
}
This code compiles, but when it runs it does not really
implement the required behavior.
For the polynomials for which g.run() gets blocked the error
message box code is never reached, plus for the polynomials
for which g.run() is working well, the error message box
code still is reached somehow and the box appears in the
application.
I am new to handling exceptions, so any help is more than
welcomed.
I think the program gets blocked somewhere inside g.run() so
it does not reach the exception, still I do not understand
what really happens.
Still I would want to throw this exception without going
into the code of g.run(), this function is implemented as
part of a bigger library, which I just use in my code.
Can I have this behavior in my program without putting any
try{...} catch{...} block statement in the g.run() function?
You cannot achieve what you want with the use of try-catch. if g.run() takes too much time or goes into an infinite loop, that doesn't mean an exception will be thrown.
What you can do is, you can move the operations that take a lot of time into another thread. Start that thread in your event handler and wait for it to finish in your main thread for a fixed amount of time. If it does not finish, kill that thread & show your messagebox.
For further reference, read QThread, Qt Thread Support
Thanks for the suggestions.
So I see how I should create the thread, something like:
class myopThread : public QThread
{
public:
void run();
};
Then I am rewriting the run() function and put all the operations that take a lot of time in it:
void myopThread::run()
{
polynomial_t p1("x^2-x*y+1"),p2("x^2+2*y-1");
BoundingBox bx(-4.01, 4.01,-6.01,6.01,-6.01,6.01);
Topology3d g(bx);
AlgebraicCurve* cv= new AlgebraicCurve(p1,p2);
g.push_back(cv);
g.run();
//other operations on g
exec();
}
Okay everything is clear so far, still I do not see how to "Start that thread in your event handler and wait for it to finish in your main thread for a fixed amount of time. If it does not finish, kill that thread & show your messagebox."
I mean start the thread in the event handler refers somehow at using the connect (..Signal, Slot..) still I do not see how exactly this is done. I have never used QThread before so it is more then new.
Thank you very much for your help,
madalina
The most elegant way to solve this that I know of is with a future value. If you haven't run across these before they can be quite handy in situations like this. Say you have a value that you'll need later on, but you can begin calculating concurrently. The code might look something like this:
SomeValue getValue() {
... calculate the value ...
}
void foo() {
Future<SomeValue> future_value(getValue);
... other code that takes a long time ...
SomeValue v = future_value.get();
}
Upon calling the .get() method of course, the value computed is returned, either by calling the function then and there or by retrieving the cache value calculated in another thread started when the Future<T> was created. One nice thing is that, at least for a few libraries, you can pass in a timeout parameter into the .get() method. This way if your value is taking too long to compute you can always unblock. Such elegant isn't usually achieved.
For a real life library, you might try looking into the library documented here. As I recall it wasn't accepted as the official boost futures library, but it certainly had promise. Good luck!