Stellarium 0.15.2
Socket.hpp
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 General Public License
12 as published by the Free Software Foundation; either version 2
13 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
18 GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this library; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
23 */
24 
25 #ifndef _SOCKET_HPP_
26 #define _SOCKET_HPP_
27 
28 #include <QtGlobal>
29 
30 #ifdef Q_OS_WIN
31 #include <winsock2.h>
32 #include <fcntl.h>
33 
34 #define ERRNO WSAGetLastError()
35 #undef EAGAIN
36 #define EAGAIN WSAEWOULDBLOCK
37 #undef EINTR
38 #define EINTR WSAEINTR
39 #undef ECONNRESET
40 #define ECONNRESET WSAECONNRESET
41 static inline int SetNonblocking(int s)
42 {
43  u_long arg = 1;
44  return ioctlsocket(s, FIONBIO, &arg);
45 }
46 #define SETNONBLOCK(s) SetNonblocking(s)
47 #define SOCKLEN_T int
48 #define close closesocket
49 #define IS_INVALID_SOCKET(fd) (fd==INVALID_SOCKET)
50 #define STRERROR(x) x
51 // #define DEBUG5
52 
53 #else
54 
55 #include <netdb.h>
56 #include <netinet/in.h>
57 #include <sys/socket.h>
58 #include <unistd.h>
59 #include <fcntl.h>
60 #include <errno.h>
61 #include <string.h> // strerror
62 #define ERRNO errno
63 #define SETNONBLOCK(s) fcntl(s,F_SETFL,O_NONBLOCK)
64 #define SOCKLEN_T socklen_t
65 #define SOCKET int
66 #define IS_INVALID_SOCKET(fd) (fd<0)
67 #define INVALID_SOCKET (-1)
68 #define STRERROR(x) strerror(x)
69 
70 #endif //Q_OS_WIN
71 
72 long long int GetNow(void);
73 
74 class Server;
75 
76 class Socket
77 {
78 public:
79  virtual ~Socket() { hangup(); }
80  void hangup();
81  virtual void prepareSelectFds(fd_set &read_fds, fd_set &write_fds, int &fd_max) = 0;
82  virtual void handleSelectFds(const fd_set &read_fds, const fd_set &write_fds) = 0;
83  virtual bool isClosed() const
84  {
85  return IS_INVALID_SOCKET(fd);
86  }
87  virtual bool isTcpConnection() const { return false; }
88  virtual void sendPosition(unsigned int ra_int, int dec_int, int status) {Q_UNUSED(ra_int); Q_UNUSED(dec_int); Q_UNUSED(status);}
89 
90 protected:
91  Socket(Server &server, SOCKET fd) : server(server), fd(fd) {}
92  Server & server;
93 
94 #ifdef Q_OS_WIN
95  virtual int readNonblocking(char *buf, int count)
96  {
97  return recv(fd, buf, count, 0);
98  }
99  virtual int writeNonblocking(const char *buf, int count)
100  {
101  return send(fd, buf, count, 0);
102  }
103 #else
104  int readNonblocking(void *buf, int count)
105  {
106  return read(fd, buf, count);
107  }
108  int writeNonblocking(const void *buf, int count) {
109  return write(fd, buf, count);
110  }
111 #endif
112 
113 protected:
114  SOCKET fd;
115 
116 private:
117  // no copying
118  Socket(const Socket&);
119  const Socket &operator=(const Socket&);
120 };
121 
122 #endif
Base class for telescope server classes.
Definition: Server.hpp:45