Skip to content

Commit 312a91e

Browse files
authored
Add the send(const MsgBufferPtr &) method to TcpConnection class (#76)
1 parent 65366dd commit 312a91e

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

trantor/net/TcpConnection.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class TcpConnection
3838
virtual void send(const MsgBuffer &buffer) = 0;
3939
virtual void send(MsgBuffer &&buffer) = 0;
4040
virtual void send(const std::shared_ptr<std::string> &msgPtr) = 0;
41-
41+
virtual void send(const std::shared_ptr<MsgBuffer> &msgPtr) = 0;
4242
virtual void sendFile(const char *fileName,
4343
size_t offset = 0,
4444
size_t length = 0) = 0;

trantor/net/inner/TcpConnectionImpl.cc

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,39 @@ void TcpConnectionImpl::send(const std::shared_ptr<std::string> &msgPtr)
767767
});
768768
}
769769
}
770+
// The order of data sending should be same as the order of calls of send()
771+
void TcpConnectionImpl::send(const std::shared_ptr<MsgBuffer> &msgPtr)
772+
{
773+
if (loop_->isInLoopThread())
774+
{
775+
std::lock_guard<std::mutex> guard(sendNumMutex_);
776+
if (sendNum_ == 0)
777+
{
778+
sendInLoop(msgPtr->peek(), msgPtr->readableBytes());
779+
}
780+
else
781+
{
782+
++sendNum_;
783+
auto thisPtr = shared_from_this();
784+
loop_->queueInLoop([thisPtr, msgPtr]() {
785+
thisPtr->sendInLoop(msgPtr->peek(), msgPtr->readableBytes());
786+
std::lock_guard<std::mutex> guard1(thisPtr->sendNumMutex_);
787+
--thisPtr->sendNum_;
788+
});
789+
}
790+
}
791+
else
792+
{
793+
auto thisPtr = shared_from_this();
794+
std::lock_guard<std::mutex> guard(sendNumMutex_);
795+
++sendNum_;
796+
loop_->queueInLoop([thisPtr, msgPtr]() {
797+
thisPtr->sendInLoop(msgPtr->peek(), msgPtr->readableBytes());
798+
std::lock_guard<std::mutex> guard1(thisPtr->sendNumMutex_);
799+
--thisPtr->sendNum_;
800+
});
801+
}
802+
}
770803
void TcpConnectionImpl::send(const char *msg, uint64_t len)
771804
{
772805
if (loop_->isInLoopThread())

trantor/net/inner/TcpConnectionImpl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ class TcpConnectionImpl : public TcpConnection,
101101
virtual void send(const MsgBuffer &buffer) override;
102102
virtual void send(MsgBuffer &&buffer) override;
103103
virtual void send(const std::shared_ptr<std::string> &msgPtr) override;
104+
virtual void send(const std::shared_ptr<MsgBuffer> &msgPtr) override;
104105
virtual void sendFile(const char *fileName,
105106
size_t offset = 0,
106107
size_t length = 0) override;

0 commit comments

Comments
 (0)