Add explanatory comments to the #endif part of multiple inclusion guards.
[mplayer/greg.git] / stream / netstream.h
blob61abfc8e8a6af55a1582cab0a79e4998f1dd23e3
2 /*
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
8 */
10 typedef struct mp_net_stream_packet_st {
11 uint16_t len;
12 uint8_t cmd;
13 char data[0];
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
20 // data is the url
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
26 // no data
27 #define NET_STREAM_RESET 5
28 // no data
30 // Server response
31 #define NET_STREAM_OK 128
32 // Data returned if open is successful
33 typedef struct mp_net_stream_opened_st {
34 uint32_t file_format;
35 uint32_t flags;
36 uint32_t sector_size;
37 uint64_t start_pos;
38 uint64_t end_pos;
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) {
46 int r = 0;
47 while(len) {
48 r = recv(fd,buf,len,0);
49 if(r <= 0) {
50 if(errno == EINTR) continue;
51 if(r < 0)
52 mp_msg(MSGT_NETST,MSGL_ERR,"Read failed: %s\n",strerror(errno));
53 return 0;
55 len -= r;
56 buf += r;
58 return 1;
61 static mp_net_stream_packet_t* read_packet(int fd) {
62 uint16_t len;
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))) {
67 free(pack);
68 return NULL;
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);
74 free(pack);
75 return NULL;
77 if(pack->len > PACKET_MAX_SIZE) {
78 mp_msg(MSGT_NETST,MSGL_WARN,"Got invalid packet (too big: %d)\n",pack->len);
79 free(pack);
80 return NULL;
82 len = pack->len;
83 if(len > sizeof(mp_net_stream_packet_t)) {
84 pack = realloc(pack,len);
85 if(!pack) {
86 mp_msg(MSGT_NETST,MSGL_ERR,"Failed to get memory for the packet (%d bytes)\n",len);
87 return NULL;
89 if(!net_read(fd,pack->data,len - sizeof(mp_net_stream_packet_t)))
90 return NULL;
92 // printf ("Read packet %d %d %d\n",fd,pack->cmd,pack->len);
93 return pack;
96 static int net_write(int fd, char* buf, int len) {
97 int w;
98 while(len) {
99 w = send(fd,buf,len,0);
100 if(w <= 0) {
101 if(errno == EINTR) continue;
102 if(w < 0)
103 mp_msg(MSGT_NETST,MSGL_ERR,"Write failed: %s\n",strerror(errno));
104 return 0;
106 len -= w;
107 buf += w;
109 return 1;
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));
115 if(len > 0 && data)
116 memcpy(pack->data,data,len);
117 pack->len = len + sizeof(mp_net_stream_packet_t);
118 pack->cmd = cmd;
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)) {
123 free(pack);
124 return 1;
126 free(pack);
127 return 0;
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);