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_CLIENT_H__
21 #define __AGS_OSC_CLIENT_H__
22 
23 #include <glib.h>
24 #include <glib-object.h>
25 
26 #include <gio/gio.h>
27 
28 G_BEGIN_DECLS
29 
30 #define AGS_TYPE_OSC_CLIENT                (ags_osc_client_get_type ())
31 #define AGS_OSC_CLIENT(obj)                (G_TYPE_CHECK_INSTANCE_CAST((obj), AGS_TYPE_OSC_CLIENT, AgsOscClient))
32 #define AGS_OSC_CLIENT_CLASS(class)        (G_TYPE_CHECK_CLASS_CAST((class), AGS_TYPE_OSC_CLIENT, AgsOscClientClass))
33 #define AGS_IS_OSC_CLIENT(obj)             (G_TYPE_CHECK_INSTANCE_TYPE ((obj), AGS_TYPE_OSC_CLIENT))
34 #define AGS_IS_OSC_CLIENT_CLASS(class)     (G_TYPE_CHECK_CLASS_TYPE ((class), AGS_TYPE_OSC_CLIENT))
35 #define AGS_OSC_CLIENT_GET_CLASS(obj)      (G_TYPE_INSTANCE_GET_CLASS ((obj), AGS_TYPE_OSC_CLIENT, AgsOscClientClass))
36 
37 #define AGS_OSC_CLIENT_GET_OBJ_MUTEX(obj) (&(((AgsOscClient *) obj)->obj_mutex))
38 
39 #define AGS_OSC_CLIENT_DEFAULT_MAX_ADDRESS_LENGTH (2048)
40 
41 #define AGS_OSC_CLIENT_DEFAULT_SERVER_PORT (9000)
42 #define AGS_OSC_CLIENT_DEFAULT_DOMAIN "localhost"
43 #define AGS_OSC_CLIENT_DEFAULT_INET4_ADDRESS "127.0.0.1"
44 #define AGS_OSC_CLIENT_DEFAULT_INET6_ADDRESS "::1"
45 
46 #define AGS_OSC_CLIENT_DEFAULT_MAX_RETRY (16)
47 
48 #define AGS_OSC_CLIENT_CHUNK_SIZE (131072)
49 
50 #define AGS_OSC_CLIENT_DEFAULT_CACHE_DATA_LENGTH (256)
51 
52 typedef struct _AgsOscClient AgsOscClient;
53 typedef struct _AgsOscClientClass AgsOscClientClass;
54 
55 /**
56  * AgsOscClientFlags:
57  * @AGS_OSC_CLIENT_INET4: use IPv4 socket
58  * @AGS_OSC_CLIENT_INET6: use IPv6 socket
59  * @AGS_OSC_CLIENT_UDP: use UDP transport protocol
60  * @AGS_OSC_CLIENT_TCP: used TCP transport protocol
61  *
62  * Enum values to configure OSC client.
63  */
64 typedef enum{
65   AGS_OSC_CLIENT_INET4      = 1,
66   AGS_OSC_CLIENT_INET6      = 1 <<  1,
67   AGS_OSC_CLIENT_UDP        = 1 <<  2,
68   AGS_OSC_CLIENT_TCP        = 1 <<  3,
69 }AgsOscClientFlags;
70 
71 struct _AgsOscClient
72 {
73   GObject gobject;
74 
75   guint flags;
76 
77   GRecMutex obj_mutex;
78 
79   gchar *ip4;
80   gchar *ip6;
81 
82   gchar *domain;
83   guint server_port;
84 
85   int ip4_fd;
86   int ip6_fd;
87 
88   GSocket *ip4_socket;
89   GSocket *ip6_socket;
90 
91   GSocketAddress *ip4_address;
92   GSocketAddress *ip6_address;
93 
94   guint max_retry_count;
95 
96   struct timespec *retry_delay;
97 
98   struct timespec *start_time;
99 
100   unsigned char *cache_data;
101   guint cache_data_length;
102 
103   unsigned char *buffer;
104   guint allocated_buffer_size;
105 
106   guint read_count;
107   gboolean has_valid_data;
108 
109   struct timespec *timeout_delay;
110 };
111 
112 struct _AgsOscClientClass
113 {
114   GObjectClass gobject;
115 
116   void (*resolve)(AgsOscClient *osc_client);
117   void (*connect)(AgsOscClient *osc_client);
118 
119   unsigned char* (*read_bytes)(AgsOscClient *osc_client,
120 			       guint *data_length);
121   gboolean (*write_bytes)(AgsOscClient *osc_client,
122 			  guchar *data, guint data_length);
123 };
124 
125 GType ags_osc_client_get_type(void);
126 
127 gboolean ags_osc_client_test_flags(AgsOscClient *osc_client, guint flags);
128 void ags_osc_client_set_flags(AgsOscClient *osc_client, guint flags);
129 void ags_osc_client_unset_flags(AgsOscClient *osc_client, guint flags);
130 
131 void ags_osc_client_resolve(AgsOscClient *osc_client);
132 void ags_osc_client_connect(AgsOscClient *osc_client);
133 
134 unsigned char* ags_osc_client_read_bytes(AgsOscClient *osc_client,
135 					 guint *data_length);
136 
137 gboolean ags_osc_client_write_bytes(AgsOscClient *osc_client,
138 				    guchar *data, guint data_length);
139 
140 AgsOscClient* ags_osc_client_new();
141 
142 G_END_DECLS
143 
144 #endif /*__AGS_OSC_CLIENT_H__*/
145