forked from coolaj86/libev-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unix-socket-ipc-remote-control.c
228 lines (183 loc) · 4.98 KB
/
unix-socket-ipc-remote-control.c
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Arg Parser
#include <argp.h>
// Standard Equipment
#include <stdlib.h>
#include <stdio.h>
// libev
#include <ev.h>
// Unix Sockets
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/fcntl.h> // fcntl
#include <unistd.h> // close
#include <errno.h>
// Application Specific
#include "my-data.h"
/* IPC Data */
// NOTE: unless you have a serialize function, you
// may not use a struct with pointers to send across
// a socket. The reference will be meaningless on
// the other side.
struct my_data data;
/***********************************
*
* Remote Control Argument Parser
*
*/
const char* argp_program_version = "Newdora Remote v0.1 (Alpha Alligator)";
const char* argp_program_bug_address = "<newdora@example.tld>";
/* Program documentation. */
static char doc[] = "Newdora Remote -- Remote Control (RC) for Newdora Serving Server Services Streamer (N4S)";
/* The options we understand. */
static struct argp_option options[] = {
{"mode", 'm', "string", 0, "turbo, fast, slow, or realtime"},
{"file", 'f', "fullpath", 0, "the N4S encoded file to render"},
{ 0 }
};
static error_t parse_opt(int key, char* arg, struct argp_state* state)
{
/* Get the input argument from argp_parse, which we
know is a pointer to our arguments structure. */
struct my_data* data = state->input;
switch (key)
{
case 'm':
if (0 == strcmp("fast", arg))
{
data->mode = NRC_FAST;
}
else if (0 == strcmp("slow", arg))
{
data->mode = NRC_SLOW;
}
else if (0 == strcmp("turbo", arg))
{
data->mode = NRC_TURBO;
}
else if (0 == strcmp("realtime", arg))
{
data->mode = NRC_REALTIME;
}
else
{
return EINVAL;
}
break;
case 'f':
if (strlen(arg) > 255) {
return E2BIG;
}
strcpy(data->file, arg);
break;
case ARGP_KEY_ARG:
return ARGP_ERR_UNKNOWN;
break;
case ARGP_KEY_END:
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp argp = { options, parse_opt, 0, doc };
/***********************************
*
* libev connection client
*
*/
// Nasty globals for now
// feel free to fork this example and clean it up
EV_P; // Macro for `struct ev_loop* loop`
ev_io daemon_w;
ev_io send_w;
int daemon_fd;
static void send_cb (EV_P_ ev_io *w, int revents)
{
int s_sent;
if (revents & EV_WRITE)
{
puts ("daemon ready for writing...");
s_sent = send(daemon_fd, &data, sizeof data, 0);
if (-1 == s_sent)
{
perror("echo send");
exit(EXIT_FAILURE);
}
else if (sizeof data != s_sent)
{
printf("what the heck? mismatch size? crazy!\n");
}
// once the data is sent, stop notifications that
// data can be sent until there is actually more
// data to send
ev_io_stop(EV_A_ &send_w);
ev_unloop(EV_A_ EVUNLOOP_ALL);
//ev_io_set(&send_w, daemon_fd, EV_READ);
//ev_io_start(EV_A_ &send_w);
}
else if (revents & EV_READ)
{
// TODO ACK / NACK
}
}
static void daemon_cb (EV_P_ ev_io *w, int revents)
{
puts ("connected, now watching stdin");
// Once the connection is established, listen for writability
ev_io_start(EV_A_ &send_w);
// Once we're connected, that's the end of that
ev_io_stop(EV_A_ &daemon_w);
}
// Simply adds O_NONBLOCK to the file descriptor of choice
int setnonblock(int fd)
{
int flags;
flags = fcntl(fd, F_GETFL);
flags |= O_NONBLOCK;
return fcntl(fd, F_SETFL, flags);
}
static void connection_new(EV_P_ char* sock_path) {
int len;
struct sockaddr_un daemon;
if (-1 == (daemon_fd = socket(AF_UNIX, SOCK_STREAM, 0))) {
perror("socket");
exit(1);
}
// Set it non-blocking
if (-1 == setnonblock(daemon_fd)) {
perror("echo client socket nonblock");
exit(EXIT_FAILURE);
}
// this should be initialized before the connect() so
// that no packets are dropped when initially sent?
// http://cr.yp.to/docs/connect.html
// initialize the connect callback so that it starts the stdin asap
ev_io_init (&daemon_w, daemon_cb, daemon_fd, EV_WRITE);
ev_io_start(EV_A_ &daemon_w);
// initialize the send callback, but wait to start until there is data to write
ev_io_init(&send_w, send_cb, daemon_fd, EV_WRITE);
ev_io_start(EV_A_ &send_w);
daemon.sun_family = AF_UNIX;
strcpy(daemon.sun_path, sock_path);
len = strlen(daemon.sun_path) + sizeof(daemon.sun_family);
if (-1 == connect(daemon_fd, (struct sockaddr *)&daemon, len)) {
perror("connect");
//exit(1);
}
}
int main (int argc, char* argv[])
{
/* Argument Parsing */
// define reasonable defaults
data.mode = NRC_REALTIME;
strcpy(data.file, "/usr/local/media/demo.n4s");
// override defaults with user settings
argp_parse (&argp, argc, argv, 0, 0, &data);
/* libev handling */
loop = EV_DEFAULT;
connection_new(EV_A_ "/tmp/libev-ipc-daemon.sock");
// now wait for events to arrive
ev_loop(EV_A_ 0);
// doesn't get here unless unloop is called
return 0;
}