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