void NetworkClient::initialize(NetworkObject *object, QTcpSocket *client) { client->setParent(this); this->client = client; ptrNetworkdObject = object; this->ip = client->peerAddress().toString(); connect(this->client, SIGNAL(readyRead()), this, SLOT(onReceivedData())); connect(this->client, SIGNAL(bytesWritten(qint64)), this, SLOT(onSendData(qint64))); connect(this->client, SIGNAL(disconnected()), this, SLOT(onDisconnected())); }
void ClientController::onSocket_ReadEvent(coconut_socket_t fd) { if(protocolFactory_) { //#define PROTOCOL_READ_FROM_SOCKET #ifdef PROTOCOL_READ_FROM_SOCKET do { if(!protocol_ || protocol_->isReadComplete()) { _LOG_TRACE("New Protocol make #1 in %p\n", this); protocol_ = protocolFactory_->makeProtocol(); } if(protocol_->processRead(socket()) == true) { onReceivedProtocol(protocol_); } else { break; } } while(1); #else char chunk[IOBUF_LEN]; int nread = socket()->read(chunk, IOBUF_LEN); if(nread <= 0) return; //logger::hexdump( (unsigned char*)chunk, nread, stdout); if(!protocol_ || protocol_->isReadComplete()) { _LOG_TRACE("New Protocol make #1 in this = %p\n", this); protocol_ = protocolFactory_->makeProtocol(); } _LOG_DEBUG("ClientController read socket fd = %d, readSize = %d in %p\n", socket()->socketFD(), nread, this); protocol_->addToReadingBuffer(chunk, nread); int index = -1; while(true) { index++; if(protocol_->processReadFromReadingBuffer() == true) { // fire! eventGotProtocol()->fireObservers(shared_from_this(), protocol_); onReceivedProtocol(protocol_); _LOG_TRACE("ClientController Protocol receved completed. readSize = %d, remainBufferSize = %d in %p, index = %d\n", nread, protocol_->remainingBufferSize(), this, index); if(protocol_->remainingBufferSize() > 0) { #define ALWAS_MAKE_PROTOCOL #ifdef ALWAS_MAKE_PROTOCOL // new protocol _LOG_TRACE("New Protocol make #2 in this = %p\n", this); boost::shared_ptr<protocol::BaseProtocol> protocolTemp = protocolFactory_->makeProtocol(); protocolTemp->addToReadingBuffer(protocol_->remainingBufferPtr(), protocol_->remainingBufferSize()); protocol_ = protocolTemp; #else protocol_->resetReadingBufferToRemainingBuffer(); #endif continue; // one more processing.. } } // --> parsing failed.. if(protocol_->isInvalidPacketReceived()) { // this session close! _LOG_ERROR("Invalid Packet Recved.. this = %p, size = \n", this, protocol_->payloadBuffer()->totalSize()); socket()->close(); } break; } #endif } else { char buffer[IOBUF_LEN]; int res = socket()->read(buffer, IOBUF_LEN); if(res > 0) onReceivedData(buffer, res); } }
void ConnectionSocket::onEvent(uint32_t events) { if (events & EPOLLIN) { if (checkSocketError()) { closeSocket(1); return; } else { ssize_t readCount; NativeByteBuffer *buffer = ConnectionsManager::getInstance().networkBuffer; while (true) { buffer->rewind(); readCount = recv(socketFd, buffer->bytes(), READ_BUFFER_SIZE, 0); if (readCount < 0) { closeSocket(1); DEBUG_E("connection(%p) recv failed", this); return; } if (readCount > 0) { buffer->limit((uint32_t) readCount); lastEventTime = ConnectionsManager::getInstance().getCurrentTimeMonotonicMillis(); if (proxyAuthState == 2) { if (readCount == 2) { uint8_t auth_method = buffer->bytes()[1]; if (auth_method == 0xff) { closeSocket(1); DEBUG_E("connection(%p) unsupported proxy auth method", this); } else if (auth_method == 0x02) { DEBUG_D("connection(%p) proxy auth required", this); proxyAuthState = 3; } else if (auth_method == 0x00) { proxyAuthState = 5; } adjustWriteOp(); } else { closeSocket(1); DEBUG_E("connection(%p) invalid proxy response on state 2", this); } } else if (proxyAuthState == 4) { if (readCount == 2) { uint8_t auth_method = buffer->bytes()[1]; if (auth_method != 0x00) { closeSocket(1); DEBUG_E("connection(%p) auth invalid", this); } else { proxyAuthState = 5; } adjustWriteOp(); } else { closeSocket(1); DEBUG_E("connection(%p) invalid proxy response on state 4", this); } } else if (proxyAuthState == 6) { if (readCount > 2) { uint8_t status = buffer->bytes()[1]; if (status == 0x00) { DEBUG_D("connection(%p) connected via proxy", this); proxyAuthState = 0; adjustWriteOp(); } else { closeSocket(1); DEBUG_E("connection(%p) invalid proxy status on state 6, 0x%x", this, status); } } else { closeSocket(1); DEBUG_E("connection(%p) invalid proxy response on state 6", this); } } else if (proxyAuthState == 0) { if (ConnectionsManager::getInstance().delegate != nullptr) { ConnectionsManager::getInstance().delegate->onBytesReceived(readCount, currentNetworkType); } onReceivedData(buffer); } } if (readCount != READ_BUFFER_SIZE) { break; } } } } if (events & EPOLLOUT) { if (checkSocketError() != 0) { closeSocket(1); return; } else { if (proxyAuthState != 0) { static uint8_t buffer[1024]; if (proxyAuthState == 1) { lastEventTime = ConnectionsManager::getInstance().getCurrentTimeMonotonicMillis(); proxyAuthState = 2; buffer[0] = 0x05; buffer[1] = 0x02; buffer[2] = 0x00; buffer[3] = 0x02; if (send(socketFd, buffer, 4, 0) < 0) { DEBUG_E("connection(%p) send failed", this); closeSocket(1); return; } adjustWriteOp(); } else if (proxyAuthState == 3) { buffer[0] = 0x01; uint8_t len1 = (uint8_t) ConnectionsManager::getInstance().proxyUser.length(); uint8_t len2 = (uint8_t) ConnectionsManager::getInstance().proxyPassword.length(); buffer[1] = len1; memcpy(&buffer[2], ConnectionsManager::getInstance().proxyUser.c_str(), len1); buffer[2 + len1] = len2; memcpy(&buffer[3 + len1], ConnectionsManager::getInstance().proxyPassword.c_str(), len2); proxyAuthState = 4; if (send(socketFd, buffer, 3 + len1 + len2, 0) < 0) { DEBUG_E("connection(%p) send failed", this); closeSocket(1); return; } adjustWriteOp(); } else if (proxyAuthState == 5) { buffer[0] = 0x05; buffer[1] = 0x01; buffer[2] = 0x00; buffer[3] = (uint8_t) (isIpv6 ? 0x04 : 0x01); uint16_t networkPort = ntohs(currentPort); inet_pton(isIpv6 ? AF_INET6 : AF_INET, currentAddress.c_str(), &buffer[4]); memcpy(&buffer[4 + (isIpv6 ? 16 : 4)], &networkPort, sizeof(uint16_t)); proxyAuthState = 6; if (send(socketFd, buffer, 4 + (isIpv6 ? 16 : 4) + 2, 0) < 0) { DEBUG_E("connection(%p) send failed", this); closeSocket(1); return; } adjustWriteOp(); } } else { if (!onConnectedSent) { lastEventTime = ConnectionsManager::getInstance().getCurrentTimeMonotonicMillis(); onConnected(); onConnectedSent = true; } NativeByteBuffer *buffer = ConnectionsManager::getInstance().networkBuffer; buffer->clear(); outgoingByteStream->get(buffer); buffer->flip(); uint32_t remaining = buffer->remaining(); if (remaining) { ssize_t sentLength; if ((sentLength = send(socketFd, buffer->bytes(), remaining, 0)) < 0) { DEBUG_E("connection(%p) send failed", this); closeSocket(1); return; } else { if (ConnectionsManager::getInstance().delegate != nullptr) { ConnectionsManager::getInstance().delegate->onBytesSent(sentLength, currentNetworkType); } outgoingByteStream->discard((uint32_t) sentLength); adjustWriteOp(); } } } } } if ((events & EPOLLRDHUP) || (events & EPOLLHUP)) { DEBUG_E("socket event has EPOLLHUP"); closeSocket(1); return; } if (events & EPOLLERR) { DEBUG_E("connection(%p) epoll error", this); return; } }