1 | //vio_winsock.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2012 |
---|
5 | * |
---|
6 | * This file is part of libroar a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * libroar is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | * NOTE for everyone want's to change something and send patches: |
---|
25 | * read README and HACKING! There a addition information on |
---|
26 | * the license of this document you need to read before you send |
---|
27 | * any patches. |
---|
28 | * |
---|
29 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
30 | * or libpulse*: |
---|
31 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
32 | * and are therefore GPL. Because of this it may be illigal to use |
---|
33 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
34 | */ |
---|
35 | |
---|
36 | #include "libroar.h" |
---|
37 | |
---|
38 | #ifdef ROAR_TARGET_WIN32 |
---|
39 | |
---|
40 | static int _get_fh(struct roar_vio_calls * vio) { |
---|
41 | int fh; |
---|
42 | |
---|
43 | if ( roar_vio_basic_ctl(vio, ROAR_VIO_CTL_GET_FH, &fh) == -1 ) |
---|
44 | return -1; |
---|
45 | |
---|
46 | return fh; |
---|
47 | } |
---|
48 | |
---|
49 | ssize_t roar_vio_winsock_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
50 | return recv(_get_fh(vio), buf, count, 0); |
---|
51 | } |
---|
52 | |
---|
53 | ssize_t roar_vio_winsock_write (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
54 | return send(_get_fh(vio), buf, count, 0); |
---|
55 | } |
---|
56 | |
---|
57 | int roar_vio_winsock_sync (struct roar_vio_calls * vio) { |
---|
58 | return 0; |
---|
59 | } |
---|
60 | |
---|
61 | int roar_vio_winsock_ctl (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) { |
---|
62 | int tmp; |
---|
63 | int s_r = 0, s_w = 0; |
---|
64 | |
---|
65 | if ( vio == NULL || cmd == -1 ) |
---|
66 | return -1; |
---|
67 | |
---|
68 | ROAR_DBG("roar_vio_winsock_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data); |
---|
69 | |
---|
70 | switch (cmd) { |
---|
71 | case ROAR_VIO_CTL_GET_NAME: |
---|
72 | if ( data == NULL ) |
---|
73 | return -1; |
---|
74 | |
---|
75 | *(char**)data = "winsock"; |
---|
76 | return 0; |
---|
77 | break; |
---|
78 | case ROAR_VIO_CTL_GET_SELECT_FH: |
---|
79 | case ROAR_VIO_CTL_GET_SELECT_READ_FH: |
---|
80 | case ROAR_VIO_CTL_GET_SELECT_WRITE_FH: |
---|
81 | ROAR_DBG("roar_vio_winsock_ctl(vio=%p, cmd=ROAR_VIO_CTL_GET_SELECT_*FH(0x%.8x), data=%p) = 0 // fh=%i", vio, cmd, data, _get_fh(vio)); |
---|
82 | *(int*)data = _get_fh(vio); |
---|
83 | return 0; |
---|
84 | break; |
---|
85 | case ROAR_VIO_CTL_SET_NOSYNC: |
---|
86 | vio->sync = NULL; |
---|
87 | return 0; |
---|
88 | break; |
---|
89 | case ROAR_VIO_CTL_ACCEPT: |
---|
90 | tmp = accept(_get_fh(vio), NULL, 0); |
---|
91 | if ( tmp == -1 ) |
---|
92 | return -1; |
---|
93 | |
---|
94 | // most proably a socket. |
---|
95 | if ( roar_vio_open_fh_socket(data, tmp) == -1 ) { |
---|
96 | closesocket(tmp); |
---|
97 | return -1; |
---|
98 | } |
---|
99 | |
---|
100 | return 0; |
---|
101 | break; |
---|
102 | case ROAR_VIO_CTL_SHUTDOWN: |
---|
103 | tmp = *(int*)data; |
---|
104 | |
---|
105 | if ( tmp & ROAR_VIO_SHUTDOWN_READ ) { |
---|
106 | s_r = 1; |
---|
107 | tmp -= ROAR_VIO_SHUTDOWN_READ; |
---|
108 | } |
---|
109 | |
---|
110 | if ( tmp & ROAR_VIO_SHUTDOWN_WRITE ) { |
---|
111 | s_w = 1; |
---|
112 | tmp -= ROAR_VIO_SHUTDOWN_WRITE; |
---|
113 | } |
---|
114 | |
---|
115 | if ( tmp != 0 ) /* we currently only support R and W shutdowns */ |
---|
116 | return -1; |
---|
117 | |
---|
118 | if ( s_r && s_w ) { |
---|
119 | tmp = SHUT_RDWR; |
---|
120 | } else if ( s_r ) { |
---|
121 | tmp = SHUT_RD; |
---|
122 | } else if ( s_w ) { |
---|
123 | tmp = SHUT_WR; |
---|
124 | } else { |
---|
125 | return 0; // nothing to do. |
---|
126 | } |
---|
127 | |
---|
128 | return shutdown(_get_fh(vio), tmp); |
---|
129 | break; |
---|
130 | case ROAR_VIO_CTL_GET_SOCKNAME: |
---|
131 | case ROAR_VIO_CTL_GET_PEERNAME: |
---|
132 | return roar_vio_basic_ctl(vio, cmd, data); |
---|
133 | break; |
---|
134 | } |
---|
135 | |
---|
136 | roar_err_set(ROAR_ERROR_BADRQC); |
---|
137 | return -1; |
---|
138 | } |
---|
139 | |
---|
140 | int roar_vio_winsock_close (struct roar_vio_calls * vio) { |
---|
141 | |
---|
142 | closesocket(_get_fh(vio)); |
---|
143 | |
---|
144 | return 0; |
---|
145 | } |
---|
146 | #endif |
---|
147 | |
---|
148 | //ll |
---|