Changeset 2115 for box/trunk/lib/server/SocketListen.h
- Timestamp:
- 28/03/2008 22:18:44 (4 years ago)
- File:
-
- 1 edited
-
box/trunk/lib/server/SocketListen.h (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
box/trunk/lib/server/SocketListen.h
r456 r2115 109 109 #endif 110 110 { 111 THROW_EXCEPTION(ServerException, SocketCloseError) 111 BOX_LOG_SYS_ERROR("Failed to close network " 112 "socket"); 113 THROW_EXCEPTION(ServerException, 114 SocketCloseError) 112 115 } 113 116 } … … 115 118 } 116 119 117 // ------------------------------------------------------------------ --------120 // ------------------------------------------------------------------ 118 121 // 119 122 // Function … … 122 125 // Created: 2003/07/31 123 126 // 124 // ------------------------------------------------------------------ --------127 // ------------------------------------------------------------------ 125 128 void Listen(int Type, const char *Name, int Port = 0) 126 129 { 127 if(mSocketHandle != -1) {THROW_EXCEPTION(ServerException, SocketAlreadyOpen)} 130 if(mSocketHandle != -1) 131 { 132 THROW_EXCEPTION(ServerException, SocketAlreadyOpen); 133 } 128 134 129 135 // Setup parameters based on type, looking up names if required … … 131 137 SocketAllAddr addr; 132 138 int addrLen = 0; 133 Socket::NameLookupToSockAddr(addr, sockDomain, Type, Name, Port, addrLen); 139 Socket::NameLookupToSockAddr(addr, sockDomain, Type, Name, 140 Port, addrLen); 134 141 135 142 // Create the socket 136 mSocketHandle = ::socket(sockDomain, SOCK_STREAM, 0 /* let OS choose protocol */); 143 mSocketHandle = ::socket(sockDomain, SOCK_STREAM, 144 0 /* let OS choose protocol */); 137 145 if(mSocketHandle == -1) 138 146 { 147 BOX_LOG_SYS_ERROR("Failed to create a network socket"); 139 148 THROW_EXCEPTION(ServerException, SocketOpenError) 140 149 } … … 142 151 // Set an option to allow reuse (useful for -HUP situations!) 143 152 #ifdef WIN32 144 if(::setsockopt(mSocketHandle, SOL_SOCKET, SO_REUSEADDR, "", 0) == -1) 153 if(::setsockopt(mSocketHandle, SOL_SOCKET, SO_REUSEADDR, "", 154 0) == -1) 145 155 #else 146 156 int option = true; 147 if(::setsockopt(mSocketHandle, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option)) == -1) 148 #endif 149 { 157 if(::setsockopt(mSocketHandle, SOL_SOCKET, SO_REUSEADDR, 158 &option, sizeof(option)) == -1) 159 #endif 160 { 161 BOX_LOG_SYS_ERROR("Failed to set socket options"); 150 162 THROW_EXCEPTION(ServerException, SocketOpenError) 151 163 } … … 162 174 } 163 175 164 // ------------------------------------------------------------------ --------176 // ------------------------------------------------------------------ 165 177 // 166 178 // Function 167 179 // Name: SocketListen::Accept(int) 168 // Purpose: Accepts a connection, returning a pointer to a class of 169 // the specified type. May return a null pointer if a signal happens, 170 // or there's a timeout. Timeout specified in milliseconds, defaults to infinite time. 180 // Purpose: Accepts a connection, returning a pointer to 181 // a class of the specified type. May return a 182 // null pointer if a signal happens, or there's 183 // a timeout. Timeout specified in 184 // milliseconds, defaults to infinite time. 171 185 // Created: 2003/07/31 172 186 // 173 // -------------------------------------------------------------------------- 174 std::auto_ptr<SocketType> Accept(int Timeout = INFTIM, std::string *pLogMsg = 0) 175 { 176 if(mSocketHandle == -1) {THROW_EXCEPTION(ServerException, BadSocketHandle)} 187 // ------------------------------------------------------------------ 188 std::auto_ptr<SocketType> Accept(int Timeout = INFTIM, 189 std::string *pLogMsg = 0) 190 { 191 if(mSocketHandle == -1) 192 { 193 THROW_EXCEPTION(ServerException, BadSocketHandle); 194 } 177 195 178 196 // Do the accept, using the supplied locking type … … 186 204 if(!socklock.HaveLock()) 187 205 { 188 // Didn't get the lock for some reason. Wait a while, then 189 // return nothing. 206 // Didn't get the lock for some reason. 207 // Wait a while, then return nothing. 208 BOX_ERROR("Failed to get a lock on incoming " 209 "connection"); 190 210 ::sleep(1); 191 211 return std::auto_ptr<SocketType>(); … … 203 223 if(errno == EINTR) 204 224 { 225 BOX_ERROR("Failed to accept " 226 "connection: interrupted by " 227 "signal"); 205 228 // return nothing 206 229 return std::auto_ptr<SocketType>(); … … 208 231 else 209 232 { 210 THROW_EXCEPTION(ServerException, SocketPollError) 233 BOX_LOG_SYS_ERROR("Failed to poll " 234 "connection"); 235 THROW_EXCEPTION(ServerException, 236 SocketPollError) 211 237 } 212 238 break; … … 221 247 sock = ::accept(mSocketHandle, &addr, &addrlen); 222 248 } 223 // Got socket (or error), unlock (implcit in destruction) 249 250 // Got socket (or error), unlock (implicit in destruction) 224 251 if(sock == -1) 225 252 { 253 BOX_LOG_SYS_ERROR("Failed to accept connection"); 226 254 THROW_EXCEPTION(ServerException, SocketAcceptError) 227 255 } … … 230 258 if(pLogMsg) 231 259 { 232 *pLogMsg = Socket::IncomingConnectionLogMessage(&addr, addrlen); 260 *pLogMsg = Socket::IncomingConnectionLogMessage(&addr, 261 addrlen); 233 262 } 234 263 else … … 244 273 // on multiple sockets. 245 274 #ifdef HAVE_KQUEUE 246 // ------------------------------------------------------------------ --------275 // ------------------------------------------------------------------ 247 276 // 248 277 // Function … … 251 280 // Created: 9/3/04 252 281 // 253 // ------------------------------------------------------------------ --------282 // ------------------------------------------------------------------ 254 283 void FillInKEvent(struct kevent &rEvent, int Flags = 0) const 255 284 { 256 EV_SET(&rEvent, mSocketHandle, EVFILT_READ, 0, 0, 0, (void*)this); 285 EV_SET(&rEvent, mSocketHandle, EVFILT_READ, 0, 0, 0, 286 (void*)this); 257 287 } 258 288 #else 259 // ------------------------------------------------------------------ --------289 // ------------------------------------------------------------------ 260 290 // 261 291 // Function 262 292 // Name: SocketListen::FillInPoll 263 // Purpose: Fills in the data necessary for a poll operation 293 // Purpose: Fills in the data necessary for a poll 294 // operation 264 295 // Created: 9/3/04 265 296 // 266 // ------------------------------------------------------------------ --------297 // ------------------------------------------------------------------ 267 298 void FillInPoll(int &fd, short &events, int Flags = 0) const 268 299 {
Note: See TracChangeset
for help on using the changeset viewer.
