1 | /*
|
---|
2 | * synergy -- mouse and keyboard sharing utility
|
---|
3 | * Copyright (C) 2002 Chris Schoeneman
|
---|
4 | *
|
---|
5 | * This package is free software you can redistribute it and/or
|
---|
6 | * modify it under the terms of the GNU General Public License
|
---|
7 | * found in the file COPYING that should have accompanied this file.
|
---|
8 | *
|
---|
9 | * This package is distributed in the hope that it will be useful,
|
---|
10 | * but WITHOUT ANY WARRANTY without even the implied warranty of
|
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
12 | * GNU General Public License for more details.
|
---|
13 | */
|
---|
14 |
|
---|
15 | #ifndef CARCHNETWORKBSD_H
|
---|
16 | #define CARCHNETWORKBSD_H
|
---|
17 |
|
---|
18 | #include "IArchNetwork.h"
|
---|
19 | #include "IArchMultithread.h"
|
---|
20 | #if HAVE_SYS_TYPES_H
|
---|
21 | # include <sys/types.h>
|
---|
22 | #endif
|
---|
23 | #if HAVE_SYS_SOCKET_H
|
---|
24 | # include <sys/socket.h>
|
---|
25 | #endif
|
---|
26 |
|
---|
27 | #if !HAVE_SOCKLEN_T
|
---|
28 | typedef int socklen_t;
|
---|
29 | #endif
|
---|
30 |
|
---|
31 | // old systems may use char* for [gs]etsockopt()'s optval argument.
|
---|
32 | // this should be void on modern systems but char is forwards
|
---|
33 | // compatible so we always use it.
|
---|
34 | typedef char optval_t;
|
---|
35 |
|
---|
36 | #define ARCH_NETWORK CArchNetworkBSD
|
---|
37 |
|
---|
38 | class CArchSocketImpl {
|
---|
39 | public:
|
---|
40 | int m_fd;
|
---|
41 | int m_refCount;
|
---|
42 | };
|
---|
43 |
|
---|
44 | class CArchNetAddressImpl {
|
---|
45 | public:
|
---|
46 | CArchNetAddressImpl() : m_len(sizeof(m_addr)) { }
|
---|
47 |
|
---|
48 | public:
|
---|
49 | struct sockaddr m_addr;
|
---|
50 | socklen_t m_len;
|
---|
51 | };
|
---|
52 |
|
---|
53 | //! Berkeley (BSD) sockets implementation of IArchNetwork
|
---|
54 | class CArchNetworkBSD : public IArchNetwork {
|
---|
55 | public:
|
---|
56 | CArchNetworkBSD();
|
---|
57 | virtual ~CArchNetworkBSD();
|
---|
58 |
|
---|
59 | // IArchNetwork overrides
|
---|
60 | virtual CArchSocket newSocket(EAddressFamily, ESocketType);
|
---|
61 | virtual CArchSocket copySocket(CArchSocket s);
|
---|
62 | virtual void closeSocket(CArchSocket s);
|
---|
63 | virtual void closeSocketForRead(CArchSocket s);
|
---|
64 | virtual void closeSocketForWrite(CArchSocket s);
|
---|
65 | virtual void bindSocket(CArchSocket s, CArchNetAddress addr);
|
---|
66 | virtual void listenOnSocket(CArchSocket s);
|
---|
67 | virtual CArchSocket acceptSocket(CArchSocket s, CArchNetAddress* addr);
|
---|
68 | virtual bool connectSocket(CArchSocket s, CArchNetAddress name);
|
---|
69 | virtual int pollSocket(CPollEntry[], int num, double timeout);
|
---|
70 | virtual void unblockPollSocket(CArchThread thread);
|
---|
71 | virtual size_t readSocket(CArchSocket s, void* buf, size_t len);
|
---|
72 | virtual size_t writeSocket(CArchSocket s,
|
---|
73 | const void* buf, size_t len);
|
---|
74 | virtual void throwErrorOnSocket(CArchSocket);
|
---|
75 | virtual bool setNoDelayOnSocket(CArchSocket, bool noDelay);
|
---|
76 | virtual bool setReuseAddrOnSocket(CArchSocket, bool reuse);
|
---|
77 | virtual std::string getHostName();
|
---|
78 | virtual CArchNetAddress newAnyAddr(EAddressFamily);
|
---|
79 | virtual CArchNetAddress copyAddr(CArchNetAddress);
|
---|
80 | virtual CArchNetAddress nameToAddr(const std::string&);
|
---|
81 | virtual void closeAddr(CArchNetAddress);
|
---|
82 | virtual std::string addrToName(CArchNetAddress);
|
---|
83 | virtual std::string addrToString(CArchNetAddress);
|
---|
84 | virtual EAddressFamily getAddrFamily(CArchNetAddress);
|
---|
85 | virtual void setAddrPort(CArchNetAddress, int port);
|
---|
86 | virtual int getAddrPort(CArchNetAddress);
|
---|
87 | virtual bool isAnyAddr(CArchNetAddress);
|
---|
88 | virtual bool isEqualAddr(CArchNetAddress, CArchNetAddress);
|
---|
89 |
|
---|
90 | private:
|
---|
91 | const int* getUnblockPipe();
|
---|
92 | const int* getUnblockPipeForThread(CArchThread);
|
---|
93 | void setBlockingOnSocket(int fd, bool blocking);
|
---|
94 | void throwError(int);
|
---|
95 | void throwNameError(int);
|
---|
96 |
|
---|
97 | private:
|
---|
98 | CArchMutex m_mutex;
|
---|
99 | };
|
---|
100 |
|
---|
101 | #endif
|
---|