d870c79ef090d782d88addb3fc5babb5595488a3
[ardour.git] / libs / midi++2 / midi++ / ipmidi_port.h
1 /*
2     Copyright (C) 1998-2010 Paul Barton-Davis
3     This program is free software; you can redistribute it and/or modify
4     it under the terms of the GNU General Public License as published by
5     the Free Software Foundation; either version 2 of the License, or
6     (at your option) any later version.
7
8     This program is distributed in the hope that it will be useful,
9     but WITHOUT ANY WARRANTY; without even the implied warranty of
10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11     GNU General Public License for more details.
12
13     You should have received a copy of the GNU General Public License
14     along with this program; if not, write to the Free Software
15     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16
17 */
18
19 #ifndef  __libmidi_ipmidi_port_h__
20 #define  __libmidi_ipmidi_port_h__
21
22 #include <string>
23 #include <iostream>
24 #if defined(PLATFORM_WINDOWS)
25 #include <winsock.h>
26 #elif defined(__FreeBSD__)
27 #include <netinet/in.h>
28 #include <sys/socket.h>
29 #include <sys/_sockaddr_storage.h>
30 #include <arpa/inet.h>
31 #include <netinet/ip_carp.h>
32 #include <sys/types.h>
33 #include <sys/time.h>
34 #include <sys/socket.h>
35 #include <net/if.h>
36 #include <net/route.h>
37 #else
38 #include <arpa/inet.h>
39 #include <net/if.h>
40 #endif
41
42 #include <glibmm/threads.h>
43
44 #include "pbd/xml++.h"
45 #include "pbd/crossthread.h"
46 #include "pbd/signals.h"
47 #include "pbd/ringbuffer.h"
48
49 #include "midi++/libmidi_visibility.h"
50 #include "midi++/types.h"
51 #include "midi++/parser.h"
52 #include "midi++/port.h"
53
54 namespace MIDI {
55
56 class LIBMIDIPP_API IPMIDIPort : public Port {
57   public:
58     IPMIDIPort (int base_port = lowest_ipmidi_port_default, const std::string& ifname = std::string());
59     IPMIDIPort (const XMLNode&);
60     ~IPMIDIPort ();
61
62     XMLNode& get_state () const;
63     void set_state (const XMLNode&);
64
65     int write (const byte *msg, size_t msglen, timestamp_t timestamp);
66     int read (byte *buf, size_t bufsize);
67     void parse (framecnt_t timestamp);
68     int selectable () const;
69
70     static const int lowest_ipmidi_port_default = 21928;
71
72 private:
73     int sockin;
74     int sockout;
75     struct sockaddr_in addrout;
76     Glib::Threads::Mutex write_lock;
77
78     bool open_sockets (int base_port, const std::string& ifname);
79     void close_sockets ();
80
81     void init (std::string const &, Flags);
82 };
83
84 } // namespace MIDI
85
86 #endif // __libmidi_ipmidi_port_h__