JackNetInterface.h 6.04 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
Copyright (C) 2001 Paul Davis
Copyright (C) 2008 Romain Moret at Grame

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#ifndef __JackNetInterface__
#define __JackNetInterface__

#include "JackNetTool.h"

namespace Jack
{
    /**
    \Brief This class describes the basic Net Interface, used by both master and slave
    */

32
    class SERVER_EXPORT JackNetInterface
33
    {
moret's avatar
moret committed
34
35
36
        protected:
            session_params_t fParams;
            JackNetSocket fSocket;
37
            char fMulticastIP[32];
moret's avatar
moret committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
            uint fNSubProcess;

            //headers
            packet_header_t fTxHeader;
            packet_header_t fRxHeader;

            //network buffers
            char* fTxBuffer;
            char* fRxBuffer;
            char* fTxData;
            char* fRxData;

            //jack buffers
            NetMidiBuffer* fNetMidiCaptureBuffer;
            NetMidiBuffer* fNetMidiPlaybackBuffer;
            NetAudioBuffer* fNetAudioCaptureBuffer;
            NetAudioBuffer* fNetAudioPlaybackBuffer;

            //sizes
            int fAudioRxLen;
            int fAudioTxLen;
            int fPayloadSize;

moret's avatar
moret committed
61
            //utility methods
62
            void SetFramesPerPacket();
moret's avatar
moret committed
63
64
65
            int SetNetBufferSize();
            int GetNMidiPckt();
            bool IsNextPacket();
moret's avatar
moret committed
66
67

            //virtual methods : depends on the sub class master/slave
moret's avatar
moret committed
68
            virtual void SetParams();
moret's avatar
moret committed
69
70
            virtual bool Init() = 0;

moret's avatar
moret committed
71
72
73
74
75
76
77
78
            //transport
            virtual int EncodeTransportData() = 0;
            virtual int DecodeTransportData() = 0;

            //sync packet
            virtual int EncodeSyncPacket() = 0;
            virtual int DecodeSyncPacket() = 0;

moret's avatar
moret committed
79
80
81
82
83
84
85
86
            virtual int SyncRecv() = 0;
            virtual int SyncSend() = 0;
            virtual int DataRecv() = 0;
            virtual int DataSend() = 0;

            virtual int Send ( size_t size, int flags ) = 0;
            virtual int Recv ( size_t size, int flags ) = 0;

87
            JackNetInterface();
moret's avatar
moret committed
88
            JackNetInterface ( const char* multicast_ip, int port );
moret's avatar
moret committed
89
            JackNetInterface ( session_params_t& params, JackNetSocket& socket, const char* multicast_ip );
moret's avatar
moret committed
90
91
92

        public:
            virtual ~JackNetInterface();
93
94
95
    };

    /**
moret's avatar
moret committed
96
    \Brief This class describes the Net Interface for masters (NetMaster)
97
98
    */

99
    class SERVER_EXPORT JackNetMasterInterface : public JackNetInterface
100
    {
moret's avatar
moret committed
101
        protected:
moret's avatar
moret committed
102
            bool fRunning;
103
            int fCycleOffset;
moret's avatar
moret committed
104

moret's avatar
moret committed
105
            bool Init();
moret's avatar
moret committed
106
            int SetRxTimeout();
moret's avatar
moret committed
107
            void SetParams();
moret's avatar
moret committed
108
            void Exit();
moret's avatar
moret committed
109
110
111
112
113
114
            int SyncRecv();
            int SyncSend();
            int DataRecv();
            int DataSend();

            int Send ( size_t size, int flags );
moret's avatar
moret committed
115
            int Recv ( size_t size, int flags );
116
117
            
            bool IsSynched();
moret's avatar
moret committed
118
119

        public:
120
            JackNetMasterInterface() : JackNetInterface(), fRunning(false), fCycleOffset(0)
moret's avatar
moret committed
121
            {}
moret's avatar
moret committed
122
123
124
125
            JackNetMasterInterface ( session_params_t& params, JackNetSocket& socket, const char* multicast_ip )
                    : JackNetInterface ( params, socket, multicast_ip )
            {}
            ~JackNetMasterInterface()
moret's avatar
moret committed
126
            {}
127
128
129
    };

    /**
moret's avatar
moret committed
130
    \Brief This class describes the Net Interface for slaves (NetDriver and NetAdapter)
131
132
    */

133
    class SERVER_EXPORT JackNetSlaveInterface : public JackNetInterface
134
    {
moret's avatar
moret committed
135
        protected:
136
        
moret's avatar
moret committed
137
            static uint fSlaveCounter;
138
139
140
            
            net_transport_data_t fSendTransportData;
            net_transport_data_t fReturnTransportData;
moret's avatar
moret committed
141

moret's avatar
moret committed
142
            bool Init();
143
144
            bool InitConnection();
            bool InitRendering();
145
            
moret's avatar
moret committed
146
            net_status_t GetNetMaster();
moret's avatar
moret committed
147
            net_status_t SendStartToMaster();
148
            
moret's avatar
moret committed
149
            void SetParams();
150
            
moret's avatar
moret committed
151
152
            int SyncRecv();
            int SyncSend();
153
            
moret's avatar
moret committed
154
155
            int DataRecv();
            int DataSend();
156
157
158
159
            
            //sync packet
            int EncodeSyncPacket();
            int DecodeSyncPacket();
moret's avatar
moret committed
160
161

            int Recv ( size_t size, int flags );
moret's avatar
moret committed
162
            int Send ( size_t size, int flags );
moret's avatar
moret committed
163
164

        public:
moret's avatar
moret committed
165
            JackNetSlaveInterface() : JackNetInterface()
moret's avatar
moret committed
166
            {
167
                //open Socket API with the first slave
168
169
170
171
                if ( fSlaveCounter++ == 0 )
                {
                    if ( SocketAPIInit() < 0 )
                    {
172
173
174
175
                        jack_error ( "Can't init Socket API, exiting..." );
                        throw -1;
                    }
                }
moret's avatar
moret committed
176
            }
moret's avatar
moret committed
177
            JackNetSlaveInterface ( const char* ip, int port ) : JackNetInterface ( ip, port )
moret's avatar
moret committed
178
            {
179
                //open Socket API with the first slave
180
181
182
183
                if ( fSlaveCounter++ == 0 )
                {
                    if ( SocketAPIInit() < 0 )
                    {
184
185
186
187
                        jack_error ( "Can't init Socket API, exiting..." );
                        throw -1;
                    }
                }
moret's avatar
moret committed
188
            }
moret's avatar
moret committed
189
190
            ~JackNetSlaveInterface()
            {
moret's avatar
moret committed
191
192
193
                //close Socket API with the last slave
                if ( --fSlaveCounter == 0 )
                    SocketAPIEnd();
moret's avatar
moret committed
194
            }
195
196
197
    };
}

198
199
#define DEFAULT_MULTICAST_IP "225.3.19.154"
#define DEFAULT_PORT 19000
200
#define DEFAULT_MTU 1500
201

202
#define SLAVE_SETUP_RETRY 5
203

204
205
206
#define MASTER_INIT_TIMEOUT 1000000     // in usec
#define SLAVE_INIT_TIMEOUT 2000000      // in usec

207
#define MAX_LATENCY 6
208

209
#endif