diff --git a/conanfile.py b/conanfile.py index ec70ee6..74f775a 100644 --- a/conanfile.py +++ b/conanfile.py @@ -9,7 +9,7 @@ class TcpcatConan(ConanFile): name = "ydcpp-tcpcat" - version = "1.0.4" + version = "1.0.5" package_type = "library" # Optional metadata diff --git a/include/tcpcat/base/EventHandler.h b/include/tcpcat/base/EventHandler.h index fe0ed0b..1499644 100644 --- a/include/tcpcat/base/EventHandler.h +++ b/include/tcpcat/base/EventHandler.h @@ -62,6 +62,12 @@ class EventHandler /// @param bytes Sent byte count. virtual void OnSent(std::shared_ptr session, const std::vector &buf, size_t bytes) {} + /// @brief Gets called when sent a message to peer. + /// @param session Session handle object. + /// @param buf Contains the sent bytes. + /// @param bytes Sent byte count. + virtual void OnSent(std::shared_ptr session, const unsigned char* buf, size_t bytes) {} + /// @brief Gets called when an error occurs. /// @param session Session handle object. /// @param err Error code object which holds error information. diff --git a/include/tcpcat/base/TcpSession.h b/include/tcpcat/base/TcpSession.h index e9d8351..7458399 100644 --- a/include/tcpcat/base/TcpSession.h +++ b/include/tcpcat/base/TcpSession.h @@ -66,6 +66,13 @@ class TcpSession : public std::enable_shared_from_this /// @return Returns the number of bytes sent. size_t Send(const std::vector &buffer, size_t offset, size_t size); + /// @brief Sends data to the peer. Calls EventHandler->OnSent on success, EventHandler->OnError otherwise. + /// @param buffer Contains bytes to be sent. + /// @param offset Offset value from the beginning of `buffer`. + /// @param size Count of bytes to be sent starting from `offset`. + /// @return Returns the number of bytes sent. + size_t Send(const unsigned char* buffer, size_t offset, size_t size); + /// @brief Sends data to the peer in non-blocking operation. Calls EventHandler->OnSent on success, EventHandler->OnError otherwise. /// @param buffer Contains bytes to be sent. /// @param offset Offset value from the beginning of `buffer`. diff --git a/src/TcpSession.cpp b/src/TcpSession.cpp index f7fec4f..dc22791 100644 --- a/src/TcpSession.cpp +++ b/src/TcpSession.cpp @@ -95,6 +95,17 @@ size_t TcpSession::Send(const std::vector &buffer, size_t offset, return sentBytes; } +size_t TcpSession::Send(const unsigned char* buffer, size_t offset, size_t size) +{ + auto sharedThis = shared_from_this(); + + asio::error_code err; + const auto sentBytes = asio::write(*socket_, asio::buffer(buffer, offset + size), err); + sentBytes > 0 ? eventHandler_->OnSent(sharedThis, buffer, sentBytes) : eventHandler_->OnError(sharedThis, err); + + return sentBytes; +} + void TcpSession::SendAsync(const std::vector &buffer, size_t offset, size_t size) { auto sharedThis = shared_from_this();