00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00025 #include "platform.h"
00026 #include "internal.h"
00027 #include "session.h"
00028 #include "io_raw.h"
00029
00030 #include <netinet/tcp.h>
00031
00032
00033 void
00034 SPDYF_raw_global_init()
00035 {
00036 }
00037
00038
00039 void
00040 SPDYF_raw_global_deinit()
00041 {
00042 }
00043
00044
00045 int
00046 SPDYF_raw_init(struct SPDY_Daemon *daemon)
00047 {
00048 (void)daemon;
00049
00050 return SPDY_YES;
00051 }
00052
00053
00054 void
00055 SPDYF_raw_deinit(struct SPDY_Daemon *daemon)
00056 {
00057 (void)daemon;
00058 }
00059
00060
00061 int
00062 SPDYF_raw_new_session(struct SPDY_Session *session)
00063 {
00064 int fd_flags;
00065 int val = 1;
00066 int ret;
00067
00068
00069 fd_flags = fcntl (session->socket_fd, F_GETFL);
00070 if ( -1 == fd_flags
00071 || 0 != fcntl (session->socket_fd, F_SETFL, fd_flags | O_NONBLOCK))
00072 SPDYF_DEBUG("WARNING: Couldn't set the new connection to be non-blocking");
00073
00074 if(SPDY_DAEMON_FLAG_NO_DELAY & session->daemon->flags)
00075 {
00076 ret = setsockopt(session->socket_fd, IPPROTO_TCP, TCP_NODELAY, &val, (socklen_t)sizeof(val));
00077 if(-1 == ret)
00078 SPDYF_DEBUG("WARNING: Couldn't set the new connection to TCP_NODELAY");
00079 }
00080
00081 return SPDY_YES;
00082 }
00083
00084
00085 void
00086 SPDYF_raw_close_session(struct SPDY_Session *session)
00087 {
00088 (void)session;
00089 }
00090
00091
00092 int
00093 SPDYF_raw_recv(struct SPDY_Session *session,
00094 void * buffer,
00095 size_t size)
00096 {
00097 int n = read(session->socket_fd,
00098 buffer,
00099 size);
00100
00101 if (n < 0)
00102 {
00103 switch(errno)
00104 {
00105 case EAGAIN:
00106 #if EAGAIN != EWOULDBLOCK
00107 case EWOULDBLOCK:
00108 #endif
00109 case EINTR:
00110 return SPDY_IO_ERROR_AGAIN;
00111
00112 default:
00113 return SPDY_IO_ERROR_ERROR;
00114 }
00115 }
00116
00117 return n;
00118 }
00119
00120
00121 int
00122 SPDYF_raw_send(struct SPDY_Session *session,
00123 const void * buffer,
00124 size_t size)
00125 {
00126 int n = write(session->socket_fd,
00127 buffer,
00128 size);
00129
00130 if (n < 0)
00131 {
00132 switch(errno)
00133 {
00134 case EAGAIN:
00135 #if EAGAIN != EWOULDBLOCK
00136 case EWOULDBLOCK:
00137 #endif
00138 case EINTR:
00139 return SPDY_IO_ERROR_AGAIN;
00140
00141 default:
00142 return SPDY_IO_ERROR_ERROR;
00143 }
00144 }
00145
00146 return n;
00147 }
00148
00149
00150 int
00151 SPDYF_raw_is_pending(struct SPDY_Session *session)
00152 {
00153 (void)session;
00154
00155 return SPDY_NO;
00156 }
00157
00158
00159 int
00160 SPDYF_raw_before_write(struct SPDY_Session *session)
00161 {
00162 #if HAVE_DECL_TCP_CORK
00163 if(0 == (SPDY_DAEMON_FLAG_NO_DELAY & session->daemon->flags))
00164 {
00165 int val = 1;
00166 int ret;
00167
00168 ret = setsockopt(session->socket_fd, IPPROTO_TCP, TCP_CORK, &val, (socklen_t)sizeof(val));
00169 if(-1 == ret)
00170 SPDYF_DEBUG("WARNING: Couldn't set the new connection to TCP_CORK");
00171 }
00172 #endif
00173
00174 return SPDY_YES;
00175 }
00176
00177
00178 int
00179 SPDYF_raw_after_write(struct SPDY_Session *session, int was_written)
00180 {
00181 #if HAVE_DECL_TCP_CORK
00182 if(SPDY_YES == was_written && 0 == (SPDY_DAEMON_FLAG_NO_DELAY & session->daemon->flags))
00183 {
00184 int val = 0;
00185 int ret;
00186
00187 ret = setsockopt(session->socket_fd, IPPROTO_TCP, TCP_CORK, &val, (socklen_t)sizeof(val));
00188 if(-1 == ret)
00189 SPDYF_DEBUG("WARNING: Couldn't unset the new connection to TCP_CORK");
00190 }
00191
00192 #endif
00193 return was_written;
00194 }