Skip to content

Commit 53cc41f

Browse files
authored
Merge pull request #34 from an-tao/dev
Add the byte statistics method to the TcpConnection class
2 parents 85e2671 + 5bb0079 commit 53cc41f

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

trantor/net/TcpConnection.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ class TcpConnection
6969
// the kickoffIdleConnections method in the TcpServer class.
7070
virtual void keepAlive() = 0;
7171
virtual bool isKeepAlive() = 0;
72+
73+
virtual size_t bytesSent() const = 0;
74+
virtual size_t bytesReceived() const = 0;
7275
};
7376

7477
} // namespace trantor

trantor/net/inner/TcpConnectionImpl.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,13 @@ void TcpConnectionImpl::readCallback()
7070
LOG_SYSERR << "read socket error";
7171
}
7272
extendLife();
73-
if (n > 0 && _recvMsgCallback)
73+
if (n > 0)
7474
{
75-
_recvMsgCallback(shared_from_this(), &_readBuffer);
75+
_bytesReceived += n;
76+
if (_recvMsgCallback)
77+
{
78+
_recvMsgCallback(shared_from_this(), &_readBuffer);
79+
}
7680
}
7781
}
7882
void TcpConnectionImpl::extendLife()
@@ -740,5 +744,6 @@ void TcpConnectionImpl::sendFileInLoop(const BufferNodePtr &filePtr)
740744

741745
ssize_t TcpConnectionImpl::writeInLoop(const char *buffer, size_t length)
742746
{
747+
_bytesSent += length;
743748
return write(_socketPtr->fd(), buffer, length);
744749
}

trantor/net/inner/TcpConnectionImpl.h

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,24 +129,32 @@ class TcpConnectionImpl : public TcpConnection,
129129

130130
virtual void setContext(const any &context) override
131131
{
132-
context_ = context;
132+
_context = context;
133133
}
134134
virtual void setContext(any &&context) override
135135
{
136-
context_ = std::move(context);
136+
_context = std::move(context);
137137
}
138138
virtual const any &getContext() const override
139139
{
140-
return context_;
140+
return _context;
141141
}
142142

143143
virtual any *getMutableContext() override
144144
{
145-
return &context_;
145+
return &_context;
146+
}
147+
virtual size_t bytesSent() const override
148+
{
149+
return _bytesSent;
150+
}
151+
virtual size_t bytesReceived() const override
152+
{
153+
return _bytesReceived;
146154
}
147155

148156
protected:
149-
any context_;
157+
any _context;
150158
/// Internal use only.
151159

152160
std::weak_ptr<KickoffEntry> _kickoffEntry;
@@ -238,6 +246,10 @@ class TcpConnectionImpl : public TcpConnection,
238246

239247
uint64_t _sendNum = 0;
240248
std::mutex _sendNumMutex;
249+
250+
size_t _bytesSent = 0;
251+
size_t _bytesReceived = 0;
252+
241253
};
242254

243255
typedef std::shared_ptr<TcpConnectionImpl> TcpConnectionImplPtr;

0 commit comments

Comments
 (0)