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 typedef struct mp_net_stream_packet_st
{
14 } __attribute__ ((packed
)) mp_net_stream_packet_t
;
16 #define PACKET_MAX_SIZE 4096
18 // Commands sent by the client
19 #define NET_STREAM_OPEN 0
21 #define NET_STREAM_FILL_BUFFER 1
22 // data is an uint16 wich is the max len of the data to return
23 #define NET_STREAM_SEEK 3
24 // data is an uint64 wich the pos where to seek
25 #define NET_STREAM_CLOSE 4
27 #define NET_STREAM_RESET 5
31 #define NET_STREAM_OK 128
32 // Data returned if open is successful
33 typedef struct mp_net_stream_opened_st
{
39 } __attribute__ ((packed
)) mp_net_stream_opened_t
;
40 // FILL_BUFFER return the data
41 // CLOSE return nothing
42 #define NET_STREAM_ERROR 129
43 // Data is the error message (if any ;)
45 static int net_read(int fd
, char* buf
, int len
) {
48 r
= recv(fd
,buf
,len
,0);
50 if(errno
== EINTR
) continue;
52 mp_msg(MSGT_NETST
,MSGL_ERR
,"Read failed: %s\n",strerror(errno
));
61 static mp_net_stream_packet_t
* read_packet(int fd
) {
63 mp_net_stream_packet_t
* pack
=
64 (mp_net_stream_packet_t
*)malloc(sizeof(mp_net_stream_packet_t
));
66 if(!net_read(fd
,(char*)pack
,sizeof(mp_net_stream_packet_t
))) {
70 pack
->len
= le2me_16(pack
->len
);
72 if(pack
->len
< sizeof(mp_net_stream_packet_t
)) {
73 mp_msg(MSGT_NETST
,MSGL_WARN
,"Got invalid packet (too small: %d)\n",pack
->len
);
77 if(pack
->len
> PACKET_MAX_SIZE
) {
78 mp_msg(MSGT_NETST
,MSGL_WARN
,"Got invalid packet (too big: %d)\n",pack
->len
);
83 if(len
> sizeof(mp_net_stream_packet_t
)) {
84 pack
= realloc(pack
,len
);
86 mp_msg(MSGT_NETST
,MSGL_ERR
,"Failed to get memory for the packet (%d bytes)\n",len
);
89 if(!net_read(fd
,pack
->data
,len
- sizeof(mp_net_stream_packet_t
)))
92 // printf ("Read packet %d %d %d\n",fd,pack->cmd,pack->len);
96 static int net_write(int fd
, char* buf
, int len
) {
99 w
= send(fd
,buf
,len
,0);
101 if(errno
== EINTR
) continue;
103 mp_msg(MSGT_NETST
,MSGL_ERR
,"Write failed: %s\n",strerror(errno
));
112 static int write_packet(int fd
, uint8_t cmd
,char* data
,int len
) {
113 mp_net_stream_packet_t
* pack
= malloc(len
+ sizeof(mp_net_stream_packet_t
));
116 memcpy(pack
->data
,data
,len
);
117 pack
->len
= len
+ sizeof(mp_net_stream_packet_t
);
120 // printf("Write packet %d %d (%p) %d\n",fd,cmd,data,len);
121 pack
->len
= le2me_16(pack
->len
);
122 if(net_write(fd
,(char*)pack
,pack
->len
)) {
130 static void net_stream_opened_2_me(mp_net_stream_opened_t
* o
) {
131 o
->file_format
= le2me_32(o
->file_format
);
132 o
->flags
= le2me_32(o
->flags
);
133 o
->sector_size
= le2me_32(o
->sector_size
);
134 o
->start_pos
= le2me_64(o
->start_pos
);
135 o
->end_pos
= le2me_64(o
->end_pos
);