| 6 |
| * Copyright Nikolai Kudashov, 2015. |
| 7 | */ |
| 8 |
| 9 | #ifndef CONNECTIONSOCKET_H |
| 10 | #define CONNECTIONSOCKET_H |
| 11 |
| 12 | #include <sys/epoll.h> |
| 13 | #include <netinet/in.h> |
| 14 | #include <string> |
| 15 |
| 16 | class NativeByteBuffer; |
| 17 | class ConnectionsManager; |
| 18 | class ByteStream; |
| 19 | class EventObject; |
| 20 |
| 21 | class ConnectionSocket { |
| 22 |
| 23 | public: |
| 24 | ConnectionSocket(); |
| 25 | virtual ~ConnectionSocket(); |
| 26 |
| 27 | void writeBuffer(NativeByteBuffer *buffer); |
| 28 | void openConnection(std::string address, uint16_t port, bool ipv6); |
| 29 | void setTimeout(time_t timeout); |
| 30 | bool isDisconnected(); |
| 31 | void dropConnection(); |
| 32 |
| 33 | protected: |
| 34 | void onEvent(uint32_t events); |
| 35 | void checkTimeout(int64_t now); |
| 36 | virtual void onReceivedData(NativeByteBuffer *buffer) = 0; |
| 37 | virtual void onDisconnected(int reason) = 0; |
| 38 | virtual void onConnected() = 0; |
| 39 |
| 40 | private: |
| 41 | ByteStream *outgoingByteStream = nullptr; |
| 42 | struct epoll_event eventMask; |
| 43 | struct sockaddr_in socketAddress; |
| 44 | struct sockaddr_in6 socketAddress6; |
| 45 | int socketFd = -1; |
| 46 | time_t timeout = 15; |
| 47 | bool onConnectedSent = false; |
| 48 | int64_t lastEventTime = 0; |
| 49 | EventObject *eventObject; |
| 50 |
| 51 | bool checkSocketError(); |
| 52 | void closeSocket(int reason); |
| 53 | void adjustWriteOp(); |
| 54 |
| 55 | friend class EventObject; |
| 56 | friend class ConnectionsManager; |
| 57 | }; |
| 58 |
| 59 | #endif |
| 60 |