File size: 2,279 Bytes
00df61d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "socket_registry.h"

#include <map>
#include <vector>
#include <algorithm>
#include "threads.h"

namespace {
  MUTEX_T socketRegistryLock;
  std::map<int, std::vector<SOCKET_T> > socketsPerProxyConnection;
}

void InitWebSocketRegistry() {
  CREATE_MUTEX(&socketRegistryLock);
}

void TrackSocketUsedByConnection(int proxyConnection, SOCKET_T usedSocket) {
  if (usedSocket == 0) return;
  if (IsSocketPartOfConnection(proxyConnection, usedSocket))
    return;

  LOCK_MUTEX(&socketRegistryLock);

  socketsPerProxyConnection[proxyConnection].push_back(usedSocket);

  UNLOCK_MUTEX(&socketRegistryLock);
}

void CloseSocketByConnection(int proxyConnection, SOCKET_T usedSocket) {
  if (!IsSocketPartOfConnection(proxyConnection, usedSocket))
    return;

  printf("Closing socket fd %d used by proxy connection %d\n", (int)usedSocket, proxyConnection);

  LOCK_MUTEX(&socketRegistryLock);

  CLOSE_SOCKET(usedSocket);
  std::vector<SOCKET_T> &sockets = socketsPerProxyConnection[proxyConnection];
  sockets.erase(std::remove(sockets.begin(), sockets.end(), usedSocket), sockets.end());

  UNLOCK_MUTEX(&socketRegistryLock);
}

void CloseAllSocketsByConnection(int proxyConnection) {
  LOCK_MUTEX(&socketRegistryLock);

  std::vector<SOCKET_T> &sockets = socketsPerProxyConnection[proxyConnection];
  for (size_t i = 0; i < sockets.size(); ++i) {
    printf("Closing socket fd %d used by proxy connection %d.\n", (int)sockets[i], proxyConnection);
    shutdown(sockets[i], SHUTDOWN_BIDIRECTIONAL);
    CLOSE_SOCKET(sockets[i]);
  }
  socketsPerProxyConnection.erase(proxyConnection);

  UNLOCK_MUTEX(&socketRegistryLock);
}

bool IsSocketPartOfConnection(int proxyConnection, SOCKET_T usedSocket) {
  bool result;
  if (usedSocket == 0) return true; // Allow all proxy connections to access "socket 0" when/if they need to refer to socket that does not exist.

  LOCK_MUTEX(&socketRegistryLock);

  if (socketsPerProxyConnection.find(proxyConnection) == socketsPerProxyConnection.end()) {
    UNLOCK_MUTEX(&socketRegistryLock);
    return false;
  }

  std::vector<SOCKET_T> &sockets = socketsPerProxyConnection[proxyConnection];
  result = std::find(sockets.begin(), sockets.end(), usedSocket) != sockets.end();

  UNLOCK_MUTEX(&socketRegistryLock);

  return result;
}