1 | /* |
---|
2 | The stellarium telescope library helps building |
---|
3 | telescope server programs, that can communicate with stellarium |
---|
4 | by means of the stellarium TCP telescope protocol. |
---|
5 | It also contains smaple server classes (dummy, Meade LX200). |
---|
6 | |
---|
7 | Author and Copyright of this file and of the stellarium telescope library: |
---|
8 | Johannes Gajdosik, 2006 |
---|
9 | |
---|
10 | This library is free software; you can redistribute it and/or |
---|
11 | modify it under the terms of the GNU Lesser General Public |
---|
12 | License as published by the Free Software Foundation; either |
---|
13 | version 2.1 of the License, or (at your option) any later version. |
---|
14 | |
---|
15 | This library is distributed in the hope that it will be useful, |
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
18 | Lesser General Public License for more details. |
---|
19 | |
---|
20 | You should have received a copy of the GNU Lesser General Public |
---|
21 | License along with this library; if not, write to the Free Software |
---|
22 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
---|
23 | */ |
---|
24 | |
---|
25 | #include "Listener.hpp" |
---|
26 | #include "Connection.hpp" |
---|
27 | #include "Server.hpp" |
---|
28 | #include "LogFile.hpp" |
---|
29 | |
---|
30 | #include <iostream> |
---|
31 | |
---|
32 | #include <stdlib.h> // exit |
---|
33 | |
---|
34 | using namespace std; |
---|
35 | |
---|
36 | void Listener::prepareSelectFds(fd_set &read_fds, |
---|
37 | fd_set &write_fds, |
---|
38 | int &fd_max) |
---|
39 | { |
---|
40 | if (IS_INVALID_SOCKET(fd)) |
---|
41 | { |
---|
42 | struct sockaddr_in sock_addr; |
---|
43 | fd = socket(AF_INET, SOCK_STREAM, 0); |
---|
44 | if (IS_INVALID_SOCKET(fd)) |
---|
45 | { |
---|
46 | *log_file << Now() |
---|
47 | << "socket() failed: " |
---|
48 | << STRERROR(ERRNO) |
---|
49 | << endl; |
---|
50 | exit(127); |
---|
51 | } |
---|
52 | |
---|
53 | int yes = -1; // all bits set to 1 |
---|
54 | if (0 != setsockopt(fd, |
---|
55 | SOL_SOCKET, |
---|
56 | SO_REUSEADDR, |
---|
57 | reinterpret_cast<const char*>(&yes), |
---|
58 | sizeof(int))) |
---|
59 | { |
---|
60 | *log_file << Now() |
---|
61 | << "setsockopt(SO_REUSEADDR) failed: " |
---|
62 | << STRERROR(ERRNO) |
---|
63 | << endl; |
---|
64 | exit(127); |
---|
65 | } |
---|
66 | |
---|
67 | sock_addr.sin_family = AF_INET; |
---|
68 | sock_addr.sin_addr.s_addr = INADDR_ANY; |
---|
69 | sock_addr.sin_port = htons(port); |
---|
70 | if (bind(fd, (struct sockaddr*)(&sock_addr), sizeof(sock_addr))) |
---|
71 | { |
---|
72 | *log_file << Now() |
---|
73 | << "bind(...) failed: " |
---|
74 | << STRERROR(ERRNO) |
---|
75 | << endl; |
---|
76 | exit(127); |
---|
77 | } |
---|
78 | |
---|
79 | if (listen(fd, 10)) |
---|
80 | { |
---|
81 | *log_file << Now() |
---|
82 | << "listen(...) failed: " |
---|
83 | << STRERROR(ERRNO) |
---|
84 | << endl; |
---|
85 | exit(127); |
---|
86 | } |
---|
87 | |
---|
88 | *log_file << Now() << "listening on port " << port << endl; |
---|
89 | } |
---|
90 | else |
---|
91 | { |
---|
92 | if (fd_max < (int)fd) |
---|
93 | fd_max = (int)fd; |
---|
94 | FD_SET(fd, &read_fds); |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | void Listener::handleSelectFds(const fd_set &read_fds, |
---|
99 | const fd_set &write_fds) |
---|
100 | { |
---|
101 | if (!IS_INVALID_SOCKET(fd) |
---|
102 | && FD_ISSET(fd, const_cast<fd_set *>(&read_fds))) |
---|
103 | { |
---|
104 | struct sockaddr_in client_addr; |
---|
105 | SOCKLEN_T length = sizeof(client_addr); |
---|
106 | const SOCKET client_sock = accept(fd, |
---|
107 | (struct sockaddr*)&client_addr, |
---|
108 | &length); |
---|
109 | |
---|
110 | if (IS_INVALID_SOCKET(client_sock)) |
---|
111 | { |
---|
112 | *log_file << Now() |
---|
113 | << "accept(...) failed: " |
---|
114 | << STRERROR(ERRNO) |
---|
115 | << endl; |
---|
116 | close(client_sock); |
---|
117 | return; |
---|
118 | } |
---|
119 | |
---|
120 | *log_file << Now() << "connection accepted" << endl; |
---|
121 | if (0 != SETNONBLOCK(client_sock)) |
---|
122 | { |
---|
123 | *log_file << Now() |
---|
124 | << "SETNONBLOCK(...) failed: " |
---|
125 | << STRERROR(ERRNO) |
---|
126 | << endl; |
---|
127 | close(client_sock); |
---|
128 | return; |
---|
129 | } |
---|
130 | server.addConnection(new Connection(server, client_sock)); |
---|
131 | } |
---|
132 | } |
---|