output: add _get_plugin()
[libmpdclient.git] / test / capture.c
blob63362d368d1af4569659ed4a0d07341c4989cf9f
1 /* libmpdclient
2 (c) 2003-2017 The Music Player Daemon Project
3 This project's homepage is: http://www.musicpd.org
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
9 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 - Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
20 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "capture.h"
31 #include <mpd/connection.h>
32 #include <mpd/async.h>
34 #include <stdio.h>
35 #include <string.h>
37 #ifdef _WIN32
38 # include <winsock2.h>
39 # include <basetsd.h> /* for SSIZE_T */
40 typedef SSIZE_T ssize_t;
41 #else
42 # include <sys/socket.h>
43 # include <unistd.h>
44 #endif
46 struct mpd_connection *
47 test_capture_init(struct test_capture *tc)
49 int sv[2];
51 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv) < 0) {
52 perror("socketpair() failed");
53 return NULL;
56 tc->fd = sv[0];
58 struct mpd_async *async = mpd_async_new(sv[1]);
59 if (async == NULL) {
60 close(sv[0]);
61 close(sv[1]);
62 return NULL;
65 struct mpd_connection *c =
66 mpd_connection_new_async(async, "OK MPD 0.21.0");
67 if (c == NULL) {
68 mpd_async_free(async);
69 close(sv[0]);
70 return NULL;
73 return c;
76 void
77 test_capture_deinit(struct test_capture *tc)
79 close(tc->fd);
82 const char *
83 test_capture_receive(struct test_capture *tc)
85 ssize_t nbytes = recv(tc->fd, tc->buffer, sizeof(tc->buffer) - 1,
86 MSG_DONTWAIT);
87 if (nbytes < 0) {
88 perror("recv() failed");
89 return NULL;
92 tc->buffer[nbytes] = 0;
93 return tc->buffer;
96 bool
97 test_capture_send(struct test_capture *tc, const char *response)
99 ssize_t nbytes = send(tc->fd, response, strlen(response),
100 MSG_DONTWAIT);
101 if (nbytes < 0) {
102 perror("send() failed");
103 return false;
106 return true;