Skip to content

Commit

Permalink
1. async setDataCallback and setDisConnectCallback, enable thread safe.
Browse files Browse the repository at this point in the history
2. multi enter callback.
  • Loading branch information
IronsDu committed Oct 2, 2018
1 parent 56e0c48 commit d03e9ac
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
10 changes: 8 additions & 2 deletions src/brynet/net/DataSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -707,12 +707,18 @@ void DataSocket::removeCheckWrite()

void DataSocket::setDataCallback(DATA_CALLBACK cb)
{
mDataCallback = std::move(cb);
auto sharedThis = shared_from_this();
mEventLoop->pushAsyncProc([sharedThis, cb]() {
sharedThis->mDataCallback = std::move(cb);
});
}

void DataSocket::setDisConnectCallback(DISCONNECT_CALLBACK cb)
{
mDisConnectCallback = std::move(cb);
auto sharedThis = shared_from_this();
mEventLoop->pushAsyncProc([sharedThis, cb]() {
sharedThis->mDisConnectCallback = std::move(cb);
});
}

const static size_t GROW_BUFFER_SIZE = 1024;
Expand Down
1 change: 0 additions & 1 deletion src/brynet/net/DataSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ namespace brynet
void send(const PACKET_PTR&, const PACKED_SENDED_CALLBACK& callback = nullptr);
void sendInLoop(const PACKET_PTR&, const PACKED_SENDED_CALLBACK& callback = nullptr);

//TODO::线程安全问题
void setDataCallback(DATA_CALLBACK cb);
void setDisConnectCallback(DISCONNECT_CALLBACK cb);

Expand Down
13 changes: 10 additions & 3 deletions src/brynet/net/TCPService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ struct brynet::net::TcpService::AddSocketOption::Options
maxRecvBufferSize = 0;
}

TcpService::ENTER_CALLBACK enterCallback;
std::vector<TcpService::ENTER_CALLBACK> enterCallback;

SSLHelper::PTR sslHelper;
bool useSSL;
Expand Down Expand Up @@ -178,10 +178,17 @@ bool TcpService::_addDataSocket(TcpSocket::PTR socket,
return false;
}

auto wrapperEnterCallback = [options](const DataSocket::PTR& dataSocket) {
for (const auto& callback : options.enterCallback)
{
callback(dataSocket);
}
};

const auto isServerSide = socket->isServerSide();
auto dataSocket = DataSocket::Create(std::move(socket),
options.maxRecvBufferSize,
options.enterCallback,
wrapperEnterCallback,
eventLoop);
(void)isServerSide;
#ifdef USE_OPENSSL
Expand Down Expand Up @@ -259,7 +266,7 @@ std::shared_ptr<std::thread>& IOLoopData::getIOThread()
TcpService::AddSocketOption::AddSocketOptionFunc TcpService::AddSocketOption::WithEnterCallback(TcpService::ENTER_CALLBACK callback)
{
return [=](TcpService::AddSocketOption::Options& option) {
option.enterCallback = callback;
option.enterCallback.push_back(callback);
};
}

Expand Down

0 comments on commit d03e9ac

Please sign in to comment.