Commit c70f01d5 authored by Stephane Letz's avatar Stephane Letz
Browse files

New restart callback in net API.

parent 0c8bb599
......@@ -84,6 +84,7 @@ extern "C"
typedef int (*JackNetSlaveBufferSizeCallback) (jack_nframes_t nframes, void *arg);
typedef int (*JackNetSlaveSampleRateCallback) (jack_nframes_t nframes, void *arg);
typedef void (*JackNetSlaveShutdownCallback) (void* data);
typedef int (*JackNetSlaveRestartCallback) (void* data);
LIB_EXPORT jack_net_slave_t* jack_net_slave_open(const char* ip, int port, const char* name, jack_slave_t* request, jack_master_t* result);
LIB_EXPORT int jack_net_slave_close(jack_net_slave_t* net);
......@@ -95,6 +96,7 @@ extern "C"
LIB_EXPORT int jack_set_net_slave_buffer_size_callback(jack_net_slave_t* net, JackNetSlaveBufferSizeCallback bufsize_callback, void *arg);
LIB_EXPORT int jack_set_net_slave_sample_rate_callback(jack_net_slave_t* net, JackNetSlaveSampleRateCallback samplerate_callback, void *arg);
LIB_EXPORT int jack_set_net_slave_shutdown_callback(jack_net_slave_t* net, JackNetSlaveShutdownCallback shutdown_callback, void *arg);
LIB_EXPORT int jack_set_net_slave_restart_callback(jack_net_slave_t* net, JackNetSlaveRestartCallback restart_callback, void *arg);
// NetJack master API
......@@ -492,6 +494,9 @@ struct JackNetExtSlave : public JackNetSlaveInterface, public JackRunnableInterf
JackNetSlaveShutdownCallback fShutdownCallback;
void* fShutdownArg;
JackNetSlaveRestartCallback fRestartCallback;
void* fRestartArg;
JackNetSlaveBufferSizeCallback fBufferSizeCallback;
void* fBufferSizeArg;
......@@ -515,6 +520,7 @@ struct JackNetExtSlave : public JackNetSlaveInterface, public JackRunnableInterf
:fThread(this),
fProcessCallback(NULL),fProcessArg(NULL),
fShutdownCallback(NULL), fShutdownArg(NULL),
fRestartCallback(NULL), fRestartArg(NULL),
fBufferSizeCallback(NULL), fBufferSizeArg(NULL),
fSampleRateCallback(NULL), fSampleRateArg(NULL),
fAudioCaptureBuffer(NULL), fAudioPlaybackBuffer(NULL),
......@@ -602,17 +608,27 @@ struct JackNetExtSlave : public JackNetSlaveInterface, public JackRunnableInterf
return 0;
}
int Restart()
int Restart()
{
// If shutdown cb is set, then call it
if (fShutdownCallback) {
fShutdownCallback(fShutdownArg);
}
// Do it until client possibly decides to stop trying to connect...
while (true) {
// If restart cb is set, then call it
if (fRestartCallback) {
if (fRestartCallback(fRestartArg) != 0) {
return -1;
}
// Otherwise if shutdown cb is set, then call it
} else if (fShutdownCallback) {
fShutdownCallback(fShutdownArg);
}
// Init network connection
if (!JackNetSlaveInterface::InitConnection(fConnectTimeOut)) {
jack_error("Initing network fails...");
return -1;
// Init network connection
if (!JackNetSlaveInterface::InitConnection(fConnectTimeOut)) {
jack_error("Initing network fails after time_out, retry...");
} else {
break;
}
}
// Finish connection
......@@ -629,11 +645,17 @@ struct JackNetExtSlave : public JackNetSlaveInterface, public JackRunnableInterf
// We need to notify possibly new buffer size and sample rate (see Execute)
if (fBufferSizeCallback) {
fBufferSizeCallback(fParams.fPeriodSize, fBufferSizeArg);
if (fBufferSizeCallback(fParams.fPeriodSize, fBufferSizeArg) != 0) {
jack_error("New buffer size = %d cannot be used...", fParams.fPeriodSize);
return -1;
}
}
if (fSampleRateCallback) {
fSampleRateCallback(fParams.fSampleRate, fSampleRateArg);
if (fSampleRateCallback(fParams.fSampleRate, fSampleRateArg) != 0) {
jack_error("New sample rate = %d cannot be used...", fParams.fSampleRate);
return -1;
}
}
AllocPorts();
......@@ -844,6 +866,17 @@ struct JackNetExtSlave : public JackNetSlaveInterface, public JackRunnableInterf
return 0;
}
}
int SetRestartCallback(JackNetSlaveRestartCallback restart_callback, void *arg)
{
if (fThread.GetStatus() == JackThread::kRunning) {
return -1;
} else {
fRestartCallback = restart_callback;
fRestartArg = arg;
return 0;
}
}
int SetBufferSizeCallback(JackNetSlaveBufferSizeCallback bufsize_callback, void *arg)
{
......@@ -998,6 +1031,12 @@ LIB_EXPORT int jack_set_net_slave_shutdown_callback(jack_net_slave_t *net, JackN
return slave->SetShutdownCallback(shutdown_callback, arg);
}
LIB_EXPORT int jack_set_net_slave_restart_callback(jack_net_slave_t *net, JackNetSlaveRestartCallback restart_callback, void *arg)
{
JackNetExtSlave* slave = (JackNetExtSlave*)net;
return slave->SetRestartCallback(restart_callback, arg);
}
// Master API
LIB_EXPORT jack_net_master_t* jack_net_master_open(const char* ip, int port, const char* name, jack_master_t* request, jack_slave_t* result)
......
......@@ -784,7 +784,7 @@ Deactivated for now..
strncpy(net_name, param->value.str, JACK_CLIENT_NAME_SIZE);
break;
case 's':
auto_save = param->value.i;
auto_save = true;
break;
/*
Deactivated for now..
......
......@@ -644,13 +644,13 @@ namespace Jack
{
jack_log("JackNetSlaveInterface::InitConnection time_out_sec = %d", time_out_sec);
int try_count = (time_out_sec > 0) ? ((1000000 * time_out_sec) / SLAVE_INIT_TIMEOUT) : INT_MAX;
// set the parameters to send
strcpy(fParams.fPacketType, "params");
fParams.fProtocolVersion = NETWORK_PROTOCOL;
SetPacketType(&fParams, SLAVE_AVAILABLE);
return (SendAvailableToMaster(try_count) != NET_SOCKET_ERROR);
return (SendAvailableToMaster(try_count) == NET_CONNECTED);
}
bool JackNetSlaveInterface::InitRendering()
......@@ -706,6 +706,7 @@ namespace Jack
// send 'AVAILABLE' until 'SLAVE_SETUP' received
jack_info("Waiting for a master...");
do {
// send 'available'
session_params_t net_params;
......@@ -725,7 +726,7 @@ namespace Jack
}
}
while (strcmp(host_params.fPacketType, fParams.fPacketType) && (GetPacketType(&host_params) != SLAVE_SETUP) && (--try_count > 0));
// time out failure..
if (try_count == 0) {
jack_error("Time out error in connect");
......@@ -740,6 +741,7 @@ namespace Jack
jack_error("Error in connect : %s", StrError(NET_ERROR_CODE));
return NET_CONNECT_ERROR;
}
return NET_CONNECTED;
}
......
......@@ -676,11 +676,11 @@ namespace Jack
break;
case 'c':
fAutoConnect = param->value.i;
fAutoConnect = true;
break;
case 's':
fAutoSave = param->value.i;
fAutoSave = true;
break;
}
}
......
......@@ -27,6 +27,7 @@ extern "C"
#include <jack/systemdeps.h>
#include <jack/types.h>
#include <jack/weakmacros.h>
#define DEFAULT_MULTICAST_IP "225.3.19.154"
#define DEFAULT_PORT 19000
......@@ -35,6 +36,8 @@ extern "C"
#define SOCKET_ERROR -1
#define RESTART_CB_API 1
enum JackNetEncoder {
JackFloatEncoder = 0, // samples are transmitted as float
......@@ -198,7 +201,28 @@ typedef void (*JackNetSlaveShutdownCallback)(void* data);
*
* @return 0 on success, otherwise a non-zero error code
*/
int jack_set_net_slave_shutdown_callback(jack_net_slave_t *net, JackNetSlaveShutdownCallback shutdown_callback, void *arg);
int jack_set_net_slave_shutdown_callback(jack_net_slave_t *net, JackNetSlaveShutdownCallback shutdown_callback, void *arg) JACK_OPTIONAL_WEAK_DEPRECATED_EXPORT;
/**
* Prototype for server Restart callback : this is the new preferable way to be notified when the master has disappeared.
* The client may want to retry connecting a certain number of time (which will be done using the time_out value given in jack_net_slave_open)
* by returning 0. Otherwise returning a non-zero error code will definively close the connection.
* If both Shutdown and Restart are supplied, Restart callback will be used.
* @param arg pointer to a client supplied structure supplied by jack_set_net_restart_callback()
*
* @return 0 on success, otherwise a non-zero error code
*/
typedef int (*JackNetSlaveRestartCallback)(void* data);
/**
* Set network restart callback.
* @param net the network connection
* @param restart_callback the shutdown callback
* @param arg pointer to a client supplied structure
*
* @return 0 on success, otherwise a non-zero error code
*/
int jack_set_net_slave_restart_callback(jack_net_slave_t *net, JackNetSlaveRestartCallback restart_callback, void *arg) JACK_OPTIONAL_WEAK_EXPORT;
/**
* jack_net_master_t is an opaque type, you may only access it using the API provided.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment