Skip to content

Commit

Permalink
修复线程安全问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Jzan authored and Jzan committed Mar 6, 2017
1 parent 53aae07 commit a549490
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
48 changes: 40 additions & 8 deletions src/Network/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ void Socket::connect(const string &url, uint16_t port, onErrCB &&connectCB,
connectCB(err);
return false;
}));
lock_guard<recursive_mutex> lck(_mtx_sockFd);
_sockFd = sockFD;
}

Expand Down Expand Up @@ -234,8 +235,11 @@ void Socket::onError(const SockFD::Ptr &pSock) {
emitErr(getSockErr(pSock));
}
bool Socket::emitErr(const SockException& err) {
if(!_sockFd){
return false;
{
lock_guard<recursive_mutex> lck(_mtx_sockFd);
if (!_sockFd) {
return false;
}
}
weak_ptr<Socket> weakSelf = this->shared_from_this();
EventPoller::Instance().async([weakSelf,err]() {
Expand All @@ -261,7 +265,11 @@ int Socket::send(const char *buf, int size,int flags) {
return 0;
}
}
auto sock = _sockFd;
SockFD::Ptr sock;
{
lock_guard<recursive_mutex> lck(_mtx_sockFd);
sock = _sockFd;
}
if (!sock) {
return -1;
}
Expand Down Expand Up @@ -365,6 +373,7 @@ int Socket::onWriteTCP(const SockFD::Ptr &pSock,bool bMainThread,int flags) {
}
void Socket::closeSock() {
_conTimer.reset();
lock_guard<recursive_mutex> lck(_mtx_sockFd);
_sockFd.reset();
}

Expand All @@ -389,6 +398,7 @@ bool Socket::listen(const uint16_t port, const char* localIp, int backLog) {
WarnL << "开始Poll监听失败";
return false;
}
lock_guard<recursive_mutex> lck(_mtx_sockFd);
_sockFd = pSock;
return true;
}
Expand All @@ -403,6 +413,7 @@ bool Socket::bindUdpSock(const uint16_t port, const char* localIp) {
WarnL << "开始Poll监听失败";
return false;
}
lock_guard<recursive_mutex> lck(_mtx_sockFd);
_sockFd = pSock;
return true;
}
Expand Down Expand Up @@ -454,20 +465,29 @@ bool Socket::setPeerSock(int sock, struct sockaddr *addr) {
return false;
}
_peerAddr = *addr;
lock_guard<recursive_mutex> lck(_mtx_sockFd);
_sockFd = pSock;
return true;
}

string Socket::get_local_ip() {
auto sock = _sockFd;
SockFD::Ptr sock;
{
lock_guard<recursive_mutex> lck(_mtx_sockFd);
sock = _sockFd;
}
if (!sock) {
return "";
}
return SockUtil::get_local_ip(sock->rawFd());
}

uint16_t Socket::get_local_port() {
auto sock = _sockFd;
SockFD::Ptr sock;
{
lock_guard<recursive_mutex> lck(_mtx_sockFd);
sock = _sockFd;
}
if (!sock) {
return 0;
}
Expand All @@ -476,15 +496,23 @@ uint16_t Socket::get_local_port() {
}

string Socket::get_peer_ip() {
auto sock = _sockFd;
SockFD::Ptr sock;
{
lock_guard<recursive_mutex> lck(_mtx_sockFd);
sock = _sockFd;
}
if (!sock) {
return "";
}
return SockUtil::get_peer_ip(sock->rawFd());
}

uint16_t Socket::get_peer_port() {
auto sock = _sockFd;
SockFD::Ptr sock;
{
lock_guard<recursive_mutex> lck(_mtx_sockFd);
sock = _sockFd;
}
if (!sock) {
return 0;
}
Expand All @@ -504,7 +532,11 @@ int Socket::sendTo(const char* buf, int size, struct sockaddr* peerAddr,int flag
return 0;
}
}
auto sock = _sockFd;
SockFD::Ptr sock;
{
lock_guard<recursive_mutex> lck(_mtx_sockFd);
sock = _sockFd;
}
if (!sock) {
return -1;
}
Expand Down
1 change: 1 addition & 0 deletions src/Network/Socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ class Socket: public std::enable_shared_from_this<Socket> {
_iUdpMaxPktSize = iPktSize;
}
private:
recursive_mutex _mtx_sockFd;
SockFD::Ptr _sockFd;
//send buffer
recursive_mutex _mtx_sendBuf;
Expand Down

0 comments on commit a549490

Please sign in to comment.