| 6 |
| * Copyright Nikolai Kudashov, 2015. |
| 7 | */ |
| 8 |
| 9 | #include <unistd.h> |
| 10 | #include <fcntl.h> |
| 11 | #include <errno.h> |
| 12 | #include <sys/socket.h> |
| 13 | #include <memory.h> |
| 14 | #include <netinet/tcp.h> |
| 15 | #include <arpa/inet.h> |
| 16 | #include "ByteStream.h" |
| 17 | #include "ConnectionSocket.h" |
| 18 | #include "FileLog.h" |
| 19 | #include "Defines.h" |
| 20 | #include "ConnectionsManager.h" |
| 21 | #include "EventObject.h" |
| 22 | #include "Timer.h" |
| 23 | #include "NativeByteBuffer.h" |
| 24 |
| 25 | #ifndef EPOLLRDHUP |
| 26 | #define EPOLLRDHUP 0x2000 |
| 27 | #endif |
| 28 |
| 29 | ConnectionSocket::ConnectionSocket() { |
| 30 | outgoingByteStream = new ByteStream(); |
| 31 | lastEventTime = ConnectionsManager::getInstance().getCurrentTimeMillis(); |
| 32 | eventObject = new EventObject(this, EventObjectTypeConnection); |
| 33 | } |
| 34 |
| 35 | ConnectionSocket::~ConnectionSocket() { |
| 36 | if (outgoingByteStream != nullptr) { |
| 37 | delete outgoingByteStream; |
| 38 | outgoingByteStream = nullptr; |
| 39 | } |
| 40 | if (eventObject != nullptr) { |
| 41 | delete eventObject; |
| 42 | eventObject = nullptr; |
| 43 | } |
| 44 | } |
| 45 |
| 46 | void ConnectionSocket::openConnection(std::string address, uint16_t port, bool ipv6) { |
| 47 | int epolFd = ConnectionsManager::getInstance().epolFd; |
| 48 |
| 49 | if ((socketFd = socket(ipv6 ? AF_INET6 : AF_INET, SOCK_STREAM, 0)) < 0) { |
| 50 | DEBUG_E("connection(%p) can't create socket", this); |
| 51 | closeSocket(1); |
| 52 | return; |
| 53 | } |
| 54 |
| 55 | memset(&socketAddress, 0, sizeof(sockaddr_in)); |
| 56 | memset(&socketAddress6, 0, sizeof(sockaddr_in6)); |
| 57 |
| 58 | if (ipv6) { |
| 59 | socketAddress6.sin6_family = AF_INET6; |
| 60 | socketAddress6.sin6_port = htons(port); |
| 61 | if (inet_pton(AF_INET6, address.c_str(), &socketAddress6.sin6_addr.s6_addr) != 1) { |
| 62 | DEBUG_E("connection(%p) bad ipv6 %s", this, address.c_str()); |
| 63 | closeSocket(1); |
| 64 | return; |
| 65 | } |
| 66 | } else { |
| 67 | socketAddress.sin_family = AF_INET; |
| 68 | socketAddress.sin_port = htons(port); |
| 69 | if (inet_pton(AF_INET, address.c_str(), &socketAddress.sin_addr.s_addr) != 1) { |
| 70 | DEBUG_E("connection(%p) bad ipv4 %s", this, address.c_str()); |
| 71 | closeSocket(1); |
| 72 | return; |
| 73 | } |
| 74 | } |
| 75 |
| 76 | int yes = 1; |
| 77 | if (setsockopt(socketFd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(int))) { |
| 78 | DEBUG_E("connection(%p) set TCP_NODELAY failed", this); |
| 79 | } |
| 80 |
| 81 | if (fcntl(socketFd, F_SETFL, O_NONBLOCK) == -1) { |
| 82 | DEBUG_E("connection(%p) set O_NONBLOCK failed", this); |
| 83 | closeSocket(1); |
| 84 | return; |
| 85 | } |
| 86 |
| 87 | if (connect(socketFd, (ipv6 ? (sockaddr *) &socketAddress6 : (sockaddr *) &socketAddress), (socklen_t) (ipv6 ? sizeof(sockaddr_in6) : sizeof(sockaddr_in))) == -1 && errno != EINPROGRESS) { |
| 88 | closeSocket(1); |
| 89 | } else { |
| 90 | eventMask.events = EPOLLOUT | EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLET; |
| 91 | eventMask.data.ptr = eventObject; |
| 92 | if (epoll_ctl(epolFd, EPOLL_CTL_ADD, socketFd, &eventMask) != 0) { |
| 93 | DEBUG_E("connection(%p) epoll_ctl, adding socket failed", this); |
| 94 | closeSocket(1); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 |
| 99 | bool ConnectionSocket::checkSocketError() { |
| 100 | if (socketFd < 0) { |
| 101 | return true; |
| 102 | } |
| 103 | int ret; |
| 104 | int code; |
| 105 | socklen_t len = sizeof(int); |
| 106 | ret = getsockopt(socketFd, SOL_SOCKET, SO_ERROR, &code, &len); |
| 107 | return (ret || code) != 0; |
| 108 | } |
| 109 |
| 110 | void ConnectionSocket::closeSocket(int reason) { |
| 111 | lastEventTime = ConnectionsManager::getInstance().getCurrentTimeMillis(); |
| 112 | ConnectionsManager::getInstance().detachConnection(this); |
| 113 | if (socketFd >= 0) { |
| 114 | epoll_ctl(ConnectionsManager::getInstance().epolFd, EPOLL_CTL_DEL, socketFd, NULL); |
| 115 | if (close(socketFd) != 0) { |
| 116 | DEBUG_E("connection(%p) unable to close socket", this); |
| 117 | } |
| 118 | socketFd = -1; |
| 119 | } |
| 120 | onConnectedSent = false; |
| 121 | outgoingByteStream->clean(); |
| 122 | onDisconnected(reason); |
| 123 | } |
| 124 |
| 125 | void ConnectionSocket::onEvent(uint32_t events) { |
| 126 | if (events & EPOLLIN) { |
| 127 | if (checkSocketError()) { |
| 128 | closeSocket(1); |
| 129 | return; |
| 130 | } else { |
| 131 | ssize_t readCount; |
| 132 | NativeByteBuffer *buffer = ConnectionsManager::getInstance().networkBuffer; |
| 133 | while (true) { |
| 134 | buffer->rewind(); |
| 135 | readCount = recv(socketFd, buffer->bytes(), READ_BUFFER_SIZE, 0); |
| 136 | if (readCount < 0) { |
| 137 | closeSocket(1); |
| 138 | DEBUG_E("connection(%p) recv failed", this); |
| 139 | return; |
| 140 | } |
| 141 | if (readCount > 0) { |
| 142 | buffer->limit((uint32_t) readCount); |
| 143 | lastEventTime = ConnectionsManager::getInstance().getCurrentTimeMillis(); |
| 144 | onReceivedData(buffer); |
| 145 | } |
| 146 | if (readCount != READ_BUFFER_SIZE) { |
| 147 | break; |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | if (events & EPOLLOUT) { |
| 153 | if (checkSocketError() != 0) { |
| 154 | closeSocket(1); |
| 155 | return; |
| 156 | } else { |
| 157 | if (!onConnectedSent) { |
| 158 | ConnectionsManager::getInstance().attachConnection(this); |
| 159 | lastEventTime = ConnectionsManager::getInstance().getCurrentTimeMillis(); |
| 160 | onConnected(); |
| 161 | onConnectedSent = true; |
| 162 | } |
| 163 | NativeByteBuffer *buffer = ConnectionsManager::getInstance().networkBuffer; |
| 164 | buffer->clear(); |
| 165 | outgoingByteStream->get(buffer); |
| 166 | buffer->flip(); |
| 167 |
| 168 | uint32_t remaining = buffer->remaining(); |
| 169 | if (remaining) { |
| 170 | ssize_t sentLength; |
| 171 | if ((sentLength = send(socketFd, buffer->bytes(), remaining, 0)) < 0) { |
| 172 | DEBUG_E("connection(%p) send failed", this); |
| 173 | closeSocket(1); |
| 174 | return; |
| 175 | } else { |
| 176 | outgoingByteStream->discard((uint32_t) sentLength); |
| 177 | adjustWriteOp(); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | if ((events & EPOLLRDHUP) || (events & EPOLLHUP)) { |
| 183 | closeSocket(1); |
| 184 | return; |
| 185 | } |
| 186 | if (events & EPOLLERR) { |
| 187 | DEBUG_E("connection(%p) epoll error", this); |
| 188 | return; |
| 189 | } |
| 190 | } |
| 191 |
| 192 | void ConnectionSocket::writeBuffer(NativeByteBuffer *buffer) { |
| 193 | outgoingByteStream->append(buffer); |
| 194 | adjustWriteOp(); |
| 195 | } |
| 196 |
| 197 | void ConnectionSocket::adjustWriteOp() { |
| 198 | eventMask.events = EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLET; |
| 199 | if (outgoingByteStream->hasData()) { |
| 200 | eventMask.events |= EPOLLOUT; |
| 201 | } |
| 202 | eventMask.data.ptr = eventObject; |
| 203 | if (epoll_ctl(ConnectionsManager::getInstance().epolFd, EPOLL_CTL_MOD, socketFd, &eventMask) != 0) { |
| 204 | DEBUG_E("connection(%p) epoll_ctl, modify socket failed", this); |
| 205 | closeSocket(1); |
| 206 | } |
| 207 | } |
| 208 |
| 209 | void ConnectionSocket::setTimeout(time_t time) { |
| 210 | timeout = time; |
| 211 | lastEventTime = ConnectionsManager::getInstance().getCurrentTimeMillis(); |
| 212 | } |
| 213 |
| 214 | void ConnectionSocket::checkTimeout(int64_t now) { |
| 215 | if (timeout != 0 && (now - lastEventTime) > (int64_t) timeout * 1000) { |
| 216 | closeSocket(2); |
| 217 | } |
| 218 | } |
| 219 |
| 220 | bool ConnectionSocket::isDisconnected() { |
| 221 | return socketFd < 0; |
| 222 | } |
| 223 |
| 224 | void ConnectionSocket::dropConnection() { |
| 225 | closeSocket(0); |
| 226 | } |
| 227 |