diff options
author | phajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-06-17 07:58:05 +0000 |
---|---|---|
committer | phajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-06-17 07:58:05 +0000 |
commit | fe4f06d49ddccb4f18d9056b85417eb925342ae9 (patch) | |
tree | ff1ffa6d71a2a3a344244bfb1dfbac60ebf13d78 /net | |
parent | 75b91f92b7adfd5fca50369a6717047d787355b3 (diff) | |
download | chromium_src-fe4f06d49ddccb4f18d9056b85417eb925342ae9.zip chromium_src-fe4f06d49ddccb4f18d9056b85417eb925342ae9.tar.gz chromium_src-fe4f06d49ddccb4f18d9056b85417eb925342ae9.tar.bz2 |
Compile fetch_server on POSIX.
More tidy cross-platformness (typedefs instead of #defines) and other simple related cleanups.
TEST=none
http://crbug.com/14191
Review URL: http://codereview.chromium.org/126218
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@18607 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r-- | net/base/listen_socket.cc | 37 | ||||
-rw-r--r-- | net/base/listen_socket.h | 5 | ||||
-rw-r--r-- | net/net.gyp | 41 | ||||
-rw-r--r-- | net/tools/fetch/fetch_server.cc | 2 | ||||
-rw-r--r-- | net/tools/fetch/http_listen_socket.cc | 11 |
5 files changed, 55 insertions, 41 deletions
diff --git a/net/base/listen_socket.cc b/net/base/listen_socket.cc index d31c66d..fd099b7 100644 --- a/net/base/listen_socket.cc +++ b/net/base/listen_socket.cc @@ -21,14 +21,23 @@ #include "net/base/listen_socket.h" #if defined(OS_WIN) -#define socklen_t int -#elif defined(OS_POSIX) -const int INVALID_SOCKET = -1; // Used same name as in Windows to avoid #ifdef -const int SOCKET_ERROR = -1; -#endif +typedef int socklen_t; +#endif // defined(OS_WIN) + +namespace { const int kReadBufSize = 200; +} // namespace + +#if defined(OS_WIN) +const SOCKET ListenSocket::kInvalidSocket = INVALID_SOCKET; +const int ListenSocket::kSocketError = SOCKET_ERROR; +#elif defined(OS_POSIX) +const SOCKET ListenSocket::kInvalidSocket = -1; +const int ListenSocket::kSocketError = -1; +#endif + ListenSocket::ListenSocket(SOCKET s, ListenSocketDelegate *del) : socket_(s), socket_delegate_(del), @@ -53,7 +62,7 @@ ListenSocket::~ListenSocket() { SOCKET ListenSocket::Listen(std::string ip, int port) { SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - if (s != INVALID_SOCKET) { + if (s != kInvalidSocket) { sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; @@ -65,7 +74,7 @@ SOCKET ListenSocket::Listen(std::string ip, int port) { #elif defined(OS_POSIX) close(s); #endif - s = INVALID_SOCKET; + s = kInvalidSocket; } } return s; @@ -74,7 +83,7 @@ SOCKET ListenSocket::Listen(std::string ip, int port) { ListenSocket* ListenSocket::Listen(std::string ip, int port, ListenSocketDelegate* del) { SOCKET s = Listen(ip, port); - if (s == INVALID_SOCKET) { + if (s == kInvalidSocket) { // TODO(erikkay): error handling } else { ListenSocket* sock = new ListenSocket(s, del); @@ -98,7 +107,7 @@ SOCKET ListenSocket::Accept(SOCKET s) { socklen_t from_len = sizeof(from); SOCKET conn = HANDLE_EINTR(accept(s, reinterpret_cast<sockaddr*>(&from), &from_len)); - if (conn != INVALID_SOCKET) { + if (conn != kInvalidSocket) { net::SetNonBlocking(conn); } return conn; @@ -106,7 +115,7 @@ SOCKET ListenSocket::Accept(SOCKET s) { void ListenSocket::Accept() { SOCKET conn = Accept(socket_); - if (conn != INVALID_SOCKET) { + if (conn != kInvalidSocket) { scoped_refptr<ListenSocket> sock = new ListenSocket(conn, socket_delegate_); // it's up to the delegate to AddRef if it wants to keep it around @@ -124,7 +133,7 @@ void ListenSocket::Read() { int len; do { len = HANDLE_EINTR(recv(socket_, buf, kReadBufSize, 0)); - if (len == SOCKET_ERROR) { + if (len == kSocketError) { #if defined(OS_WIN) int err = WSAGetLastError(); if (err == WSAEWOULDBLOCK) { @@ -152,7 +161,7 @@ void ListenSocket::Read() { } void ListenSocket::CloseSocket(SOCKET s) { - if (s && s != INVALID_SOCKET) { + if (s && s != kInvalidSocket) { UnwatchSocket(); #if defined(OS_WIN) closesocket(s); @@ -193,7 +202,7 @@ void ListenSocket::WatchSocket(WaitState state) { void ListenSocket::SendInternal(const char* bytes, int len) { int sent = HANDLE_EINTR(send(socket_, bytes, len, 0)); - if (sent == SOCKET_ERROR) { + if (sent == kSocketError) { #if defined(OS_WIN) int err = WSAGetLastError(); if (err == WSAEWOULDBLOCK) { @@ -238,7 +247,7 @@ void ListenSocket::ResumeReads() { // MessageLoop watcher callback void ListenSocket::OnObjectSignaled(HANDLE object) { WSANETWORKEVENTS ev; - if (SOCKET_ERROR == WSAEnumNetworkEvents(socket_, socket_event_, &ev)) { + if (kSocketError == WSAEnumNetworkEvents(socket_, socket_event_, &ev)) { // TODO return; } diff --git a/net/base/listen_socket.h b/net/base/listen_socket.h index 17fa50d..9ed30b6 100644 --- a/net/base/listen_socket.h +++ b/net/base/listen_socket.h @@ -28,7 +28,7 @@ #if defined(OS_POSIX) struct event; // From libevent -#define SOCKET int +typedef int SOCKET; #endif // Implements a raw socket interface @@ -72,6 +72,9 @@ class ListenSocket : public base::RefCountedThreadSafe<ListenSocket>, void ResumeReads(); protected: + static const SOCKET kInvalidSocket; + static const int kSocketError; + ListenSocket(SOCKET s, ListenSocketDelegate* del); static SOCKET Listen(std::string ip, int port); // if valid, returned SOCKET is non-blocking diff --git a/net/net.gyp b/net/net.gyp index eee05cf..9296919 100644 --- a/net/net.gyp +++ b/net/net.gyp @@ -634,6 +634,26 @@ 'tools/fetch/fetch_client.cc', ], }, + { + 'target_name': 'fetch_server', + 'type': 'executable', + 'dependencies': [ + 'net', + '../base/base.gyp:base', + '../base/base.gyp:test_support_base', + '../testing/gtest.gyp:gtest', + ], + 'msvs_guid': 'DABB8796-B9A2-4CD9-BF89-09B03E92B124', + 'sources': [ + 'tools/fetch/fetch_server.cc', + 'tools/fetch/http_listen_socket.cc', + 'tools/fetch/http_listen_socket.h', + 'tools/fetch/http_server.cc', + 'tools/fetch/http_server.h', + 'tools/fetch/http_session.cc', + 'tools/fetch/http_session.h', + ], + }, ], 'conditions': [ ['OS=="win"', { @@ -652,27 +672,6 @@ 'tools/dump_cache/upgrade.cc', ], }, - { - # TODO(port): fetch_server uses Windows-specific SOCKET. - 'target_name': 'fetch_server', - 'type': 'executable', - 'dependencies': [ - 'net', - '../base/base.gyp:base', - '../base/base.gyp:test_support_base', - '../testing/gtest.gyp:gtest', - ], - 'msvs_guid': 'DABB8796-B9A2-4CD9-BF89-09B03E92B124', - 'sources': [ - 'tools/fetch/fetch_server.cc', - 'tools/fetch/http_listen_socket.cc', - 'tools/fetch/http_listen_socket.h', - 'tools/fetch/http_server.cc', - 'tools/fetch/http_server.h', - 'tools/fetch/http_session.cc', - 'tools/fetch/http_session.h', - ], - }, ], }], ], diff --git a/net/tools/fetch/fetch_server.cc b/net/tools/fetch/fetch_server.cc index ed284f9..5e23fbe 100644 --- a/net/tools/fetch/fetch_server.cc +++ b/net/tools/fetch/fetch_server.cc @@ -27,7 +27,9 @@ int main(int argc, char**argv) { StatsTable table("fetchserver", 50, 1000);
table.set_current(&table);
+#if defined(OS_WIN)
net::EnsureWinsockInit();
+#endif // defined(OS_WIN)
CommandLine::Init(0, NULL);
const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
diff --git a/net/tools/fetch/http_listen_socket.cc b/net/tools/fetch/http_listen_socket.cc index e64a5f2..c04720e 100644 --- a/net/tools/fetch/http_listen_socket.cc +++ b/net/tools/fetch/http_listen_socket.cc @@ -3,6 +3,7 @@ // found in the LICENSE file.
#include "base/compiler_specific.h"
+#include "base/logging.h"
#include "base/message_loop.h"
#include "base/string_util.h"
#include "net/tools/fetch/http_listen_socket.h"
@@ -10,8 +11,8 @@ #include "net/tools/fetch/http_server_response_info.h"
// must run in the IO thread
-HttpListenSocket::HttpListenSocket(SOCKET s,
- HttpListenSocket::Delegate* delegate)
+HttpListenSocket::HttpListenSocket(SOCKET s,
+ HttpListenSocket::Delegate* delegate)
: ALLOW_THIS_IN_INITIALIZER_LIST(ListenSocket(s, this)),
delegate_(delegate) {
}
@@ -22,8 +23,8 @@ HttpListenSocket::~HttpListenSocket() { void HttpListenSocket::Accept() {
SOCKET conn = ListenSocket::Accept(socket_);
- DCHECK(conn != INVALID_SOCKET);
- if (conn == INVALID_SOCKET) {
+ DCHECK_NE(conn, ListenSocket::kInvalidSocket);
+ if (conn == ListenSocket::kInvalidSocket) {
// TODO
} else {
scoped_refptr<HttpListenSocket> sock =
@@ -36,7 +37,7 @@ void HttpListenSocket::Accept() { HttpListenSocket* HttpListenSocket::Listen(const std::string& ip, int port,
HttpListenSocket::Delegate* delegate) {
SOCKET s = ListenSocket::Listen(ip, port);
- if (s == INVALID_SOCKET) {
+ if (s == ListenSocket::kInvalidSocket) {
// TODO (ibrar): error handling
} else {
HttpListenSocket *serv = new HttpListenSocket(s, delegate);
|