Translation system changes part 2: replace macros by strings
[mplayer.git] / stream / stream_netstream.h
blob6ae46cabefb2f27a148c7cbc00c3a1f1bbe2ff5e
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 #ifndef MPLAYER_NETSTREAM_H
11 #define MPLAYER_NETSTREAM_H
13 #include <stdint.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <errno.h>
17 #if !HAVE_WINSOCK2_H
18 #include <sys/socket.h>
19 #endif
20 #include "mp_msg.h"
21 #include "mpbswap.h"
23 typedef struct mp_net_stream_packet_st {
24 uint16_t len;
25 uint8_t cmd;
26 char data[0];
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
33 // data is the url
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
39 // no data
40 #define NET_STREAM_RESET 5
41 // no data
43 // Server response
44 #define NET_STREAM_OK 128
45 // Data returned if open is successful
46 typedef struct mp_net_stream_opened_st {
47 uint32_t file_format;
48 uint32_t flags;
49 uint32_t sector_size;
50 uint64_t start_pos;
51 uint64_t end_pos;
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) {
59 int r = 0;
60 while(len) {
61 r = recv(fd,buf,len,0);
62 if(r <= 0) {
63 if(errno == EINTR) continue;
64 if(r < 0)
65 mp_msg(MSGT_NETST,MSGL_ERR,"Read failed: %s\n",strerror(errno));
66 return 0;
68 len -= r;
69 buf += r;
71 return 1;
74 static mp_net_stream_packet_t* read_packet(int fd) {
75 uint16_t len;
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))) {
80 free(pack);
81 return NULL;
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);
87 free(pack);
88 return NULL;
90 if(pack->len > PACKET_MAX_SIZE) {
91 mp_msg(MSGT_NETST,MSGL_WARN,"Got invalid packet (too big: %d)\n",pack->len);
92 free(pack);
93 return NULL;
95 len = pack->len;
96 if(len > sizeof(mp_net_stream_packet_t)) {
97 pack = realloc(pack,len);
98 if(!pack) {
99 mp_msg(MSGT_NETST,MSGL_ERR,"Failed to get memory for the packet (%d bytes)\n",len);
100 return NULL;
102 if(!net_read(fd,pack->data,len - sizeof(mp_net_stream_packet_t)))
103 return NULL;
105 // printf ("Read packet %d %d %d\n",fd,pack->cmd,pack->len);
106 return pack;
109 static int net_write(int fd, char* buf, int len) {
110 int w;
111 while(len) {
112 w = send(fd,buf,len,0);
113 if(w <= 0) {
114 if(errno == EINTR) continue;
115 if(w < 0)
116 mp_msg(MSGT_NETST,MSGL_ERR,"Write failed: %s\n",strerror(errno));
117 return 0;
119 len -= w;
120 buf += w;
122 return 1;
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));
128 if(len > 0 && data)
129 memcpy(pack->data,data,len);
130 pack->len = len + sizeof(mp_net_stream_packet_t);
131 pack->cmd = cmd;
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)) {
136 free(pack);
137 return 1;
139 free(pack);
140 return 0;
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 */