diff --git a/connection.h b/connection.h index b74b45d..a26eb98 100644 --- a/connection.h +++ b/connection.h @@ -117,12 +117,14 @@ private: bool closeRequested; unsigned long long closeTimeUs; std::condition_variable_any closeAwaitCondition; - + + friend class Protocol; + void open(Socket *socket); + public: Connection(); virtual ~Connection(); - void open(Socket *socket); void close(ErrorCode errorCode = ERR_NONE); ReqId writeReq(const void *data, size_t size, void *userData = nullptr); diff --git a/error.h b/error.h index 38f55f3..3814c2b 100644 --- a/error.h +++ b/error.h @@ -19,6 +19,7 @@ enum : ErrorCode { enum : ErrorCode { ERR_COMMON = ERR_NONE + 1, ERR_NOT_IMPLEMENTED, + ERR_INVALIDD_ARGS, }; diff --git a/handle.h b/handle.h index c5402b7..1017b5f 100644 --- a/handle.h +++ b/handle.h @@ -209,8 +209,8 @@ public: { HandleBase::swap(other); return *this; } inline Type* pointer() const { return (Type*)HandleBase::get(); } - inline Type* operator->() const { return pointer(); } - inline Type& operator*() const { return *pointer(); } + inline Type* operator->() const { assert(pointer()); return pointer(); } + inline Type& operator*() const { assert(pointer()); return *pointer(); } template inline operator Handle&() diff --git a/protocol.cpp b/protocol.cpp index f457467..57beaeb 100644 --- a/protocol.cpp +++ b/protocol.cpp @@ -75,6 +75,12 @@ Protocol::~Protocol() { assert(!first); } +void Protocol::connectionOpen(const Connection::Handle &connection, Socket *socket) { + assert(connection && socket); + connection->open(socket); +} + + void Protocol::closeAll() { Lock lock(mutex); while(first) diff --git a/protocol.h b/protocol.h index 4e33d97..71fb875 100644 --- a/protocol.h +++ b/protocol.h @@ -105,6 +105,9 @@ protected: inline Socket* socketSetUnchanged(Socket *socket) { socketCheck(socket, true); Socket *chNext = socket->chNext; socket->setChanged(false); return chNext; } + + // thread-safe methods + void connectionOpen(const Connection::Handle &connection, Socket *socket); virtual void wakeup(); public: diff --git a/server.h b/server.h index d9c5d27..5f45272 100644 --- a/server.h +++ b/server.h @@ -32,11 +32,13 @@ private: Mutex mutex; Socket *socket; + friend class Protocol; + void open(Socket *socket); + public: Server(); ~Server(); - void open(Socket *socket); void close(ErrorCode errorCode = ERR_NONE); protected: diff --git a/tcpprotocol.cpp b/tcpprotocol.cpp index a18d182..44c2e6a 100644 --- a/tcpprotocol.cpp +++ b/tcpprotocol.cpp @@ -111,6 +111,7 @@ void TcpProtocol::threadRun() { ErrorCode TcpProtocol::connect(const Connection::Handle &connection, const Address &address) { if (!thread) return ERR_PROTOCOL_NOT_INITIALIZED; + if (!connection) return ERR_INVALIDD_ARGS; Address remoteAddres = address; ErrorCode errorCode = resolve(remoteAddres); @@ -125,7 +126,7 @@ ErrorCode TcpProtocol::connect(const Connection::Handle &connection, const Addre } TcpSocket *socket = new TcpSocket(*this, connection, nullptr, remoteAddres, sockId); - connection->open(socket); + connectionOpen(connection, socket); return ERR_NONE; }