1 /* GSequencer - Advanced GTK Sequencer
2  * Copyright (C) 2005-2020 Joël Krähemann
3  *
4  * This file is part of GSequencer.
5  *
6  * GSequencer is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GSequencer is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GSequencer.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef __AGS_OSC_CONNECTION_H__
21 #define __AGS_OSC_CONNECTION_H__
22 
23 #include <glib.h>
24 #include <glib-object.h>
25 
26 #include <gio/gio.h>
27 
28 #include <ags/libags.h>
29 
30 G_BEGIN_DECLS
31 
32 #define AGS_TYPE_OSC_CONNECTION                (ags_osc_connection_get_type ())
33 #define AGS_OSC_CONNECTION(obj)                (G_TYPE_CHECK_INSTANCE_CAST((obj), AGS_TYPE_OSC_CONNECTION, AgsOscConnection))
34 #define AGS_OSC_CONNECTION_CLASS(class)        (G_TYPE_CHECK_CLASS_CAST((class), AGS_TYPE_OSC_CONNECTION, AgsOscConnectionClass))
35 #define AGS_IS_OSC_CONNECTION(obj)             (G_TYPE_CHECK_INSTANCE_TYPE ((obj), AGS_TYPE_OSC_CONNECTION))
36 #define AGS_IS_OSC_CONNECTION_CLASS(class)     (G_TYPE_CHECK_CLASS_TYPE ((class), AGS_TYPE_OSC_CONNECTION))
37 #define AGS_OSC_CONNECTION_GET_CLASS(obj)      (G_TYPE_INSTANCE_GET_CLASS ((obj), AGS_TYPE_OSC_CONNECTION, AgsOscConnectionClass))
38 
39 #define AGS_OSC_CONNECTION_GET_OBJ_MUTEX(obj) (&(((AgsOscConnection *) obj)->obj_mutex))
40 
41 #define AGS_OSC_CONNECTION_TIMEOUT_USEC (250)
42 #define AGS_OSC_CONNECTION_DEAD_LINE_USEC (60000000)
43 #define AGS_OSC_CONNECTION_CHUNK_SIZE (131072)
44 
45 #define AGS_OSC_CONNECTION_DEFAULT_CACHE_DATA_LENGTH (256)
46 
47 typedef struct _AgsOscConnection AgsOscConnection;
48 typedef struct _AgsOscConnectionClass AgsOscConnectionClass;
49 
50 /**
51  * AgsOscConnectionFlags:
52  * @AGS_OSC_CONNECTION_ACTIVE: is active
53  * @AGS_OSC_CONNECTION_INET4: IPv4 connection
54  * @AGS_OSC_CONNECTION_INET6: IPv6 connection
55  *
56  * Enum values to configure OSC connection.
57  */
58 typedef enum{
59   AGS_OSC_CONNECTION_ACTIVE     = 1,
60   AGS_OSC_CONNECTION_INET4      = 1 <<  1,
61   AGS_OSC_CONNECTION_INET6      = 1 <<  2,
62 }AgsOscConnectionFlags;
63 
64 struct _AgsOscConnection
65 {
66   GObject gobject;
67 
68   guint flags;
69 
70   GRecMutex obj_mutex;
71 
72   GObject *osc_server;
73 
74   gchar *ip4;
75   gchar *ip6;
76 
77   int fd;
78 
79   GSocket *socket;
80 
81   struct timespec *start_time;
82 
83   guchar *cache_data;
84 
85   guint cache_data_length;
86 
87   guchar *buffer;
88   guint allocated_buffer_size;
89 
90   guint read_count;
91   gboolean has_valid_data;
92 
93   struct timespec *timeout_delay;
94   struct timespec *timestamp;
95 };
96 
97 struct _AgsOscConnectionClass
98 {
99   GObjectClass gobject;
100 
101   guchar* (*read_bytes)(AgsOscConnection *osc_connection,
102 			guint *data_length);
103   gint64 (*write_response)(AgsOscConnection *osc_connection,
104 			   GObject *osc_response);
105 
106   void (*close)(AgsOscConnection *osc_connection);
107 };
108 
109 GType ags_osc_connection_get_type(void);
110 
111 gboolean ags_osc_connection_test_flags(AgsOscConnection *osc_connection, guint flags);
112 void ags_osc_connection_set_flags(AgsOscConnection *osc_connection, guint flags);
113 void ags_osc_connection_unset_flags(AgsOscConnection *osc_connection, guint flags);
114 
115 gboolean ags_osc_connection_timeout_expired(struct timespec *start_time,
116 					    struct timespec *timeout_delay);
117 
118 /* events */
119 guchar* ags_osc_connection_read_bytes(AgsOscConnection *osc_connection,
120 					     guint *data_length);
121 
122 gint64 ags_osc_connection_write_response(AgsOscConnection *osc_connection,
123 					 GObject *osc_response);
124 
125 void ags_osc_connection_close(AgsOscConnection *osc_connection);
126 
127 /* instance */
128 AgsOscConnection* ags_osc_connection_new(GObject *osc_server);
129 
130 G_END_DECLS
131 
132 #endif /*__AGS_OSC_CONNECTION_H__*/
133