00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "avformat.h"
00022 #include <unistd.h>
00023 #include "network.h"
00024 #include <sys/time.h>
00025
00026 typedef struct TCPContext {
00027 int fd;
00028 } TCPContext;
00029
00030
00031 static int tcp_open(URLContext *h, const char *uri, int flags)
00032 {
00033 struct sockaddr_in dest_addr;
00034 char hostname[1024], *q;
00035 int port, fd = -1;
00036 TCPContext *s = NULL;
00037 fd_set wfds;
00038 int fd_max, ret;
00039 struct timeval tv;
00040 socklen_t optlen;
00041 char proto[1024],path[1024],tmp[1024];
00042
00043 url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
00044 &port, path, sizeof(path), uri);
00045 if (strcmp(proto,"tcp")) goto fail;
00046 if ((q = strchr(hostname,'@'))) { strcpy(tmp,q+1); strcpy(hostname,tmp); }
00047
00048 s = av_malloc(sizeof(TCPContext));
00049 if (!s)
00050 return AVERROR(ENOMEM);
00051 h->priv_data = s;
00052
00053 if (port <= 0 || port >= 65536)
00054 goto fail;
00055
00056 if(!ff_network_init())
00057 return AVERROR(EIO);
00058
00059 dest_addr.sin_family = AF_INET;
00060 dest_addr.sin_port = htons(port);
00061 if (resolve_host(&dest_addr.sin_addr, hostname) < 0)
00062 goto fail;
00063
00064 fd = socket(AF_INET, SOCK_STREAM, 0);
00065 if (fd < 0)
00066 goto fail;
00067 ff_socket_nonblock(fd, 1);
00068
00069 redo:
00070 ret = connect(fd, (struct sockaddr *)&dest_addr,
00071 sizeof(dest_addr));
00072 if (ret < 0) {
00073 if (ff_neterrno() == FF_NETERROR(EINTR))
00074 goto redo;
00075 if (ff_neterrno() != FF_NETERROR(EINPROGRESS) &&
00076 ff_neterrno() != FF_NETERROR(EAGAIN))
00077 goto fail;
00078
00079
00080 for(;;) {
00081 if (url_interrupt_cb()) {
00082 ret = AVERROR(EINTR);
00083 goto fail1;
00084 }
00085 fd_max = fd;
00086 FD_ZERO(&wfds);
00087 FD_SET(fd, &wfds);
00088 tv.tv_sec = 0;
00089 tv.tv_usec = 100 * 1000;
00090 ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
00091 if (ret > 0 && FD_ISSET(fd, &wfds))
00092 break;
00093 }
00094
00095
00096 optlen = sizeof(ret);
00097 getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
00098 if (ret != 0)
00099 goto fail;
00100 }
00101 s->fd = fd;
00102 return 0;
00103
00104 fail:
00105 ret = AVERROR(EIO);
00106 fail1:
00107 if (fd >= 0)
00108 closesocket(fd);
00109 av_free(s);
00110 return ret;
00111 }
00112
00113 static int tcp_read(URLContext *h, uint8_t *buf, int size)
00114 {
00115 TCPContext *s = h->priv_data;
00116 int len, fd_max, ret;
00117 fd_set rfds;
00118 struct timeval tv;
00119
00120 for (;;) {
00121 if (url_interrupt_cb())
00122 return AVERROR(EINTR);
00123 fd_max = s->fd;
00124 FD_ZERO(&rfds);
00125 FD_SET(s->fd, &rfds);
00126 tv.tv_sec = 0;
00127 tv.tv_usec = 100 * 1000;
00128 ret = select(fd_max + 1, &rfds, NULL, NULL, &tv);
00129 if (ret > 0 && FD_ISSET(s->fd, &rfds)) {
00130 len = recv(s->fd, buf, size, 0);
00131 if (len < 0) {
00132 if (ff_neterrno() != FF_NETERROR(EINTR) &&
00133 ff_neterrno() != FF_NETERROR(EAGAIN))
00134 return AVERROR(errno);
00135 } else return len;
00136 } else if (ret < 0) {
00137 return -1;
00138 }
00139 }
00140 }
00141
00142 static int tcp_write(URLContext *h, uint8_t *buf, int size)
00143 {
00144 TCPContext *s = h->priv_data;
00145 int ret, size1, fd_max, len;
00146 fd_set wfds;
00147 struct timeval tv;
00148
00149 size1 = size;
00150 while (size > 0) {
00151 if (url_interrupt_cb())
00152 return AVERROR(EINTR);
00153 fd_max = s->fd;
00154 FD_ZERO(&wfds);
00155 FD_SET(s->fd, &wfds);
00156 tv.tv_sec = 0;
00157 tv.tv_usec = 100 * 1000;
00158 ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
00159 if (ret > 0 && FD_ISSET(s->fd, &wfds)) {
00160 len = send(s->fd, buf, size, 0);
00161 if (len < 0) {
00162 if (ff_neterrno() != FF_NETERROR(EINTR) &&
00163 ff_neterrno() != FF_NETERROR(EAGAIN))
00164 return AVERROR(errno);
00165 continue;
00166 }
00167 size -= len;
00168 buf += len;
00169 } else if (ret < 0) {
00170 return -1;
00171 }
00172 }
00173 return size1 - size;
00174 }
00175
00176 static int tcp_close(URLContext *h)
00177 {
00178 TCPContext *s = h->priv_data;
00179 closesocket(s->fd);
00180 ff_network_close();
00181 av_free(s);
00182 return 0;
00183 }
00184
00185 URLProtocol tcp_protocol = {
00186 "tcp",
00187 tcp_open,
00188 tcp_read,
00189 tcp_write,
00190 NULL,
00191 tcp_close,
00192 };