Stellarium 0.12.3
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_WIN32
31 
32 #include <winsock2.h>
33 #include <fcntl.h>
34 
35 #define ERRNO WSAGetLastError()
36 #undef EAGAIN
37 #define EAGAIN WSAEWOULDBLOCK
38 #undef EINTR
39 #define EINTR WSAEINTR
40 #undef ECONNRESET
41 #define ECONNRESET WSAECONNRESET
42 static inline int SetNonblocking(int s)
43 {
44  u_long arg = 1;
45  return ioctlsocket(s, FIONBIO, &arg);
46 }
47 #define SETNONBLOCK(s) SetNonblocking(s)
48 #define SOCKLEN_T int
49 #define close closesocket
50 #define IS_INVALID_SOCKET(fd) (fd==INVALID_SOCKET)
51 #define STRERROR(x) x
52 // #define DEBUG5
53 
54 #else
55 
56 #include <netdb.h>
57 #include <netinet/in.h>
58 #include <sys/socket.h>
59 #include <unistd.h>
60 #include <fcntl.h>
61 #include <errno.h>
62 #include <string.h> // strerror
63 #define ERRNO errno
64 #define SETNONBLOCK(s) fcntl(s,F_SETFL,O_NONBLOCK)
65 #define SOCKLEN_T socklen_t
66 #define SOCKET int
67 #define IS_INVALID_SOCKET(fd) (fd<0)
68 #define INVALID_SOCKET (-1)
69 #define STRERROR(x) strerror(x)
70 
71 #endif //Q_OS_WIN32
72 
73 long long int GetNow(void);
74 
75 class Server;
76 
77 class Socket
78 {
79 public:
80  virtual ~Socket() { hangup(); }
81  void hangup();
82  virtual void prepareSelectFds(fd_set &read_fds, fd_set &write_fds, int &fd_max) = 0;
83  virtual void handleSelectFds(const fd_set &read_fds, const fd_set &write_fds) = 0;
84  virtual bool isClosed() const
85  {
86  return IS_INVALID_SOCKET(fd);
87  }
88  virtual bool isTcpConnection() const { return false; }
89  virtual void sendPosition(unsigned int ra_int, int dec_int, int status) {Q_UNUSED(ra_int); Q_UNUSED(dec_int); Q_UNUSED(status);}
90 
91 protected:
92  Socket(Server &server, SOCKET fd) : server(server), fd(fd) {}
93  Server & server;
94 
95 #ifdef Q_OS_WIN32
96  virtual int readNonblocking(char *buf, int count)
97  {
98  return recv(fd, buf, count, 0);
99  }
100  virtual int writeNonblocking(const char *buf, int count)
101  {
102  return send(fd, buf, count, 0);
103  }
104 #else
105  int readNonblocking(void *buf, int count)
106  {
107  return read(fd, buf, count);
108  }
109  int writeNonblocking(const void *buf, int count) {
110  return write(fd, buf, count);
111  }
112 #endif
113 
114 protected:
115  SOCKET fd;
116 
117 private:
118  // no copying
119  Socket(const Socket&);
120  const Socket &operator=(const Socket&);
121 };
122 
123 #endif