From 57fb87ad1065c337ad8fe1ce9720aeb6206db833 Mon Sep 17 00:00:00 2001 From: crs Date: Mon, 17 Jun 2002 12:02:26 +0000 Subject: [PATCH] refactored ISocket into IDataSocket. the latter and IListenSocket now derive from ISocket. --- client/CClient.cpp | 4 ++-- net/CTCPListenSocket.cpp | 2 +- net/CTCPListenSocket.h | 6 ++++-- net/CTCPSocket.cpp | 34 +++++++++++++++++----------------- net/CTCPSocket.h | 8 +++++--- net/IDataSocket.h | 28 ++++++++++++++++++++++++++++ net/IListenSocket.h | 19 ++++++++----------- net/ISocket.h | 10 ---------- server/CHTTPServer.cpp | 4 ++-- server/CHTTPServer.h | 4 ++-- server/CServer.cpp | 10 +++++----- synergy/CTCPSocketFactory.cpp | 2 +- synergy/CTCPSocketFactory.h | 2 +- synergy/ISocketFactory.h | 4 ++-- 14 files changed, 78 insertions(+), 59 deletions(-) create mode 100644 net/IDataSocket.h diff --git a/client/CClient.cpp b/client/CClient.cpp index 394efdf0..579f007d 100644 --- a/client/CClient.cpp +++ b/client/CClient.cpp @@ -197,7 +197,7 @@ CClient::runSession(void*) { log((CLOG_DEBUG "starting client \"%s\"", m_name.c_str())); - std::auto_ptr socket; + std::auto_ptr socket; std::auto_ptr input; std::auto_ptr output; try { @@ -209,7 +209,7 @@ CClient::runSession(void*) // create socket and attempt to connect to server log((CLOG_DEBUG1 "connecting to server")); - assign(socket, new CTCPSocket(), ISocket); // FIXME -- use factory + assign(socket, new CTCPSocket(), IDataSocket); // FIXME -- use factory socket->connect(*m_serverAddress); log((CLOG_INFO "connected to server")); break; diff --git a/net/CTCPListenSocket.cpp b/net/CTCPListenSocket.cpp index f7f2266e..abf72602 100644 --- a/net/CTCPListenSocket.cpp +++ b/net/CTCPListenSocket.cpp @@ -43,7 +43,7 @@ CTCPListenSocket::bind( } } -ISocket* +IDataSocket* CTCPListenSocket::accept() { CNetwork::PollEntry pfds[1]; diff --git a/net/CTCPListenSocket.h b/net/CTCPListenSocket.h index 3120d00b..d89f0049 100644 --- a/net/CTCPListenSocket.h +++ b/net/CTCPListenSocket.h @@ -13,11 +13,13 @@ public: // accessors - // IListenSocket overrides + // ISocket overrides virtual void bind(const CNetworkAddress&); - virtual ISocket* accept(); virtual void close(); + // IListenSocket overrides + virtual IDataSocket* accept(); + private: CNetwork::Socket m_fd; }; diff --git a/net/CTCPSocket.cpp b/net/CTCPSocket.cpp index fdc640f3..6881f7b7 100644 --- a/net/CTCPSocket.cpp +++ b/net/CTCPSocket.cpp @@ -67,23 +67,6 @@ CTCPSocket::bind( } } -void -CTCPSocket::connect( - const CNetworkAddress& addr) -{ - CThread::testCancel(); - if (CNetwork::connect(m_fd, addr.getAddress(), - addr.getAddressLength()) == CNetwork::Error) { - CThread::testCancel(); - throw XSocketConnect(); - } - - // start servicing the socket - m_connected = kReadWrite; - m_thread = new CThread(new TMethodJob( - this, &CTCPSocket::ioThread)); -} - void CTCPSocket::close() { @@ -124,6 +107,23 @@ CTCPSocket::close() } } +void +CTCPSocket::connect( + const CNetworkAddress& addr) +{ + CThread::testCancel(); + if (CNetwork::connect(m_fd, addr.getAddress(), + addr.getAddressLength()) == CNetwork::Error) { + CThread::testCancel(); + throw XSocketConnect(); + } + + // start servicing the socket + m_connected = kReadWrite; + m_thread = new CThread(new TMethodJob( + this, &CTCPSocket::ioThread)); +} + IInputStream* CTCPSocket::getInputStream() { diff --git a/net/CTCPSocket.h b/net/CTCPSocket.h index 980e0dec..a38789df 100644 --- a/net/CTCPSocket.h +++ b/net/CTCPSocket.h @@ -1,7 +1,7 @@ #ifndef CTCPSOCKET_H #define CTCPSOCKET_H -#include "ISocket.h" +#include "IDataSocket.h" #include "CNetwork.h" class CMutex; @@ -11,7 +11,7 @@ class CThread; class CBufferedInputStream; class CBufferedOutputStream; -class CTCPSocket : public ISocket { +class CTCPSocket : public IDataSocket { public: CTCPSocket(); CTCPSocket(CNetwork::Socket); @@ -23,8 +23,10 @@ public: // ISocket overrides virtual void bind(const CNetworkAddress&); - virtual void connect(const CNetworkAddress&); virtual void close(); + + // IDataSocket overrides + virtual void connect(const CNetworkAddress&); virtual IInputStream* getInputStream(); virtual IOutputStream* getOutputStream(); diff --git a/net/IDataSocket.h b/net/IDataSocket.h new file mode 100644 index 00000000..faf645b8 --- /dev/null +++ b/net/IDataSocket.h @@ -0,0 +1,28 @@ +#ifndef IDATASOCKET_H +#define IDATASOCKET_H + +#include "ISocket.h" + +class IInputStream; +class IOutputStream; + +class IDataSocket : public ISocket { +public: + // manipulators + + // connect the socket + virtual void connect(const CNetworkAddress&) = 0; + + // get the input and output streams for the socket. closing + // these streams closes the appropriate half of the socket. + virtual IInputStream* getInputStream() = 0; + virtual IOutputStream* getOutputStream() = 0; + + // accessors + + // ISocket overrides + virtual void bind(const CNetworkAddress&) = 0; + virtual void close() = 0; +}; + +#endif diff --git a/net/IListenSocket.h b/net/IListenSocket.h index dd454976..51d73cfc 100644 --- a/net/IListenSocket.h +++ b/net/IListenSocket.h @@ -1,25 +1,22 @@ #ifndef ILISTENSOCKET_H #define ILISTENSOCKET_H -#include "IInterface.h" +#include "ISocket.h" -class CNetworkAddress; -class ISocket; +class IDataSocket; -class IListenSocket : public IInterface { +class IListenSocket : public ISocket { public: // manipulators - // bind the socket to a particular address - virtual void bind(const CNetworkAddress&) = 0; - // wait for a connection - virtual ISocket* accept() = 0; + virtual IDataSocket* accept() = 0; - // close the socket - virtual void close() = 0; - // accessors + + // ISocket overrides + virtual void bind(const CNetworkAddress&) = 0; + virtual void close() = 0; }; #endif diff --git a/net/ISocket.h b/net/ISocket.h index 6c853b59..8ca8fbf0 100644 --- a/net/ISocket.h +++ b/net/ISocket.h @@ -4,8 +4,6 @@ #include "IInterface.h" class CNetworkAddress; -class IInputStream; -class IOutputStream; class ISocket : public IInterface { public: @@ -14,18 +12,10 @@ public: // bind the socket to a particular address virtual void bind(const CNetworkAddress&) = 0; - // connect the socket - virtual void connect(const CNetworkAddress&) = 0; - // close the socket. this will flush the output stream if it // hasn't been closed yet. virtual void close() = 0; - // get the input and output streams for the socket. closing - // these streams closes the appropriate half of the socket. - virtual IInputStream* getInputStream() = 0; - virtual IOutputStream* getOutputStream() = 0; - // accessors }; diff --git a/server/CHTTPServer.cpp b/server/CHTTPServer.cpp index 36f46967..892cee3e 100644 --- a/server/CHTTPServer.cpp +++ b/server/CHTTPServer.cpp @@ -3,7 +3,7 @@ #include "CHTTPProtocol.h" #include "CServer.h" #include "XHTTP.h" -#include "ISocket.h" +#include "IDataSocket.h" #include "XThread.h" #include "CLog.h" #include "stdset.h" @@ -32,7 +32,7 @@ CHTTPServer::~CHTTPServer() void CHTTPServer::processRequest( - ISocket* socket) + IDataSocket* socket) { assert(socket != NULL); diff --git a/server/CHTTPServer.h b/server/CHTTPServer.h index 9d9ac35e..f8c9b241 100644 --- a/server/CHTTPServer.h +++ b/server/CHTTPServer.h @@ -9,7 +9,7 @@ class CServer; class CConfig; class CHTTPRequest; class CHTTPReply; -class ISocket; +class IDataSocket; class CHTTPServer { public: @@ -19,7 +19,7 @@ public: // manipulators // synchronously process an HTTP request on the given socket - void processRequest(ISocket*); + void processRequest(IDataSocket*); // accessors diff --git a/server/CServer.cpp b/server/CServer.cpp index fb30944e..79e59f55 100644 --- a/server/CServer.cpp +++ b/server/CServer.cpp @@ -9,8 +9,8 @@ #include "XScreen.h" #include "XSynergy.h" #include "CNetworkAddress.h" +#include "IDataSocket.h" #include "IListenSocket.h" -#include "ISocket.h" #include "ISocketFactory.h" #include "XSocket.h" #include "CLock.h" @@ -1088,7 +1088,7 @@ CServer::acceptClients(void*) for (;;) { // accept connection CThread::testCancel(); - ISocket* socket = listen->accept(); + IDataSocket* socket = listen->accept(); log((CLOG_NOTE "accepted client connection")); CThread::testCancel(); @@ -1111,7 +1111,7 @@ CServer::handshakeClient( // get the socket pointer from the argument assert(vsocket != NULL); - std::auto_ptr socket(reinterpret_cast(vsocket)); + std::auto_ptr socket(reinterpret_cast(vsocket)); // add this thread to the list of threads to cancel. remove from // list in d'tor. @@ -1289,7 +1289,7 @@ CServer::acceptHTTPClients(void*) // accept connection CThread::testCancel(); - ISocket* socket = listen->accept(); + IDataSocket* socket = listen->accept(); log((CLOG_NOTE "accepted HTTP connection")); CThread::testCancel(); @@ -1313,7 +1313,7 @@ CServer::processHTTPRequest( // list in d'tor. CCleanupNote cleanupNote(this); - ISocket* socket = reinterpret_cast(vsocket); + IDataSocket* socket = reinterpret_cast(vsocket); try { // process the request and force delivery m_httpServer->processRequest(socket); diff --git a/synergy/CTCPSocketFactory.cpp b/synergy/CTCPSocketFactory.cpp index 5c5da342..4067e6bb 100644 --- a/synergy/CTCPSocketFactory.cpp +++ b/synergy/CTCPSocketFactory.cpp @@ -16,7 +16,7 @@ CTCPSocketFactory::~CTCPSocketFactory() // do nothing } -ISocket* +IDataSocket* CTCPSocketFactory::create() const { return new CTCPSocket; diff --git a/synergy/CTCPSocketFactory.h b/synergy/CTCPSocketFactory.h index a320f9ae..2b900bb2 100644 --- a/synergy/CTCPSocketFactory.h +++ b/synergy/CTCPSocketFactory.h @@ -13,7 +13,7 @@ public: // accessors // ISocketFactory overrides - virtual ISocket* create() const; + virtual IDataSocket* create() const; virtual IListenSocket* createListen() const; }; diff --git a/synergy/ISocketFactory.h b/synergy/ISocketFactory.h index 9d0f1104..7b7ff8cd 100644 --- a/synergy/ISocketFactory.h +++ b/synergy/ISocketFactory.h @@ -3,7 +3,7 @@ #include "IInterface.h" -class ISocket; +class IDataSocket; class IListenSocket; class ISocketFactory : public IInterface { @@ -13,7 +13,7 @@ public: // accessors // create sockets - virtual ISocket* create() const = 0; + virtual IDataSocket* create() const = 0; virtual IListenSocket* createListen() const = 0; };