mirror of
https://github.com/hrydgard/ppsspp.git
synced 2026-11-28 00:33:34 +01:00
Merge pull request #13428 from ANR2ME/adhoc_socket
Adhoc Socket - Prevent Clients from freezing trying to re-connect to exited Host forever
This commit is contained in:
4 files changed
+21
-23
No files matched your search
@@ -77,7 +77,6 @@ SceNetAdhocctlAdhocId product_code;
|
||||
std::thread friendFinderThread;
|
||||
std::recursive_mutex peerlock;
|
||||
AdhocSocket* adhocSockets[MAX_SOCKET];
|
||||
std::map<int, int> ptpConnectCount;
|
||||
std::vector<std::string> chatLog;
|
||||
std::string name = "";
|
||||
std::string incoming = "";
|
||||
@@ -1322,12 +1321,14 @@ int friendFinder(){
|
||||
closesocket(metasocket);
|
||||
metasocket = (int)INVALID_SOCKET;
|
||||
host->NotifyUserMessage(std::string(n->T("Disconnected from AdhocServer")) + " (" + std::string(n->T("Error")) + ": " + std::to_string(error) + ")", 2.0, 0x0000ff);
|
||||
// Mark all friends as timedout since we won't be able to detects disconnected friends anymore without being connected to Adhoc Server
|
||||
timeoutFriendsRecursive(friends);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Update Ping Time
|
||||
lastping = now;
|
||||
DEBUG_LOG(SCENET, "FriendFinder: Sending OPCODE_PING (%llu)", now);
|
||||
DEBUG_LOG(SCENET, "FriendFinder: Sending OPCODE_PING (%llu)", static_cast<unsigned long long>(now));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+5
-3
@@ -328,6 +328,9 @@ typedef struct SceNetAdhocPtpStat {
|
||||
typedef struct AdhocSocket {
|
||||
s32_le type;
|
||||
s32_le flags; // Socket Alert Flags
|
||||
u32 send_timeout;
|
||||
u32 recv_timeout;
|
||||
s32 connectCount;
|
||||
union {
|
||||
SceNetAdhocPdpStat pdp;
|
||||
SceNetAdhocPtpStat ptp;
|
||||
@@ -840,7 +843,6 @@ extern SceNetAdhocctlAdhocId product_code;
|
||||
extern std::thread friendFinderThread;
|
||||
extern std::recursive_mutex peerlock;
|
||||
extern AdhocSocket* adhocSockets[MAX_SOCKET];
|
||||
extern std::map<int, int> ptpConnectCount;
|
||||
|
||||
union SockAddrIN4 {
|
||||
sockaddr addr;
|
||||
@@ -987,9 +989,9 @@ void deleteFriendByIP(uint32_t ip);
|
||||
* Recursive Memory Freeing-Helper for Friend-Structures
|
||||
* @param node Current Node in List
|
||||
*/
|
||||
void freeFriendsRecursive(SceNetAdhocctlPeerInfo * node, int32_t* count);
|
||||
void freeFriendsRecursive(SceNetAdhocctlPeerInfo * node, int32_t* count = nullptr);
|
||||
|
||||
void timeoutFriendsRecursive(SceNetAdhocctlPeerInfo * node, int32_t* count);
|
||||
void timeoutFriendsRecursive(SceNetAdhocctlPeerInfo * node, int32_t* count = nullptr);
|
||||
|
||||
/**
|
||||
* Friend Finder Thread (Receives Peer Information)
|
||||
|
||||
@@ -590,7 +590,6 @@ static int sceNetInit(u32 poolSize, u32 calloutPri, u32 calloutStack, u32 netini
|
||||
|
||||
// Clear Socket Translator Memory
|
||||
memset(&adhocSockets, 0, sizeof(adhocSockets));
|
||||
ptpConnectCount.clear();
|
||||
|
||||
return hleLogSuccessI(SCENET, 0);
|
||||
}
|
||||
|
||||
+13
-17
@@ -393,7 +393,7 @@ int DoBlockingPtpRecv(int uid, AdhocSocketRequest& req, s64& result) {
|
||||
int ret = recv(uid, (char*)req.buffer, *req.length, MSG_NOSIGNAL);
|
||||
int sockerr = errno;
|
||||
|
||||
// Received Data
|
||||
// Received Data. POSIX: May received 0 bytes when the remote peer already closed the connection.
|
||||
if (ret > 0) {
|
||||
DEBUG_LOG(SCENET, "sceNetAdhocPtpRecv[%i:%u]: Received %u bytes from %s:%u\n", req.id, ptpsocket.lport, ret, mac2str(&ptpsocket.paddr).c_str(), ptpsocket.pport);
|
||||
// Save Length
|
||||
@@ -1413,7 +1413,7 @@ static int sceNetAdhocPdpRecv(int id, void *addr, void * port, void *buf, void *
|
||||
received = recvfrom(pdpsocket.id, (char*)buf, *len, MSG_NOSIGNAL, (sockaddr*)&sin, &sinlen);
|
||||
error = errno;
|
||||
|
||||
if (received == SOCKET_ERROR) {
|
||||
if (received == SOCKET_ERROR && (error == EAGAIN || error == EWOULDBLOCK)) {
|
||||
if (flag == 0) {
|
||||
// Simulate blocking behaviour with non-blocking socket
|
||||
u64 threadSocketId = ((u64)__KernelGetCurThread()) << 32 | pdpsocket.id;
|
||||
@@ -1553,10 +1553,8 @@ int PollAdhocSocket(SceNetAdhocPollSd* sds, int count, int timeout) {
|
||||
if (affectedsockets > 0) {
|
||||
affectedsockets = 0;
|
||||
for (int i = 0; i < count; i++) {
|
||||
s32_le* fd_flags;
|
||||
if (sds[i].id > 0 && sds[i].id <= MAX_SOCKET && adhocSockets[sds[i].id - 1] != NULL) {
|
||||
auto sock = adhocSockets[sds[i].id - 1];
|
||||
fd_flags = &sock->flags;
|
||||
if (sock->type == SOCK_PTP) {
|
||||
fd = sock->data.ptp.id;
|
||||
}
|
||||
@@ -1572,10 +1570,10 @@ int PollAdhocSocket(SceNetAdhocPollSd* sds, int count, int timeout) {
|
||||
sds[i].revents |= ADHOC_EV_ALERT; // Does Alert can be raised on revents regardless of events bitmask?
|
||||
if (sds[i].revents) affectedsockets++;
|
||||
|
||||
if (*fd_flags & ADHOC_F_ALERTPOLL) {
|
||||
if (sock->flags & ADHOC_F_ALERTPOLL) {
|
||||
affectedsockets = ERROR_NET_ADHOC_SOCKET_ALERTED;
|
||||
// FIXME: Should we clear the flag after alert signaled?
|
||||
*fd_flags &= ~ADHOC_F_ALERTPOLL;
|
||||
sock->flags &= ~ADHOC_F_ALERTPOLL;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -2670,6 +2668,7 @@ static int sceNetAdhocPtpOpen(const char *srcmac, int sport, const char *dstmac,
|
||||
|
||||
// Socket Type
|
||||
internal->type = SOCK_PTP;
|
||||
internal->send_timeout = rexmt_int;
|
||||
|
||||
// Copy Infrastructure Socket ID
|
||||
internal->data.ptp.id = tcpsocket;
|
||||
@@ -2685,7 +2684,6 @@ static int sceNetAdhocPtpOpen(const char *srcmac, int sport, const char *dstmac,
|
||||
|
||||
// Link PTP Socket
|
||||
adhocSockets[i] = internal;
|
||||
ptpConnectCount[i] = 0;
|
||||
|
||||
// Add Port Forward to Router. We may not even need to forward this local port, since PtpOpen usually have port 0 (any port) as source port and followed by PtpConnect (which mean acting as Client), right?
|
||||
//sceNetPortOpen("TCP", sport);
|
||||
@@ -2798,7 +2796,6 @@ int AcceptPtpSocket(int ptpId, int newsocket, sockaddr_in& peeraddr, SceNetEther
|
||||
|
||||
// Link PTP Socket
|
||||
adhocSockets[i] = internal;
|
||||
ptpConnectCount[i] = 0;
|
||||
|
||||
// Add Port Forward to Router. Or may be doesn't need to be forwarded since local port already accessible from outside if others were able to connect & get accepted at this point, right?
|
||||
//sceNetPortOpen("TCP", internal->lport);
|
||||
@@ -2872,7 +2869,7 @@ static int sceNetAdhocPtpAccept(int id, u32 peerMacAddrPtr, u32 peerPortPtr, int
|
||||
int newsocket = accept(ptpsocket.id, (sockaddr*)&peeraddr, &peeraddrlen);
|
||||
int error = errno;
|
||||
|
||||
if (newsocket == SOCKET_ERROR) {
|
||||
if (newsocket == SOCKET_ERROR && (error == EAGAIN || error == EWOULDBLOCK)) {
|
||||
if (flag == 0) {
|
||||
// Simulate blocking behaviour with non-blocking socket
|
||||
u64 threadSocketId = ((u64)__KernelGetCurThread()) << 32 | ptpsocket.id;
|
||||
@@ -2974,7 +2971,7 @@ static int sceNetAdhocPtpConnect(int id, int timeout, int flag) {
|
||||
|
||||
// Instant Connection (Lucky!)
|
||||
if (connectresult != SOCKET_ERROR || errorcode == EISCONN) {
|
||||
ptpConnectCount[id - 1]++;
|
||||
socket->connectCount++;
|
||||
// Set Connected State
|
||||
ptpsocket.state = ADHOC_PTP_STATE_ESTABLISHED;
|
||||
|
||||
@@ -2985,9 +2982,9 @@ static int sceNetAdhocPtpConnect(int id, int timeout, int flag) {
|
||||
|
||||
// Connection in Progress
|
||||
else if (connectresult == SOCKET_ERROR && connectInProgress(errorcode)) {
|
||||
ptpConnectCount[id - 1]++;
|
||||
socket->connectCount++;
|
||||
// Nonblocking Mode. First attempt need to be blocking for GvG Next Plus to work, even though it used non-blocking flag but only try to connect once per socket, which mean treating it just like blocking socket instead of non-blocking :(
|
||||
if (flag && ptpConnectCount[id - 1] > 1) {
|
||||
if (flag && socket->connectCount > 1) {
|
||||
//if (errorcode == EALREADY) return ERROR_NET_ADHOC_BUSY;
|
||||
return ERROR_NET_ADHOC_WOULD_BLOCK;
|
||||
}
|
||||
@@ -2995,7 +2992,7 @@ static int sceNetAdhocPtpConnect(int id, int timeout, int flag) {
|
||||
else {
|
||||
// Simulate blocking behaviour with non-blocking socket
|
||||
u64 threadSocketId = ((u64)__KernelGetCurThread()) << 32 | ptpsocket.id;
|
||||
return WaitBlockingAdhocSocket(threadSocketId, PTP_CONNECT, id, nullptr, nullptr, timeout, nullptr, nullptr, "ptp connect");
|
||||
return WaitBlockingAdhocSocket(threadSocketId, PTP_CONNECT, id, nullptr, nullptr, (flag)? socket->send_timeout: timeout, nullptr, nullptr, "ptp connect");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3042,7 +3039,6 @@ int NetAdhocPtp_Close(int id, int unknown) {
|
||||
|
||||
// Free Reference
|
||||
adhocSockets[id - 1] = NULL;
|
||||
ptpConnectCount.erase(id - 1);
|
||||
|
||||
// Success
|
||||
return 0;
|
||||
@@ -3169,6 +3165,7 @@ static int sceNetAdhocPtpListen(const char *srcmac, int sport, int bufsize, int
|
||||
|
||||
// Socket Type
|
||||
internal->type = SOCK_PTP;
|
||||
internal->recv_timeout = rexmt_int;
|
||||
|
||||
// Copy Infrastructure Socket ID
|
||||
internal->data.ptp.id = tcpsocket;
|
||||
@@ -3185,7 +3182,6 @@ static int sceNetAdhocPtpListen(const char *srcmac, int sport, int bufsize, int
|
||||
|
||||
// Link PTP Socket
|
||||
adhocSockets[i] = internal;
|
||||
ptpConnectCount[i] = 0;
|
||||
|
||||
// Add Port Forward to Router
|
||||
//sceNetPortOpen("TCP", sport);
|
||||
@@ -3367,11 +3363,11 @@ static int sceNetAdhocPtpRecv(int id, u32 dataAddr, u32 dataSizeAddr, int timeou
|
||||
int received = 0;
|
||||
int error = 0;
|
||||
|
||||
// Receive Data
|
||||
// Receive Data. POSIX: May received 0 bytes when the remote peer already closed the connection.
|
||||
received = recv(ptpsocket.id, (char*)buf, *len, MSG_NOSIGNAL);
|
||||
error = errno;
|
||||
|
||||
if (received == SOCKET_ERROR) {
|
||||
if (received == SOCKET_ERROR && (error == EAGAIN || error == EWOULDBLOCK)) {
|
||||
if (flag == 0) {
|
||||
// Simulate blocking behaviour with non-blocking socket
|
||||
u64 threadSocketId = ((u64)__KernelGetCurThread()) << 32 | ptpsocket.id;
|
||||
|
||||
Reference in new issue
Block a user