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