1 | /** |
---|
2 | * @file TcpipServer.cpp |
---|
3 | * @author Tomas Urban |
---|
4 | * @version 0.3 |
---|
5 | * @date 23/07/2009 |
---|
6 | */ |
---|
7 | #include "TcpipServer.h" |
---|
8 | #include <iostream> |
---|
9 | #include <boost/thread/thread.hpp> |
---|
10 | #include <signal.h> |
---|
11 | #include "Logger/Logger.h" |
---|
12 | |
---|
13 | using namespace std; |
---|
14 | |
---|
15 | TcpipServer::TcpipServer(void) : |
---|
16 | m_hListeningSocket (SOCKET_CALL_ERROR) |
---|
17 | { |
---|
18 | } |
---|
19 | |
---|
20 | TcpipServer::~TcpipServer(void) |
---|
21 | { |
---|
22 | } |
---|
23 | |
---|
24 | void TcpipServer::Run(int nPort) |
---|
25 | { |
---|
26 | signal(SIGINT, OnExit); |
---|
27 | |
---|
28 | Logger::Instance().LogDebug("Initializing TCP/IP connection..."); |
---|
29 | |
---|
30 | // create socket and start listening |
---|
31 | struct sockaddr_in srvAddr; |
---|
32 | |
---|
33 | memset(&srvAddr, 0, sizeof srvAddr); |
---|
34 | srvAddr.sin_family = AF_INET; |
---|
35 | srvAddr.sin_addr.s_addr = INADDR_ANY; |
---|
36 | srvAddr.sin_port = htons(nPort); |
---|
37 | |
---|
38 | if ( (m_hListeningSocket = socket(AF_INET, SOCK_STREAM, 0)) == SOCKET_CALL_ERROR ) |
---|
39 | { |
---|
40 | Logger::Instance().LogError("Error creating listening socket"); |
---|
41 | return; |
---|
42 | } |
---|
43 | Logger::Instance().LogDebug("Listening socket successfully created"); |
---|
44 | |
---|
45 | if (bind(m_hListeningSocket, reinterpret_cast<sockaddr*> (&srvAddr), sizeof(srvAddr)) != 0) |
---|
46 | { |
---|
47 | Logger::Instance().LogError("Error binding listening socket; the port is probably used by other process"); |
---|
48 | return; |
---|
49 | } |
---|
50 | |
---|
51 | Logger::Instance().LogDebug("Listening socket successfully bound"); |
---|
52 | |
---|
53 | if (listen(m_hListeningSocket, 0) != 0 ) |
---|
54 | { |
---|
55 | Logger::Instance().LogError("Listening operation failed"); |
---|
56 | return; |
---|
57 | } |
---|
58 | |
---|
59 | std::string s = "Waiting for connection request on port "; |
---|
60 | s += boost::lexical_cast<std::string>(nPort); |
---|
61 | s += "..."; |
---|
62 | Logger::Instance().LogInfo(s); |
---|
63 | |
---|
64 | struct sockaddr_in clientAddr; |
---|
65 | while (-1) |
---|
66 | { |
---|
67 | int nLen = sizeof(clientAddr); |
---|
68 | SOCKET_TYPE sock = accept( |
---|
69 | m_hListeningSocket, reinterpret_cast<sockaddr*>(&clientAddr), &nLen); |
---|
70 | if (sock == SOCKET_CALL_ERROR) |
---|
71 | break; |
---|
72 | Logger::Instance().LogInfo("Connection accepted"); |
---|
73 | Logger::Instance().LogDebug("Starting socket thread..."); |
---|
74 | ConnectionHandler handler(sock); |
---|
75 | boost::thread thread(handler); |
---|
76 | } |
---|
77 | OnExit(); |
---|
78 | } |
---|
79 | |
---|
80 | TcpipServer::ConnectionHandler::ConnectionHandler(SOCKET_TYPE hSocket) : |
---|
81 | m_hSocket(hSocket) |
---|
82 | { |
---|
83 | } |
---|
84 | |
---|
85 | void TcpipServer::ConnectionHandler::operator()() |
---|
86 | { |
---|
87 | Logger::Instance().LogDebug("Socket thread started successfully"); |
---|
88 | |
---|
89 | ConnectionController cc(m_hSocket); |
---|
90 | { |
---|
91 | boost::mutex::scoped_lock lock(TcpipServer::Instance().m_mutex); |
---|
92 | TcpipServer::Instance().m_lActiveControllers.push_back(&cc); |
---|
93 | } |
---|
94 | cc.Run(); |
---|
95 | { |
---|
96 | boost::mutex::scoped_lock lock(TcpipServer::Instance().m_mutex); |
---|
97 | TcpipServer::Instance().m_lActiveControllers.remove(&cc); |
---|
98 | |
---|
99 | if (m_hSocket != SOCKET_CALL_ERROR) |
---|
100 | { |
---|
101 | CLOSE_SOCKET(m_hSocket); |
---|
102 | m_hSocket = SOCKET_CALL_ERROR; |
---|
103 | } |
---|
104 | Logger::Instance().LogDebug("Socket thread terminated"); |
---|
105 | TcpipServer::Instance().m_operationEnded.notify_one(); |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | void TcpipServer::OnExit() |
---|
110 | { |
---|
111 | if (m_hListeningSocket != SOCKET_CALL_ERROR) |
---|
112 | { |
---|
113 | { |
---|
114 | boost::mutex::scoped_lock lock(m_mutex); |
---|
115 | for(std::list<ConnectionController *>::iterator it = m_lActiveControllers.begin(); |
---|
116 | it != m_lActiveControllers.end(); it++) |
---|
117 | (*it)->Stop(); |
---|
118 | } |
---|
119 | // wait for threads to quit |
---|
120 | while (-1) |
---|
121 | { |
---|
122 | boost::mutex::scoped_lock cond(m_mutex); |
---|
123 | if (m_lActiveControllers.begin() == m_lActiveControllers.end()) |
---|
124 | break; |
---|
125 | m_operationEnded.wait(cond); |
---|
126 | } |
---|
127 | CLOSE_SOCKET(m_hListeningSocket); |
---|
128 | m_hListeningSocket = SOCKET_CALL_ERROR; |
---|
129 | Logger::Instance().LogDebug("Listening socket closed"); |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | void TcpipServer::OnExit(int nSig) |
---|
134 | { |
---|
135 | Instance().OnExit(); |
---|
136 | } |
---|