2 * Common stuff for netstream
3 * Packets and so on are defined here along with a few helpers
4 * wich are used by both the client and the server
6 * Data is always low endian
8 * This file is part of MPlayer.
10 * MPlayer is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * MPlayer is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #ifndef MPLAYER_NETSTREAM_H
26 #define MPLAYER_NETSTREAM_H
34 #include <sys/socket.h>
39 typedef struct mp_net_stream_packet_st
{
43 } __attribute__ ((packed
)) mp_net_stream_packet_t
;
45 #define PACKET_MAX_SIZE 4096
47 // Commands sent by the client
48 #define NET_STREAM_OPEN 0
50 #define NET_STREAM_FILL_BUFFER 1
51 // data is an uint16 wich is the max len of the data to return
52 #define NET_STREAM_SEEK 3
53 // data is an uint64 wich the pos where to seek
54 #define NET_STREAM_CLOSE 4
56 #define NET_STREAM_RESET 5
60 #define NET_STREAM_OK 128
61 // Data returned if open is successful
62 typedef struct mp_net_stream_opened_st
{
68 } __attribute__ ((packed
)) mp_net_stream_opened_t
;
69 // FILL_BUFFER return the data
70 // CLOSE return nothing
71 #define NET_STREAM_ERROR 129
72 // Data is the error message (if any ;)
74 static int net_read(int fd
, char* buf
, int len
) {
77 r
= recv(fd
,buf
,len
,0);
79 if(errno
== EINTR
) continue;
81 mp_msg(MSGT_NETST
,MSGL_ERR
,"Read failed: %s\n",strerror(errno
));
90 static mp_net_stream_packet_t
* read_packet(int fd
) {
92 mp_net_stream_packet_t
* pack
= malloc(sizeof(mp_net_stream_packet_t
));
94 if(!net_read(fd
,(char*)pack
,sizeof(mp_net_stream_packet_t
))) {
98 pack
->len
= le2me_16(pack
->len
);
100 if(pack
->len
< sizeof(mp_net_stream_packet_t
)) {
101 mp_msg(MSGT_NETST
,MSGL_WARN
,"Got invalid packet (too small: %d)\n",pack
->len
);
105 if(pack
->len
> PACKET_MAX_SIZE
) {
106 mp_msg(MSGT_NETST
,MSGL_WARN
,"Got invalid packet (too big: %d)\n",pack
->len
);
111 if(len
> sizeof(mp_net_stream_packet_t
)) {
112 pack
= realloc(pack
,len
);
114 mp_msg(MSGT_NETST
,MSGL_ERR
,"Failed to get memory for the packet (%d bytes)\n",len
);
117 if(!net_read(fd
,pack
->data
,len
- sizeof(mp_net_stream_packet_t
)))
120 // printf ("Read packet %d %d %d\n",fd,pack->cmd,pack->len);
124 static int net_write(int fd
, char* buf
, int len
) {
127 w
= send(fd
,buf
,len
,DEFAULT_SEND_FLAGS
);
129 if(errno
== EINTR
) continue;
131 mp_msg(MSGT_NETST
,MSGL_ERR
,"Write failed: %s\n",strerror(errno
));
140 static int write_packet(int fd
, uint8_t cmd
,char* data
,int len
) {
141 mp_net_stream_packet_t
* pack
= malloc(len
+ sizeof(mp_net_stream_packet_t
));
144 memcpy(pack
->data
,data
,len
);
145 pack
->len
= len
+ sizeof(mp_net_stream_packet_t
);
148 // printf("Write packet %d %d (%p) %d\n",fd,cmd,data,len);
149 pack
->len
= le2me_16(pack
->len
);
150 if(net_write(fd
,(char*)pack
,pack
->len
)) {
158 static void net_stream_opened_2_me(mp_net_stream_opened_t
* o
) {
159 o
->file_format
= le2me_32(o
->file_format
);
160 o
->flags
= le2me_32(o
->flags
);
161 o
->sector_size
= le2me_32(o
->sector_size
);
162 o
->start_pos
= le2me_64(o
->start_pos
);
163 o
->end_pos
= le2me_64(o
->end_pos
);
166 #endif /* MPLAYER_NETSTREAM_H */