Skip to content

Commit ec56d47

Browse files
authored
Add the send(const void *, size_t) method to the TcpConnection class (#78)
1 parent 312a91e commit ec56d47

File tree

3 files changed

+63
-10
lines changed

3 files changed

+63
-10
lines changed

trantor/net/TcpConnection.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ class TcpConnection
3232
public:
3333
TcpConnection() = default;
3434
virtual ~TcpConnection(){};
35-
virtual void send(const char *msg, uint64_t len) = 0;
35+
virtual void send(const char *msg, size_t len) = 0;
36+
virtual void send(const void *msg, size_t len) = 0;
3637
virtual void send(const std::string &msg) = 0;
3738
virtual void send(std::string &&msg) = 0;
3839
virtual void send(const MsgBuffer &buffer) = 0;

trantor/net/inner/TcpConnectionImpl.cc

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -665,8 +665,11 @@ void TcpConnectionImpl::forceClose()
665665
}
666666
});
667667
}
668-
668+
#ifndef _WIN32
669+
void TcpConnectionImpl::sendInLoop(const void *buffer, size_t length)
670+
#else
669671
void TcpConnectionImpl::sendInLoop(const char *buffer, size_t length)
672+
#endif
670673
{
671674
loop_->assertInLoopThread();
672675
if (status_ != ConnStatus::Connected)
@@ -720,8 +723,8 @@ void TcpConnectionImpl::sendInLoop(const char *buffer, size_t length)
720723
node->msgBuffer_ = std::shared_ptr<MsgBuffer>(new MsgBuffer);
721724
writeBufferList_.push_back(std::move(node));
722725
}
723-
writeBufferList_.back()->msgBuffer_->append(buffer + sendLen,
724-
remainLen);
726+
writeBufferList_.back()->msgBuffer_->append(
727+
static_cast<const char *>(buffer) + sendLen, remainLen);
725728
if (!ioChannelPtr_->isWriting())
726729
ioChannelPtr_->enableWriting();
727730
if (highWaterMarkCallback_ &&
@@ -800,7 +803,7 @@ void TcpConnectionImpl::send(const std::shared_ptr<MsgBuffer> &msgPtr)
800803
});
801804
}
802805
}
803-
void TcpConnectionImpl::send(const char *msg, uint64_t len)
806+
void TcpConnectionImpl::send(const char *msg, size_t len)
804807
{
805808
if (loop_->isInLoopThread())
806809
{
@@ -834,6 +837,47 @@ void TcpConnectionImpl::send(const char *msg, uint64_t len)
834837
});
835838
}
836839
}
840+
void TcpConnectionImpl::send(const void *msg, size_t len)
841+
{
842+
if (loop_->isInLoopThread())
843+
{
844+
std::lock_guard<std::mutex> guard(sendNumMutex_);
845+
if (sendNum_ == 0)
846+
{
847+
#ifndef _WIN32
848+
sendInLoop(msg, len);
849+
#else
850+
sendInLoop(static_cast<const char *>(msg), len);
851+
#endif
852+
}
853+
else
854+
{
855+
++sendNum_;
856+
auto buffer =
857+
std::make_shared<std::string>(static_cast<const char *>(msg),
858+
len);
859+
auto thisPtr = shared_from_this();
860+
loop_->queueInLoop([thisPtr, buffer]() {
861+
thisPtr->sendInLoop(buffer->data(), buffer->length());
862+
std::lock_guard<std::mutex> guard1(thisPtr->sendNumMutex_);
863+
--thisPtr->sendNum_;
864+
});
865+
}
866+
}
867+
else
868+
{
869+
auto buffer =
870+
std::make_shared<std::string>(static_cast<const char *>(msg), len);
871+
auto thisPtr = shared_from_this();
872+
std::lock_guard<std::mutex> guard(sendNumMutex_);
873+
++sendNum_;
874+
loop_->queueInLoop([thisPtr, buffer]() {
875+
thisPtr->sendInLoop(buffer->data(), buffer->length());
876+
std::lock_guard<std::mutex> guard1(thisPtr->sendNumMutex_);
877+
--thisPtr->sendNum_;
878+
});
879+
}
880+
}
837881
void TcpConnectionImpl::send(const std::string &msg)
838882
{
839883
if (loop_->isInLoopThread())
@@ -1215,8 +1259,11 @@ void TcpConnectionImpl::sendFileInLoop(const BufferNodePtr &filePtr)
12151259
ioChannelPtr_->enableWriting();
12161260
}
12171261
}
1218-
1262+
#ifndef _WIN32
1263+
ssize_t TcpConnectionImpl::writeInLoop(const void *buffer, size_t length)
1264+
#else
12191265
ssize_t TcpConnectionImpl::writeInLoop(const char *buffer, size_t length)
1266+
#endif
12201267
{
12211268
#ifdef USE_OPENSSL
12221269
if (!isEncrypted_)
@@ -1255,7 +1302,7 @@ ssize_t TcpConnectionImpl::writeInLoop(const char *buffer, size_t length)
12551302
len = sslEncryptionPtr_->sendBufferPtr_->size();
12561303
}
12571304
memcpy(sslEncryptionPtr_->sendBufferPtr_->data(),
1258-
buffer + sendTotalLen,
1305+
static_cast<const char *>(buffer) + sendTotalLen,
12591306
len);
12601307
ERR_clear_error();
12611308
auto sendLen = SSL_write(sslEncryptionPtr_->sslPtr_->get(),

trantor/net/inner/TcpConnectionImpl.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ class TcpConnectionImpl : public TcpConnection,
9595
bool isServer = true);
9696
#endif
9797
virtual ~TcpConnectionImpl();
98-
virtual void send(const char *msg, uint64_t len) override;
98+
virtual void send(const char *msg, size_t len) override;
99+
virtual void send(const void *msg, size_t len) override;
99100
virtual void send(const std::string &msg) override;
100101
virtual void send(std::string &&msg) override;
101102
virtual void send(const MsgBuffer &buffer) override;
@@ -268,11 +269,15 @@ class TcpConnectionImpl : public TcpConnection,
268269
void handleClose();
269270
void handleError();
270271
// virtual void sendInLoop(const std::string &msg);
271-
void sendInLoop(const char *buffer, size_t length);
272272

273273
void sendFileInLoop(const BufferNodePtr &file);
274-
274+
#ifndef _WIN32
275+
void sendInLoop(const void *buffer, size_t length);
276+
ssize_t writeInLoop(const void *buffer, size_t length);
277+
#else
278+
void sendInLoop(const char *buffer, size_t length);
275279
ssize_t writeInLoop(const char *buffer, size_t length);
280+
#endif
276281
size_t highWaterMarkLen_;
277282
std::string name_;
278283

0 commit comments

Comments
 (0)