Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class TcpcatConan(ConanFile):
name = "ydcpp-tcpcat"
version = "1.0.4"
version = "1.0.5"
package_type = "library"

# Optional metadata
Expand Down
6 changes: 6 additions & 0 deletions include/tcpcat/base/EventHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ class EventHandler
/// @param bytes Sent byte count.
virtual void OnSent(std::shared_ptr<tcpcat::TcpSession> session, const std::vector<unsigned char> &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<tcpcat::TcpSession> 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.
Expand Down
7 changes: 7 additions & 0 deletions include/tcpcat/base/TcpSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ class TcpSession : public std::enable_shared_from_this<TcpSession>
/// @return Returns the number of bytes sent.
size_t Send(const std::vector<unsigned char> &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`.
Expand Down
11 changes: 11 additions & 0 deletions src/TcpSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ size_t TcpSession::Send(const std::vector<unsigned char> &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<unsigned char> &buffer, size_t offset, size_t size)
{
auto sharedThis = shared_from_this();
Expand Down