From baa590770e6d3729fbaef99277c669ab417e6d8f Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Mon, 7 Nov 2005 22:43:17 +0000 Subject: [PATCH] Port Pipe class to windows. --- src/pipe.cpp | 49 +++++++++++++++++++++++++++++------------- src/thread_pool_observer.cpp | 6 +++--- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/src/pipe.cpp b/src/pipe.cpp index d45dee6..227f31d 100644 --- a/src/pipe.cpp +++ b/src/pipe.cpp @@ -1,5 +1,5 @@ -/* $Id: pipe.cpp,v 1.2 2005-11-07 21:57:10 adam Exp $ +/* $Id: pipe.cpp,v 1.3 2005-11-07 22:43:17 adam Exp $ Copyright (c) 2005, Index Data. %LICENSE% @@ -49,11 +49,24 @@ namespace yp2 { int m_fd[2]; int m_socket; bool nonblock(int s); + void close(int &fd); }; } using namespace yp2; +void Pipe::Rep::close(int &fd) +{ +#ifdef WIN32 + if (fd != -1) + ::closesocket(fd); +#else + if (fd != -1) + ::close(fd); +#endif + fd = -1; +} + Pipe::Rep::Rep() { m_fd[0] = m_fd[1] = -1; @@ -63,6 +76,7 @@ Pipe::Rep::Rep() bool Pipe::Rep::nonblock(int s) { #ifdef WIN32 + unsigned long tru = 1; if (ioctlsocket(s, FIONBIO, &tru) < 0) return false; #else @@ -103,7 +117,7 @@ Pipe::Pipe(int port_to_use) : m_p(new Rep) throw Pipe::Error("could not listen on socket"); // client socket - in_addr_t tmpadd; + unsigned int tmpadd; tmpadd = (unsigned) inet_addr("127.0.0.1"); if (tmpadd) memcpy(&add.sin_addr.s_addr, &tmpadd, sizeof(struct in_addr)); @@ -116,17 +130,24 @@ Pipe::Pipe(int port_to_use) : m_p(new Rep) m_p->nonblock(m_p->m_fd[1]); - if (connect(m_p->m_fd[1], addr, sizeof(*addr)) < 0 && - errno != EINPROGRESS) + if (connect(m_p->m_fd[1], addr, sizeof(*addr)) < 0) { - fprintf(stderr, "errno=%d[%s] tmpadd=%x\n", - errno, strerror(errno), tmpadd); - throw Pipe::Error("could not connect to socket"); +#ifdef WIN32 + if (WSAGetLastError() != WSAEWOULDBLOCK) + throw Pipe::Error("could not connect to socket"); +#else + if (errno != EINPROGRESS) + throw Pipe::Error("could not connect to socket"); +#endif } // server accept struct sockaddr caddr; +#ifdef WIN32 + int caddr_len = sizeof(caddr); +#else socklen_t caddr_len = sizeof(caddr); +#endif m_p->m_fd[0] = accept(m_p->m_socket, &caddr, &caddr_len); if (m_p->m_fd[0] < 0) throw Pipe::Error("could not accept on socket"); @@ -139,23 +160,21 @@ Pipe::Pipe(int port_to_use) : m_p(new Rep) if (r != 1) throw Pipe::Error("could not complete connect"); - close(m_p->m_socket); - m_p->m_socket = -1; + m_p->close(m_p->m_socket); } else { +#ifndef WIN32 pipe(m_p->m_fd); +#endif } } Pipe::~Pipe() { - if (m_p->m_fd[0] != -1) - close(m_p->m_fd[0]); - if (m_p->m_fd[1] != -1) - close(m_p->m_fd[1]); - if (m_p->m_socket != -1) - close(m_p->m_socket); + m_p->close(m_p->m_fd[0]); + m_p->close(m_p->m_fd[1]); + m_p->close(m_p->m_socket); } int &Pipe::read_fd() const diff --git a/src/thread_pool_observer.cpp b/src/thread_pool_observer.cpp index 2f3a0e0..0714770 100644 --- a/src/thread_pool_observer.cpp +++ b/src/thread_pool_observer.cpp @@ -1,5 +1,5 @@ -/* $Id: thread_pool_observer.cpp,v 1.11 2005-11-07 21:57:10 adam Exp $ +/* $Id: thread_pool_observer.cpp,v 1.12 2005-11-07 22:43:17 adam Exp $ Copyright (c) 2005, Index Data. %LICENSE% @@ -109,7 +109,7 @@ void ThreadPoolSocketObserver::socketNotify(int event) if (event & SOCKET_OBSERVE_READ) { char buf[2]; - read(m_p->m_pipe.read_fd(), buf, 1); + recv(m_p->m_pipe.read_fd(), buf, 1, 0); IThreadPoolMsg *out; { boost::mutex::scoped_lock output_lock(m_p->m_mutex_output_data); @@ -140,7 +140,7 @@ void ThreadPoolSocketObserver::run(void *p) { boost::mutex::scoped_lock output_lock(m_p->m_mutex_output_data); m_p->m_output.push_back(out); - write(m_p->m_pipe.write_fd(), "", 1); + send(m_p->m_pipe.write_fd(), "", 1, 0); } } } -- 1.7.10.4