Stellarium  0.16.1
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 //FIXME: Remove these macro-redefinitions, we have no business messing with them
35 //ESPECIALLY if the change the values and are not commented
36 #ifdef _MSC_VER
37 //for now, this warning is disabled when this header is included
38 #pragma warning(disable: 4005)
39 #endif
40 
41 #define ERRNO WSAGetLastError()
42 #undef EAGAIN
43 #define EAGAIN WSAEWOULDBLOCK
44 #undef EINTR
45 #define EINTR WSAEINTR
46 #undef ECONNRESET
47 #define ECONNRESET WSAECONNRESET
48 static inline int SetNonblocking(int s)
49 {
50  u_long arg = 1;
51  return ioctlsocket(s, FIONBIO, &arg);
52 }
53 #define SETNONBLOCK(s) SetNonblocking(s)
54 #define SOCKLEN_T int
55 #define close closesocket
56 #define IS_INVALID_SOCKET(fd) (fd==INVALID_SOCKET)
57 #define STRERROR(x) x
58 // #define DEBUG5
59 
60 #else
61 
62 #include <netdb.h>
63 #include <netinet/in.h>
64 #include <sys/socket.h>
65 #include <sys/select.h>
66 #include <unistd.h>
67 #include <fcntl.h>
68 #include <errno.h>
69 #include <string.h> // strerror
70 #define ERRNO errno
71 #define SETNONBLOCK(s) fcntl(s,F_SETFL,O_NONBLOCK)
72 #define SOCKLEN_T socklen_t
73 #define SOCKET int
74 #define IS_INVALID_SOCKET(fd) (fd<0)
75 #define INVALID_SOCKET (-1)
76 #define STRERROR(x) strerror(x)
77 
78 #endif //Q_OS_WIN
79 
80 long long int GetNow(void);
81 
82 class Server;
83 
84 class Socket
85 {
86 public:
87  virtual ~Socket() { hangup(); }
88  void hangup();
89  virtual void prepareSelectFds(fd_set &read_fds, fd_set &write_fds, int &fd_max) = 0;
90  virtual void handleSelectFds(const fd_set &read_fds, const fd_set &write_fds) = 0;
91  virtual bool isClosed() const
92  {
93  return IS_INVALID_SOCKET(fd);
94  }
95  virtual bool isTcpConnection() const { return false; }
96  virtual void sendPosition(unsigned int ra_int, int dec_int, int status) {Q_UNUSED(ra_int); Q_UNUSED(dec_int); Q_UNUSED(status);}
97 
98 protected:
99  Socket(Server &server, SOCKET fd) : server(server), fd(fd) {}
100  Server & server;
101 
102 #ifdef Q_OS_WIN
103  virtual int readNonblocking(char *buf, int count)
104  {
105  return recv(fd, buf, count, 0);
106  }
107  virtual int writeNonblocking(const char *buf, int count)
108  {
109  return send(fd, buf, count, 0);
110  }
111 #else
112  int readNonblocking(void *buf, int count)
113  {
114  return read(fd, buf, count);
115  }
116  int writeNonblocking(const void *buf, int count) {
117  return write(fd, buf, count);
118  }
119 #endif
120 
121 protected:
122  SOCKET fd;
123 
124 private:
125  // no copying
126  Socket(const Socket&);
127  const Socket &operator=(const Socket&);
128 };
129 
130 #endif
Base class for telescope server classes.
Definition: Server.hpp:45