1 /* 2 * synergy -- mouse and keyboard sharing utility 3 * Copyright (C) 2012-2016 Symless Ltd. 4 * Copyright (C) 2002 Chris Schoeneman 5 * 6 * This package is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License 8 * found in the file LICENSE that should have accompanied this file. 9 * 10 * This package is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 19 #pragma once 20 21 #include "net/IDataSocket.h" 22 #include "io/StreamBuffer.h" 23 #include "mt/CondVar.h" 24 #include "mt/Mutex.h" 25 #include "arch/IArchNetwork.h" 26 27 class Mutex; 28 class Thread; 29 class ISocketMultiplexerJob; 30 class IEventQueue; 31 class SocketMultiplexer; 32 33 //! TCP data socket 34 /*! 35 A data socket using TCP. 36 */ 37 class TCPSocket : public IDataSocket { 38 public: 39 TCPSocket(IEventQueue* events, SocketMultiplexer* socketMultiplexer, IArchNetwork::EAddressFamily family = IArchNetwork::kINET); 40 TCPSocket(IEventQueue* events, SocketMultiplexer* socketMultiplexer, ArchSocket socket); 41 TCPSocket(TCPSocket const &) =delete; 42 TCPSocket(TCPSocket &&) =delete; 43 virtual ~TCPSocket(); 44 45 TCPSocket& operator=(TCPSocket const &) =delete; 46 TCPSocket& operator=(TCPSocket &&) =delete; 47 48 // ISocket overrides 49 virtual void bind(const NetworkAddress&); 50 virtual void close(); 51 virtual void* getEventTarget() const; 52 53 // IStream overrides 54 virtual UInt32 read(void* buffer, UInt32 n); 55 virtual void write(const void* buffer, UInt32 n); 56 virtual void flush(); 57 virtual void shutdownInput(); 58 virtual void shutdownOutput(); 59 virtual bool isReady() const; 60 virtual bool isFatal() const; 61 virtual UInt32 getSize() const; 62 63 // IDataSocket overrides 64 virtual void connect(const NetworkAddress&); 65 66 67 virtual ISocketMultiplexerJob* 68 newJob(); 69 70 protected: 71 enum EJobResult { 72 kBreak = -1, //!< Break the Job chain 73 kRetry, //!< Retry the same job 74 kNew //!< Require a new job 75 }; 76 getSocket()77 ArchSocket getSocket() { return m_socket; } getEvents()78 IEventQueue* getEvents() { return m_events; } 79 virtual EJobResult doRead(); 80 virtual EJobResult doWrite(); 81 82 void setJob(ISocketMultiplexerJob*); 83 isReadable()84 bool isReadable() { return m_readable; } isWritable()85 bool isWritable() { return m_writable; } 86 getMutex()87 Mutex& getMutex() { return m_mutex; } 88 89 void sendEvent(Event::Type); 90 void discardWrittenData(int bytesWrote); 91 92 private: 93 void init(); 94 95 void sendConnectionFailedEvent(const char*); 96 void onConnected(); 97 void onInputShutdown(); 98 void onOutputShutdown(); 99 void onDisconnected(); 100 101 ISocketMultiplexerJob* 102 serviceConnecting(ISocketMultiplexerJob*, 103 bool, bool, bool); 104 ISocketMultiplexerJob* 105 serviceConnected(ISocketMultiplexerJob*, 106 bool, bool, bool); 107 108 protected: 109 bool m_readable; 110 bool m_writable; 111 bool m_connected; 112 IEventQueue* m_events; 113 StreamBuffer m_inputBuffer; 114 StreamBuffer m_outputBuffer; 115 116 private: 117 Mutex m_mutex; 118 ArchSocket m_socket; 119 CondVar<bool> m_flushed; 120 SocketMultiplexer* m_socketMultiplexer; 121 }; 122