Better isolation of server and clients system resources to allow starting the server...
[jack2.git] / common / JackNetInterface.cpp
bloba9862b4c57c88b18580af6bb910866015a4f6c0f
1 /*
2 Copyright (C) 2001 Paul Davis
3 Copyright (C) 2008 Romain Moret at Grame
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "JackNetInterface.h"
21 #include "JackException.h"
22 #include "JackPlatformPlug.h"
23 #include <assert.h>
25 using namespace std;
27 namespace Jack
29 // JackNetInterface*******************************************
31 JackNetInterface::JackNetInterface() : fSocket()
33 fMulticastIP = NULL;
34 fTxBuffer = NULL;
35 fRxBuffer = NULL;
36 fNetAudioCaptureBuffer = NULL;
37 fNetAudioPlaybackBuffer = NULL;
38 fNetMidiCaptureBuffer = NULL;
39 fNetMidiPlaybackBuffer = NULL;
42 JackNetInterface::JackNetInterface ( const char* multicast_ip, int port ) : fSocket ( multicast_ip, port )
44 fMulticastIP = strdup ( multicast_ip );
45 fTxBuffer = NULL;
46 fRxBuffer = NULL;
47 fNetAudioCaptureBuffer = NULL;
48 fNetAudioPlaybackBuffer = NULL;
49 fNetMidiCaptureBuffer = NULL;
50 fNetMidiPlaybackBuffer = NULL;
53 JackNetInterface::JackNetInterface ( session_params_t& params, JackNetSocket& socket, const char* multicast_ip ) : fSocket ( socket )
55 fParams = params;
56 fMulticastIP = strdup ( multicast_ip );
57 fTxBuffer = NULL;
58 fRxBuffer = NULL;
59 fNetAudioCaptureBuffer = NULL;
60 fNetAudioPlaybackBuffer = NULL;
61 fNetMidiCaptureBuffer = NULL;
62 fNetMidiPlaybackBuffer = NULL;
65 JackNetInterface::~JackNetInterface()
67 jack_log ( "JackNetInterface::~JackNetInterface" );
69 fSocket.Close();
70 delete[] fTxBuffer;
71 delete[] fRxBuffer;
72 delete[] fMulticastIP;
73 delete fNetAudioCaptureBuffer;
74 delete fNetAudioPlaybackBuffer;
75 delete fNetMidiCaptureBuffer;
76 delete fNetMidiPlaybackBuffer;
79 jack_nframes_t JackNetInterface::SetFramesPerPacket()
81 jack_log ( "JackNetInterface::SetFramesPerPacket" );
83 if ( !fParams.fSendAudioChannels && !fParams.fReturnAudioChannels )
84 return ( fParams.fFramesPerPacket = fParams.fPeriodSize );
85 jack_nframes_t period = ( int ) powf ( 2.f, ( int ) ( log ( ( fParams.fMtu - sizeof ( packet_header_t ) )
86 / ( max ( fParams.fReturnAudioChannels, fParams.fSendAudioChannels ) * sizeof ( sample_t ) ) ) / log ( 2 ) ) );
87 return ( fParams.fFramesPerPacket = ( period > fParams.fPeriodSize ) ? fParams.fPeriodSize : period );
90 int JackNetInterface::SetNetBufferSize()
92 jack_log ( "JackNetInterface::SetNetBufferSize" );
94 float audio_size, midi_size;
95 int bufsize, res = 0;
96 //audio
97 audio_size = fParams.fMtu * ( fParams.fPeriodSize / fParams.fFramesPerPacket );
98 //midi
99 midi_size = fParams.fMtu * ( max ( fParams.fSendMidiChannels, fParams.fReturnMidiChannels ) *
100 fParams.fPeriodSize * sizeof ( sample_t ) / ( fParams.fMtu - sizeof ( packet_header_t ) ) );
101 //bufsize = sync + audio + midi
102 bufsize = fParams.fMtu + ( int ) audio_size + ( int ) midi_size;
104 //tx buffer
105 if ( fSocket.SetOption ( SOL_SOCKET, SO_SNDBUF, &bufsize, sizeof ( bufsize ) ) == SOCKET_ERROR )
106 res = SOCKET_ERROR;
108 //rx buffer
109 if ( fSocket.SetOption ( SOL_SOCKET, SO_RCVBUF, &bufsize, sizeof ( bufsize ) ) == SOCKET_ERROR )
110 res = SOCKET_ERROR;
112 return res;
115 int JackNetInterface::GetNMidiPckt()
117 //even if there is no midi data, jack need an empty buffer to know there is no event to read
118 //99% of the cases : all data in one packet
119 if ( fTxHeader.fMidiDataSize <= ( fParams.fMtu - sizeof ( packet_header_t ) ) )
120 return 1;
121 //else, get the number of needed packets (simply slice the biiig buffer)
122 int npckt = fTxHeader.fMidiDataSize / ( fParams.fMtu - sizeof ( packet_header_t ) );
123 if ( fTxHeader.fMidiDataSize % ( fParams.fMtu - sizeof ( packet_header_t ) ) )
124 return ++npckt;
125 return npckt;
128 bool JackNetInterface::IsNextPacket()
130 packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
131 //ignore first cycle
132 if ( fRxHeader.fCycle <= 1 )
133 return true;
134 //same PcktID (cycle), next SubPcktID (subcycle)
135 if ( ( fRxHeader.fSubCycle < ( fNSubProcess - 1 ) ) && ( rx_head->fCycle == fRxHeader.fCycle ) && ( rx_head->fSubCycle == ( fRxHeader.fSubCycle + 1 ) ) )
136 return true;
137 //next PcktID (cycle), SubPcktID reset to 0 (first subcyle)
138 if ( ( rx_head->fCycle == ( fRxHeader.fCycle + 1 ) ) && ( fRxHeader.fSubCycle == ( fNSubProcess - 1 ) ) && ( rx_head->fSubCycle == 0 ) )
139 return true;
140 //else, packet(s) missing, return false
141 return false;
144 void JackNetInterface::SetParams()
146 //number of audio subcycles (packets)
147 fNSubProcess = fParams.fPeriodSize / fParams.fFramesPerPacket;
149 //payload size
150 fPayloadSize = fParams.fMtu - sizeof ( packet_header_t );
152 //TX header init
153 strcpy ( fTxHeader.fPacketType, "header" );
154 fTxHeader.fID = fParams.fID;
155 fTxHeader.fCycle = 0;
156 fTxHeader.fSubCycle = 0;
157 fTxHeader.fMidiDataSize = 0;
158 fTxHeader.fBitdepth = fParams.fBitdepth;
159 fTxHeader.fIsLastPckt = 0;
161 //RX header init
162 strcpy ( fRxHeader.fPacketType, "header" );
163 fRxHeader.fID = fParams.fID;
164 fRxHeader.fCycle = 0;
165 fRxHeader.fSubCycle = 0;
166 fRxHeader.fMidiDataSize = 0;
167 fRxHeader.fBitdepth = fParams.fBitdepth;
168 fRxHeader.fIsLastPckt = 0;
170 //network buffers
171 fTxBuffer = new char[fParams.fMtu];
172 fRxBuffer = new char[fParams.fMtu];
173 assert ( fTxBuffer );
174 assert ( fRxBuffer );
176 //net audio/midi buffers'addresses
177 fTxData = fTxBuffer + sizeof ( packet_header_t );
178 fRxData = fRxBuffer + sizeof ( packet_header_t );
181 // JackNetMasterInterface ************************************************************************************
183 bool JackNetMasterInterface::Init()
185 jack_log ( "JackNetMasterInterface::Init, ID %u.", fParams.fID );
187 session_params_t params;
188 uint attempt = 0;
189 int rx_bytes = 0;
191 //socket
192 if ( fSocket.NewSocket() == SOCKET_ERROR )
194 jack_error ( "Can't create socket : %s", StrError ( NET_ERROR_CODE ) );
195 return false;
198 //timeout on receive (for init)
199 if ( fSocket.SetTimeOut ( MASTER_INIT_TIMEOUT ) < 0 )
200 jack_error ( "Can't set timeout : %s", StrError ( NET_ERROR_CODE ) );
202 //connect
203 if ( fSocket.Connect() == SOCKET_ERROR )
205 jack_error ( "Can't connect : %s", StrError ( NET_ERROR_CODE ) );
206 return false;
209 //set the number of complete audio frames we can put in a packet
210 SetFramesPerPacket();
212 //send 'SLAVE_SETUP' until 'START_MASTER' received
213 jack_info ( "Sending parameters to %s ...", fParams.fSlaveNetName );
216 SetPacketType ( &fParams, SLAVE_SETUP );
217 if ( fSocket.Send ( &fParams, sizeof ( session_params_t ), 0 ) == SOCKET_ERROR )
218 jack_error ( "Error in send : ", StrError ( NET_ERROR_CODE ) );
219 if ( ( ( rx_bytes = fSocket.Recv ( &params, sizeof ( session_params_t ), 0 ) ) == SOCKET_ERROR ) && ( fSocket.GetError() != NET_NO_DATA ) )
221 jack_error ( "Problem with network." );
222 return false;
225 while ( ( GetPacketType ( &params ) != START_MASTER ) && ( ++attempt < SLAVE_SETUP_RETRY ) );
226 if ( attempt == SLAVE_SETUP_RETRY )
228 jack_error ( "Slave doesn't respond, exiting." );
229 return false;
232 //set the new timeout for the socket
233 if ( SetRxTimeout() == SOCKET_ERROR )
235 jack_error ( "Can't set rx timeout : %s", StrError ( NET_ERROR_CODE ) );
236 return false;
239 //set the new rx buffer size
240 if ( SetNetBufferSize() == SOCKET_ERROR )
242 jack_error ( "Can't set net buffer sizes : %s", StrError ( NET_ERROR_CODE ) );
243 return false;
246 return true;
249 int JackNetMasterInterface::SetRxTimeout()
251 jack_log ( "JackNetMasterInterface::SetRxTimeout" );
253 float time = 0;
254 //slow or normal mode, short timeout on recv (2 audio subcycles)
255 if ( ( fParams.fNetworkMode == 's' ) || ( fParams.fNetworkMode == 'n' ) )
256 time = 2000000.f * ( static_cast<float> ( fParams.fFramesPerPacket ) / static_cast<float> ( fParams.fSampleRate ) );
257 //fast mode, wait for 75% of the entire cycle duration
258 else if ( fParams.fNetworkMode == 'f' )
259 time = 750000.f * ( static_cast<float> ( fParams.fPeriodSize ) / static_cast<float> ( fParams.fSampleRate ) );
260 return fSocket.SetTimeOut ( static_cast<int> ( time ) );
263 void JackNetMasterInterface::SetParams()
265 jack_log ( "JackNetMasterInterface::SetParams" );
267 JackNetInterface::SetParams();
269 fTxHeader.fDataStream = 's';
270 fRxHeader.fDataStream = 'r';
272 //midi net buffers
273 fNetMidiCaptureBuffer = new NetMidiBuffer ( &fParams, fParams.fSendMidiChannels, fTxData );
274 fNetMidiPlaybackBuffer = new NetMidiBuffer ( &fParams, fParams.fReturnMidiChannels, fRxData );
275 assert ( fNetMidiCaptureBuffer );
276 assert ( fNetMidiPlaybackBuffer );
278 //audio net buffers
279 fNetAudioCaptureBuffer = new NetAudioBuffer ( &fParams, fParams.fSendAudioChannels, fTxData );
280 fNetAudioPlaybackBuffer = new NetAudioBuffer ( &fParams, fParams.fReturnAudioChannels, fRxData );
281 assert ( fNetAudioCaptureBuffer );
282 assert ( fNetAudioPlaybackBuffer );
284 //audio netbuffer length
285 fAudioTxLen = sizeof ( packet_header_t ) + fNetAudioPlaybackBuffer->GetSize();
286 fAudioRxLen = sizeof ( packet_header_t ) + fNetAudioCaptureBuffer->GetSize();
289 void JackNetMasterInterface::Exit()
291 jack_log ( "JackNetMasterInterface::Exit, ID %u", fParams.fID );
293 //stop process
294 fRunning = false;
296 //send a 'multicast euthanasia request' - new socket is required on macosx
297 jack_info ( "Exiting '%s'", fParams.fName );
298 SetPacketType ( &fParams, KILL_MASTER );
299 JackNetSocket mcast_socket ( fMulticastIP, fSocket.GetPort() );
300 if ( mcast_socket.NewSocket() == SOCKET_ERROR )
301 jack_error ( "Can't create socket : %s", StrError ( NET_ERROR_CODE ) );
302 if ( mcast_socket.SendTo ( &fParams, sizeof ( session_params_t ), 0, fMulticastIP ) == SOCKET_ERROR )
303 jack_error ( "Can't send suicide request : %s", StrError ( NET_ERROR_CODE ) );
304 mcast_socket.Close();
306 // UGLY temporary way to be sure the thread does not call code possibly causing a deadlock in JackEngine.
307 ThreadExit();
310 int JackNetMasterInterface::Send ( size_t size, int flags )
312 int tx_bytes;
313 if ( ( ( tx_bytes = fSocket.Send ( fTxBuffer, size, flags ) ) == SOCKET_ERROR ) && fRunning )
315 net_error_t error = fSocket.GetError();
316 if ( error == NET_CONN_ERROR )
318 //fatal connection issue, exit
319 jack_error ( "'%s' : %s, exiting.", fParams.fName, StrError ( NET_ERROR_CODE ) );
320 Exit();
322 else
323 jack_error ( "Error in send : %s", StrError ( NET_ERROR_CODE ) );
325 return tx_bytes;
328 int JackNetMasterInterface::Recv ( size_t size, int flags )
330 int rx_bytes;
331 if ( ( ( rx_bytes = fSocket.Recv ( fRxBuffer, size, flags ) ) == SOCKET_ERROR ) && fRunning )
333 net_error_t error = fSocket.GetError();
334 //no data isn't really a network error, so just return 0 avalaible read bytes
335 if ( error == NET_NO_DATA )
336 return 0;
337 else if ( error == NET_CONN_ERROR )
339 //fatal connection issue, exit
340 jack_error ( "'%s' : %s, exiting.", fParams.fName, StrError ( NET_ERROR_CODE ) );
341 //ask to the manager to properly remove the master
342 Exit();
344 else
345 jack_error ( "Error in receive : %s", StrError ( NET_ERROR_CODE ) );
347 return rx_bytes;
350 int JackNetMasterInterface::SyncSend()
352 fTxHeader.fCycle++;
353 fTxHeader.fSubCycle = 0;
354 fTxHeader.fDataType = 's';
355 fTxHeader.fIsLastPckt = ( !fParams.fSendMidiChannels && !fParams.fSendAudioChannels ) ? 1 : 0;
356 fTxHeader.fPacketSize = fParams.fMtu;
357 memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
358 return Send ( fTxHeader.fPacketSize, 0 );
361 int JackNetMasterInterface::DataSend()
363 uint subproc;
364 //midi
365 if ( fParams.fSendMidiChannels )
367 //set global header fields and get the number of midi packets
368 fTxHeader.fDataType = 'm';
369 fTxHeader.fMidiDataSize = fNetMidiCaptureBuffer->RenderFromJackPorts();
370 fTxHeader.fNMidiPckt = GetNMidiPckt();
371 for ( subproc = 0; subproc < fTxHeader.fNMidiPckt; subproc++ )
373 fTxHeader.fSubCycle = subproc;
374 fTxHeader.fIsLastPckt = ( ( subproc == ( fTxHeader.fNMidiPckt - 1 ) ) && !fParams.fSendAudioChannels ) ? 1 : 0;
375 fTxHeader.fPacketSize = sizeof ( packet_header_t );
376 fTxHeader.fPacketSize += fNetMidiCaptureBuffer->RenderToNetwork ( subproc, fTxHeader.fMidiDataSize );
377 memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
378 if ( Send ( fTxHeader.fPacketSize, 0 ) == SOCKET_ERROR )
379 return SOCKET_ERROR;
383 //audio
384 if ( fParams.fSendAudioChannels )
386 fTxHeader.fDataType = 'a';
387 for ( subproc = 0; subproc < fNSubProcess; subproc++ )
389 fTxHeader.fSubCycle = subproc;
390 fTxHeader.fIsLastPckt = ( subproc == ( fNSubProcess - 1 ) ) ? 1 : 0;
391 fTxHeader.fPacketSize = fAudioTxLen;
392 memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
393 fNetAudioCaptureBuffer->RenderFromJackPorts ( subproc );
394 if ( Send ( fTxHeader.fPacketSize, 0 ) == SOCKET_ERROR )
395 return SOCKET_ERROR;
399 return 0;
402 int JackNetMasterInterface::SyncRecv()
404 int rx_bytes = 0;
405 int cycle_offset = 0;
406 packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
407 rx_bytes = Recv ( fParams.fMtu, MSG_PEEK );
408 if ( ( rx_bytes == 0 ) || ( rx_bytes == SOCKET_ERROR ) )
409 return rx_bytes;
411 cycle_offset = fTxHeader.fCycle - rx_head->fCycle;
413 switch ( fParams.fNetworkMode )
415 case 's' :
416 //slow mode : allow to use full bandwidth and heavy process on the slave
417 // - extra latency is set to two cycles, one cycle for send/receive operations + one cycle for heavy process on the slave
418 // - if the network is two fast, just wait the next cycle, this mode allows a shorter cycle duration for the master
419 // - this mode will skip the two first cycles, thus it lets time for data to be processed and queued on the socket rx buffer
420 //the slow mode is the safest mode because it wait twice the bandwidth relative time (send/return + process)
421 if ( cycle_offset < 2 )
422 return 0;
423 else
424 rx_bytes = Recv ( rx_head->fPacketSize, 0 );
425 break;
427 case 'n' :
428 //normal use of the network :
429 // - extra latency is set to one cycle, what is the time needed to receive streams using full network bandwidth
430 // - if the network is too fast, just wait the next cycle, the benefit here is the master's cycle is shorter
431 // - indeed, data is supposed to be on the network rx buffer, so we don't have to wait for it
432 if ( cycle_offset < 1 )
433 return 0;
434 else
435 rx_bytes = Recv ( rx_head->fPacketSize, 0 );
436 break;
438 case 'f' :
439 //fast mode suppose the network bandwith is larger than required for the transmission (only a few channels for example)
440 // - packets can be quickly received, quickly is here relative to the cycle duration
441 // - here, receive data, we can't keep it queued on the rx buffer,
442 // - but if there is a cycle offset, tell the user, that means we're not in fast mode anymore, network is too slow
443 rx_bytes = Recv ( rx_head->fPacketSize, 0 );
444 if ( cycle_offset )
445 jack_error ( "'%s' can't run in fast network mode, data received too late (%d cycle(s) offset)", fParams.fName, cycle_offset );
446 break;
449 fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
450 return rx_bytes;
453 int JackNetMasterInterface::DataRecv()
455 int rx_bytes = 0;
456 uint jumpcnt = 0;
457 uint midi_recvd_pckt = 0;
458 packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
460 while ( !fRxHeader.fIsLastPckt )
462 //how much data is queued on the rx buffer ?
463 rx_bytes = Recv ( fParams.fMtu, MSG_PEEK );
464 if ( rx_bytes == SOCKET_ERROR )
465 return rx_bytes;
466 //if no data
467 if ( ( rx_bytes == 0 ) && ( ++jumpcnt == fNSubProcess ) )
469 jack_error ( "No data from %s...", fParams.fName );
470 jumpcnt = 0;
472 //else if data is valid,
473 if ( rx_bytes && ( rx_head->fDataStream == 'r' ) && ( rx_head->fID == fParams.fID ) )
475 //read data
476 switch ( rx_head->fDataType )
478 case 'm': //midi
479 Recv ( rx_head->fPacketSize, 0 );
480 fRxHeader.fCycle = rx_head->fCycle;
481 fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
482 fNetMidiPlaybackBuffer->RenderFromNetwork ( rx_head->fSubCycle, rx_bytes - sizeof ( packet_header_t ) );
483 if ( ++midi_recvd_pckt == rx_head->fNMidiPckt )
484 fNetMidiPlaybackBuffer->RenderToJackPorts();
485 jumpcnt = 0;
486 break;
488 case 'a': //audio
489 Recv ( rx_head->fPacketSize, 0 );
490 if ( !IsNextPacket() )
491 jack_error ( "Packet(s) missing from '%s'...", fParams.fName );
492 fRxHeader.fCycle = rx_head->fCycle;
493 fRxHeader.fSubCycle = rx_head->fSubCycle;
494 fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
495 fNetAudioPlaybackBuffer->RenderToJackPorts ( rx_head->fSubCycle );
496 jumpcnt = 0;
497 break;
499 case 's': //sync
500 if ( rx_head->fCycle == fTxHeader.fCycle )
501 return 0;
502 break;
506 return rx_bytes;
509 // JackNetSlaveInterface ************************************************************************************************
511 uint JackNetSlaveInterface::fSlaveCounter = 0;
513 bool JackNetSlaveInterface::Init()
515 jack_log ( "JackNetSlaveInterface::Init()" );
517 //set the parameters to send
518 strcpy ( fParams.fPacketType, "params" );
519 fParams.fProtocolVersion = 'a';
520 SetPacketType ( &fParams, SLAVE_AVAILABLE );
522 //init loop : get a master and start, do it until connection is ok
523 net_status_t status;
526 //first, get a master, do it until a valid connection is running
529 status = GetNetMaster();
530 if ( status == NET_SOCKET_ERROR )
531 return false;
533 while ( status != NET_CONNECTED );
535 //then tell the master we are ready
536 jack_info ( "Initializing connection with %s...", fParams.fMasterNetName );
537 status = SendStartToMaster();
538 if ( status == NET_ERROR )
539 return false;
541 while ( status != NET_ROLLING );
543 return true;
546 net_status_t JackNetSlaveInterface::GetNetMaster()
548 jack_log ( "JackNetSlaveInterface::GetNetMaster()" );
549 //utility
550 session_params_t params;
551 int rx_bytes = 0;
553 //socket
554 if ( fSocket.NewSocket() == SOCKET_ERROR )
556 jack_error ( "Fatal error : network unreachable - %s", StrError ( NET_ERROR_CODE ) );
557 return NET_SOCKET_ERROR;
560 //bind the socket
561 if ( fSocket.Bind() == SOCKET_ERROR )
562 jack_error ( "Can't bind the socket : %s", StrError ( NET_ERROR_CODE ) );
564 //timeout on receive
565 if ( fSocket.SetTimeOut ( SLAVE_INIT_TIMEOUT ) == SOCKET_ERROR )
566 jack_error ( "Can't set timeout : %s", StrError ( NET_ERROR_CODE ) );
568 //disable local loop
569 if ( fSocket.SetLocalLoop() == SOCKET_ERROR )
570 jack_error ( "Can't disable multicast loop : %s", StrError ( NET_ERROR_CODE ) );
572 //send 'AVAILABLE' until 'SLAVE_SETUP' received
573 jack_info ( "Waiting for a master..." );
576 //send 'available'
577 if ( fSocket.SendTo ( &fParams, sizeof ( session_params_t ), 0, fMulticastIP ) == SOCKET_ERROR )
578 jack_error ( "Error in data send : %s", StrError ( NET_ERROR_CODE ) );
579 //filter incoming packets : don't exit while no error is detected
580 rx_bytes = fSocket.CatchHost ( &params, sizeof ( session_params_t ), 0 );
581 if ( ( rx_bytes == SOCKET_ERROR ) && ( fSocket.GetError() != NET_NO_DATA ) )
583 jack_error ( "Can't receive : %s", StrError ( NET_ERROR_CODE ) );
584 return NET_RECV_ERROR;
587 while ( strcmp ( params.fPacketType, fParams.fPacketType ) && ( GetPacketType ( &params ) != SLAVE_SETUP ) );
589 //everything is OK, copy parameters
590 fParams = params;
592 //set the new buffer sizes
593 if ( SetNetBufferSize() == SOCKET_ERROR )
594 jack_error ( "Can't set net buffer sizes : %s", StrError ( NET_ERROR_CODE ) );
596 //connect the socket
597 if ( fSocket.Connect() == SOCKET_ERROR )
599 jack_error ( "Error in connect : %s", StrError ( NET_ERROR_CODE ) );
600 return NET_CONNECT_ERROR;
603 return NET_CONNECTED;
606 net_status_t JackNetSlaveInterface::SendStartToMaster()
608 jack_log ( "JackNetSlaveInterface::SendStartToMaster" );
610 //tell the master to start
611 SetPacketType ( &fParams, START_MASTER );
612 if ( fSocket.Send ( &fParams, sizeof ( session_params_t ), 0 ) == SOCKET_ERROR )
614 jack_error ( "Error in send : %s", StrError ( NET_ERROR_CODE ) );
615 return ( fSocket.GetError() == NET_CONN_ERROR ) ? NET_ERROR : NET_SEND_ERROR;
617 return NET_ROLLING;
620 void JackNetSlaveInterface::SetParams()
622 jack_log ( "JackNetSlaveInterface::SetParams" );
624 JackNetInterface::SetParams();
626 fTxHeader.fDataStream = 'r';
627 fRxHeader.fDataStream = 's';
629 //midi net buffers
630 fNetMidiCaptureBuffer = new NetMidiBuffer ( &fParams, fParams.fSendMidiChannels, fRxData );
631 fNetMidiPlaybackBuffer = new NetMidiBuffer ( &fParams, fParams.fReturnMidiChannels, fTxData );
633 //audio net buffers
634 fNetAudioCaptureBuffer = new NetAudioBuffer ( &fParams, fParams.fSendAudioChannels, fRxData );
635 fNetAudioPlaybackBuffer = new NetAudioBuffer ( &fParams, fParams.fReturnAudioChannels, fTxData );
637 //audio netbuffer length
638 fAudioTxLen = sizeof ( packet_header_t ) + fNetAudioPlaybackBuffer->GetSize();
639 fAudioRxLen = sizeof ( packet_header_t ) + fNetAudioCaptureBuffer->GetSize();
642 int JackNetSlaveInterface::Recv ( size_t size, int flags )
644 int rx_bytes = fSocket.Recv ( fRxBuffer, size, flags );
645 //handle errors
646 if ( rx_bytes == SOCKET_ERROR )
648 net_error_t error = fSocket.GetError();
649 //no data isn't really an error in realtime processing, so just return 0
650 if ( error == NET_NO_DATA )
651 jack_error ( "No data, is the master still running ?" );
652 //if a network error occurs, this exception will restart the driver
653 else if ( error == NET_CONN_ERROR )
655 jack_error ( "Connection lost." );
656 throw JackNetException();
658 else
659 jack_error ( "Fatal error in receive : %s", StrError ( NET_ERROR_CODE ) );
661 return rx_bytes;
664 int JackNetSlaveInterface::Send ( size_t size, int flags )
666 int tx_bytes = fSocket.Send ( fTxBuffer, size, flags );
667 //handle errors
668 if ( tx_bytes == SOCKET_ERROR )
670 net_error_t error = fSocket.GetError();
671 //if a network error occurs, this exception will restart the driver
672 if ( error == NET_CONN_ERROR )
674 jack_error ( "Connection lost." );
675 throw JackNetException();
677 else
678 jack_error ( "Fatal error in send : %s", StrError ( NET_ERROR_CODE ) );
680 return tx_bytes;
683 int JackNetSlaveInterface::SyncRecv()
685 int rx_bytes = 0;
686 packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
687 //receive sync (launch the cycle)
690 rx_bytes = Recv ( fParams.fMtu, 0 );
691 //connection issue, send will detect it, so don't skip the cycle (return 0)
692 if ( rx_bytes == SOCKET_ERROR )
693 return rx_bytes;
695 while ( !rx_bytes && ( rx_head->fDataType != 's' ) );
696 fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
697 return rx_bytes;
700 int JackNetSlaveInterface::DataRecv()
702 uint recvd_midi_pckt = 0;
703 int rx_bytes = 0;
704 packet_header_t* rx_head = reinterpret_cast<packet_header_t*> ( fRxBuffer );
706 while ( !fRxHeader.fIsLastPckt )
708 rx_bytes = Recv ( fParams.fMtu, MSG_PEEK );
709 //error here, problem with recv, just skip the cycle (return -1)
711 if ( rx_bytes == SOCKET_ERROR )
712 return rx_bytes;
713 if ( rx_bytes && ( rx_head->fDataStream == 's' ) && ( rx_head->fID == fParams.fID ) )
715 switch ( rx_head->fDataType )
717 case 'm': //midi
718 rx_bytes = Recv ( rx_head->fPacketSize, 0 );
719 fRxHeader.fCycle = rx_head->fCycle;
720 fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
721 fNetMidiCaptureBuffer->RenderFromNetwork ( rx_head->fSubCycle, rx_bytes - sizeof ( packet_header_t ) );
722 if ( ++recvd_midi_pckt == rx_head->fNMidiPckt )
723 fNetMidiCaptureBuffer->RenderToJackPorts();
724 break;
726 case 'a': //audio
727 rx_bytes = Recv ( rx_head->fPacketSize, 0 );
728 if ( !IsNextPacket() )
729 jack_error ( "Packet(s) missing..." );
730 fRxHeader.fCycle = rx_head->fCycle;
731 fRxHeader.fSubCycle = rx_head->fSubCycle;
732 fRxHeader.fIsLastPckt = rx_head->fIsLastPckt;
733 fNetAudioCaptureBuffer->RenderToJackPorts ( rx_head->fSubCycle );
734 break;
736 case 's': //sync
737 jack_info ( "NetSlave : overloaded, skipping receive." );
738 return 0;
742 fRxHeader.fCycle = rx_head->fCycle;
743 return 0;
746 int JackNetSlaveInterface::SyncSend()
748 //tx header
749 if ( fParams.fSlaveSyncMode )
750 fTxHeader.fCycle = fRxHeader.fCycle;
751 else
752 fTxHeader.fCycle++;
753 fTxHeader.fSubCycle = 0;
754 fTxHeader.fDataType = 's';
755 fTxHeader.fIsLastPckt = ( !fParams.fReturnMidiChannels && !fParams.fReturnAudioChannels ) ? 1 : 0;
756 fTxHeader.fPacketSize = fParams.fMtu;
757 memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
758 return Send ( fTxHeader.fPacketSize, 0 );
761 int JackNetSlaveInterface::DataSend()
763 uint subproc;
765 //midi
766 if ( fParams.fReturnMidiChannels )
768 fTxHeader.fDataType = 'm';
769 fTxHeader.fMidiDataSize = fNetMidiPlaybackBuffer->RenderFromJackPorts();
770 fTxHeader.fNMidiPckt = GetNMidiPckt();
771 for ( subproc = 0; subproc < fTxHeader.fNMidiPckt; subproc++ )
773 fTxHeader.fSubCycle = subproc;
774 fTxHeader.fIsLastPckt = ( ( subproc == ( fTxHeader.fNMidiPckt - 1 ) ) && !fParams.fReturnAudioChannels ) ? 1 : 0;
775 fTxHeader.fPacketSize = sizeof ( packet_header_t );
776 fTxHeader.fPacketSize += fNetMidiPlaybackBuffer->RenderToNetwork ( subproc, fTxHeader.fMidiDataSize );
777 memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
778 if ( Send ( fTxHeader.fPacketSize, 0 ) == SOCKET_ERROR )
779 return SOCKET_ERROR;
783 //audio
784 if ( fParams.fReturnAudioChannels )
786 fTxHeader.fDataType = 'a';
787 for ( subproc = 0; subproc < fNSubProcess; subproc++ )
789 fTxHeader.fSubCycle = subproc;
790 fTxHeader.fIsLastPckt = ( subproc == ( fNSubProcess - 1 ) ) ? 1 : 0;
791 fTxHeader.fPacketSize = fAudioTxLen;
792 memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) );
793 fNetAudioPlaybackBuffer->RenderFromJackPorts ( subproc );
794 if ( Send ( fTxHeader.fPacketSize, 0 ) == SOCKET_ERROR )
795 return SOCKET_ERROR;
798 return 0;