sync with en/mplayer.1 rev. 30611
[mplayer/glamo.git] / stream / stream_netstream.h
bloba81bc37c83ea319656c1f60c6549f23a5c4944ab
1 /*
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
28 #include "config.h"
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <errno.h>
33 #if !HAVE_WINSOCK2_H
34 #include <sys/socket.h>
35 #endif
36 #include "mp_msg.h"
37 #include "mpbswap.h"
39 typedef struct mp_net_stream_packet_st {
40 uint16_t len;
41 uint8_t cmd;
42 char data[0];
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
49 // data is the url
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
55 // no data
56 #define NET_STREAM_RESET 5
57 // no data
59 // Server response
60 #define NET_STREAM_OK 128
61 // Data returned if open is successful
62 typedef struct mp_net_stream_opened_st {
63 uint32_t file_format;
64 uint32_t flags;
65 uint32_t sector_size;
66 uint64_t start_pos;
67 uint64_t end_pos;
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) {
75 int r = 0;
76 while(len) {
77 r = recv(fd,buf,len,0);
78 if(r <= 0) {
79 if(errno == EINTR) continue;
80 if(r < 0)
81 mp_msg(MSGT_NETST,MSGL_ERR,"Read failed: %s\n",strerror(errno));
82 return 0;
84 len -= r;
85 buf += r;
87 return 1;
90 static mp_net_stream_packet_t* read_packet(int fd) {
91 uint16_t len;
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))) {
96 free(pack);
97 return NULL;
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);
103 free(pack);
104 return NULL;
106 if(pack->len > PACKET_MAX_SIZE) {
107 mp_msg(MSGT_NETST,MSGL_WARN,"Got invalid packet (too big: %d)\n",pack->len);
108 free(pack);
109 return NULL;
111 len = pack->len;
112 if(len > sizeof(mp_net_stream_packet_t)) {
113 pack = realloc(pack,len);
114 if(!pack) {
115 mp_msg(MSGT_NETST,MSGL_ERR,"Failed to get memory for the packet (%d bytes)\n",len);
116 return NULL;
118 if(!net_read(fd,pack->data,len - sizeof(mp_net_stream_packet_t)))
119 return NULL;
121 // printf ("Read packet %d %d %d\n",fd,pack->cmd,pack->len);
122 return pack;
125 static int net_write(int fd, char* buf, int len) {
126 int w;
127 while(len) {
128 w = send(fd,buf,len,0);
129 if(w <= 0) {
130 if(errno == EINTR) continue;
131 if(w < 0)
132 mp_msg(MSGT_NETST,MSGL_ERR,"Write failed: %s\n",strerror(errno));
133 return 0;
135 len -= w;
136 buf += w;
138 return 1;
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));
144 if(len > 0 && data)
145 memcpy(pack->data,data,len);
146 pack->len = len + sizeof(mp_net_stream_packet_t);
147 pack->cmd = cmd;
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)) {
152 free(pack);
153 return 1;
155 free(pack);
156 return 0;
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 */