summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2025-03-12 17:16:59 +0800
committerlittleblack111 <littleblack11111@gmail.com>2025-03-20 23:44:59 +0800
commit108e9bd4b5628d2ce93862dd88473ed5b1ad36ab (patch)
tree013b6d2803b101a15fbedc580ce5731967c00731
parent73c9cb4dfaa1da463534862813c3ea3a528a4b03 (diff)
session(write): fix newline not being printed before write
rn if you write() while there is a read("text") it would output the new write() in the same line which is not ideal.
-rw-r--r--src/session.cpp4
-rw-r--r--src/session.hpp2
2 files changed, 6 insertions, 0 deletions
diff --git a/src/session.cpp b/src/session.cpp
index b37d896..463e16a 100644
--- a/src/session.cpp
+++ b/src/session.cpp
@@ -79,11 +79,13 @@ CSession::SRecvData CSession::read() {
}
onRecv(recvData);
+ m_isReading = false;
return recvData;
}
CSession::SRecvData CSession::read(const std::string &msg) {
write({}, "{}", msg, false);
+ m_isReading = true;
return read();
}
@@ -131,6 +133,8 @@ bool CSession::isValid() {
template <typename... Args>
bool CSession::write(eFormatType type, std::format_string<Args...> fmt, Args &&...args) {
std::string msg = NFormatter::fmt(type, fmt, std::forward<Args>(args)...);
+ if (m_isReading)
+ msg.insert(0, "\n");
if (send(m_sockfd.get(), msg.c_str(), msg.size(), 0) < 0) {
onErrno(WRITE);
diff --git a/src/session.hpp b/src/session.hpp
index 82eca9c..3e5bbc1 100644
--- a/src/session.hpp
+++ b/src/session.hpp
@@ -45,6 +45,8 @@ class CSession {
char m_ip[INET_ADDRSTRLEN];
int m_port;
+ bool m_isReading = false;
+
void recvLoop();
bool registerSession();