3 * Common stuff for netstream
4 * Packets and so on are defined here along with a few helpers
5 * wich are used by both the client and the server
7 * Data is always low endian
10 #ifndef MPLAYER_NETSTREAM_H
11 #define MPLAYER_NETSTREAM_H
18 #include <sys/socket.h>
23 typedef struct mp_net_stream_packet_st
{
27 } __attribute__ ((packed
)) mp_net_stream_packet_t
;
29 #define PACKET_MAX_SIZE 4096
31 // Commands sent by the client
32 #define NET_STREAM_OPEN 0
34 #define NET_STREAM_FILL_BUFFER 1
35 // data is an uint16 wich is the max len of the data to return
36 #define NET_STREAM_SEEK 3
37 // data is an uint64 wich the pos where to seek
38 #define NET_STREAM_CLOSE 4
40 #define NET_STREAM_RESET 5
44 #define NET_STREAM_OK 128
45 // Data returned if open is successful
46 typedef struct mp_net_stream_opened_st
{
52 } __attribute__ ((packed
)) mp_net_stream_opened_t
;
53 // FILL_BUFFER return the data
54 // CLOSE return nothing
55 #define NET_STREAM_ERROR 129
56 // Data is the error message (if any ;)
58 static int net_read(int fd
, char* buf
, int len
) {
61 r
= recv(fd
,buf
,len
,0);
63 if(errno
== EINTR
) continue;
65 mp_msg(MSGT_NETST
,MSGL_ERR
,"Read failed: %s\n",strerror(errno
));
74 static mp_net_stream_packet_t
* read_packet(int fd
) {
76 mp_net_stream_packet_t
* pack
=
77 (mp_net_stream_packet_t
*)malloc(sizeof(mp_net_stream_packet_t
));
79 if(!net_read(fd
,(char*)pack
,sizeof(mp_net_stream_packet_t
))) {
83 pack
->len
= le2me_16(pack
->len
);
85 if(pack
->len
< sizeof(mp_net_stream_packet_t
)) {
86 mp_msg(MSGT_NETST
,MSGL_WARN
,"Got invalid packet (too small: %d)\n",pack
->len
);
90 if(pack
->len
> PACKET_MAX_SIZE
) {
91 mp_msg(MSGT_NETST
,MSGL_WARN
,"Got invalid packet (too big: %d)\n",pack
->len
);
96 if(len
> sizeof(mp_net_stream_packet_t
)) {
97 pack
= realloc(pack
,len
);
99 mp_msg(MSGT_NETST
,MSGL_ERR
,"Failed to get memory for the packet (%d bytes)\n",len
);
102 if(!net_read(fd
,pack
->data
,len
- sizeof(mp_net_stream_packet_t
)))
105 // printf ("Read packet %d %d %d\n",fd,pack->cmd,pack->len);
109 static int net_write(int fd
, char* buf
, int len
) {
112 w
= send(fd
,buf
,len
,0);
114 if(errno
== EINTR
) continue;
116 mp_msg(MSGT_NETST
,MSGL_ERR
,"Write failed: %s\n",strerror(errno
));
125 static int write_packet(int fd
, uint8_t cmd
,char* data
,int len
) {
126 mp_net_stream_packet_t
* pack
= malloc(len
+ sizeof(mp_net_stream_packet_t
));
129 memcpy(pack
->data
,data
,len
);
130 pack
->len
= len
+ sizeof(mp_net_stream_packet_t
);
133 // printf("Write packet %d %d (%p) %d\n",fd,cmd,data,len);
134 pack
->len
= le2me_16(pack
->len
);
135 if(net_write(fd
,(char*)pack
,pack
->len
)) {
143 static void net_stream_opened_2_me(mp_net_stream_opened_t
* o
) {
144 o
->file_format
= le2me_32(o
->file_format
);
145 o
->flags
= le2me_32(o
->flags
);
146 o
->sector_size
= le2me_32(o
->sector_size
);
147 o
->start_pos
= le2me_64(o
->start_pos
);
148 o
->end_pos
= le2me_64(o
->end_pos
);
151 #endif /* MPLAYER_NETSTREAM_H */