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
=
93 (mp_net_stream_packet_t
*)malloc(sizeof(mp_net_stream_packet_t
));
95 if(!net_read(fd
,(char*)pack
,sizeof(mp_net_stream_packet_t
))) {
99 pack
->len
= le2me_16(pack
->len
);
101 if(pack
->len
< sizeof(mp_net_stream_packet_t
)) {
102 mp_msg(MSGT_NETST
,MSGL_WARN
,"Got invalid packet (too small: %d)\n",pack
->len
);
106 if(pack
->len
> PACKET_MAX_SIZE
) {
107 mp_msg(MSGT_NETST
,MSGL_WARN
,"Got invalid packet (too big: %d)\n",pack
->len
);
112 if(len
> sizeof(mp_net_stream_packet_t
)) {
113 pack
= realloc(pack
,len
);
115 mp_msg(MSGT_NETST
,MSGL_ERR
,"Failed to get memory for the packet (%d bytes)\n",len
);
118 if(!net_read(fd
,pack
->data
,len
- sizeof(mp_net_stream_packet_t
)))
121 // printf ("Read packet %d %d %d\n",fd,pack->cmd,pack->len);
125 static int net_write(int fd
, char* buf
, int len
) {
128 w
= send(fd
,buf
,len
,0);
130 if(errno
== EINTR
) continue;
132 mp_msg(MSGT_NETST
,MSGL_ERR
,"Write failed: %s\n",strerror(errno
));
141 static int write_packet(int fd
, uint8_t cmd
,char* data
,int len
) {
142 mp_net_stream_packet_t
* pack
= malloc(len
+ sizeof(mp_net_stream_packet_t
));
145 memcpy(pack
->data
,data
,len
);
146 pack
->len
= len
+ sizeof(mp_net_stream_packet_t
);
149 // printf("Write packet %d %d (%p) %d\n",fd,cmd,data,len);
150 pack
->len
= le2me_16(pack
->len
);
151 if(net_write(fd
,(char*)pack
,pack
->len
)) {
159 static void net_stream_opened_2_me(mp_net_stream_opened_t
* o
) {
160 o
->file_format
= le2me_32(o
->file_format
);
161 o
->flags
= le2me_32(o
->flags
);
162 o
->sector_size
= le2me_32(o
->sector_size
);
163 o
->start_pos
= le2me_64(o
->start_pos
);
164 o
->end_pos
= le2me_64(o
->end_pos
);
167 #endif /* MPLAYER_NETSTREAM_H */