void SSLSocket::onConnect(uv_connect_t* handle, int status) { TraceS(this) << "On connect" << endl; if (status) { setUVError("SSL connect error", status); return; } else readStart(); SSL* ssl = SSL_new(_context->sslContext()); // TODO: Automatic SSL session handling. // Maybe add a stored session to the network manager. if (_session) SSL_set_session(ssl, _session->sslSession()); SSL_set_connect_state(ssl); SSL_do_handshake(ssl); _sslAdapter.init(ssl); _sslAdapter.flush(); //emitConnect(); onSocketConnect(); TraceS(this) << "On connect: OK" << endl; }
void UDPSocket::afterSend(uv_udp_send_t* req, int status) { auto sr = reinterpret_cast<internal::SendRequest*>(req); auto socket = reinterpret_cast<UDPSocket*>(sr->req.handle->data); if (status) { ErrorL << "Send error: " << uv_err_name(status) << endl; socket->setUVError("UDP send error", status); } delete sr; }
void UDPSocket::init() { if (ptr()) return; TraceS(this) << "Init" << endl; uv_udp_t* udp = new uv_udp_t; udp->data = this; //instance(); _closed = false; _ptr = reinterpret_cast<uv_handle_t*>(udp); int r = uv_udp_init(loop(), udp); if (r) setUVError("Cannot initialize UDP socket", r); }
void TCPSocket::init() { if (ptr()) return; TraceLS(this) << "Init" << endl; auto tcp = new uv_tcp_t; tcp->data = this; _ptr = reinterpret_cast<uv_handle_t*>(tcp); _closed = false; _error.reset(); int r = uv_tcp_init(loop(), tcp); if (r) setUVError("Cannot initialize TCP socket", r); }
void TCPSocket::onConnect(uv_connect_t* handle, int status) { TraceLS(this) << "On connect" << endl; // Error handled by static callback proxy if (status == 0) { if (readStart()) onSocketConnect(); } else { setUVError("Connection failed", status); //ErrorLS(this) << "Connection failed: " << error().message << endl; } delete handle; }
int UDPSocket::send(const char* data, std::size_t len, const Address& peerAddress, int /* flags */) { TraceS(this) << "Send: " << len << ": " << peerAddress << endl; assert(Thread::currentID() == tid()); // assert(len <= net::MAX_UDP_PACKET_SIZE); if (_peer.valid() && _peer != peerAddress) { ErrorS(this) << "Peer not authorized: " << peerAddress << endl; return -1; } if (!peerAddress.valid()) { ErrorS(this) << "Peer not valid: " << peerAddress << endl; return -1; } int r; auto sr = new internal::SendRequest; sr->buf = uv_buf_init((char*)data, len); // TODO: memcpy data? r = uv_udp_send(&sr->req, ptr<uv_udp_t>(), &sr->buf, 1, peerAddress.addr(), UDPSocket::afterSend); #if 0 switch (peerAddress.af()) { case AF_INET: r = uv_udp_send(&sr->req, ptr<uv_udp_t>(), &sr->buf, 1, peerAddress.addr(), UDPSocket::afterSend); break; case AF_INET6: r = uv_udp_send6(&sr->req, ptr<uv_udp_t>(), &sr->buf, 1, *reinterpret_cast<const sockaddr_in6*>(peerAddress.addr()), UDPSocket::afterSend); break; default: throw std::runtime_error("Unexpected address family"); } #endif if (r) { ErrorS(this) << "Send failed: " << uv_err_name(r) << endl; setUVError("Invalid UDP socket", r); } // R is -1 on error, otherwise return len return r ? r : len; }
void UDPSocket::onRecv(uv_udp_t* handle, ssize_t nread, const uv_buf_t* buf, const struct sockaddr* addr, unsigned /* flags */) { auto socket = static_cast<UDPSocket*>(handle->data); TraceL << "On recv: " << nread << endl; if (nread < 0) { //assert(0 && "unexpected error"); TraceL << "Recv error: " << uv_err_name(nread)<< endl; socket->setUVError("UDP error", nread); return; } if (nread == 0) { assert(addr == NULL); // Returning unused buffer, this is not an error // 11/12/13: This happens on linux but not windows //socket->setUVError("End of file", UV_EOF); return; } socket->onRecv(mutableBuffer(buf->base, nread), net::Address(addr, sizeof(*addr))); }
void TCPSocket::setSimultaneousAccepts(bool enable) { init(); int r = uv_tcp_simultaneous_accepts(ptr<uv_tcp_t>(), enable ? 1 : 0); if (r) setUVError("TCP socket error", r); }
void TCPSocket::setKeepAlive(int enable, unsigned int delay) { init(); int r = uv_tcp_keepalive(ptr<uv_tcp_t>(), enable, delay); if (r) setUVError("TCP socket error", r); }
void TCPSocket::setNoDelay(bool enable) { init(); int r = uv_tcp_nodelay(ptr<uv_tcp_t>(), enable ? 1 : 0); if (r) setUVError("TCP socket error", r); }
void Handle::setAndThrowError(const std::string& prefix, int errorno) { setUVError(prefix, errorno); throwError(prefix, errorno); }