const char* aio_stream::get_peer(bool full /* = false */) const { if (stream_ == NULL) return dummy_; ACL_VSTREAM* vs = acl_aio_vstream(stream_); const char* ptr = ACL_VSTREAM_PEER(vs); if (ptr == NULL || *ptr == 0) { char buf[256]; ACL_SOCKET fd = ACL_VSTREAM_SOCK(vs); if (acl_getpeername(fd, buf, sizeof(buf)) == -1) return dummy_; acl_vstream_set_peer(vs, buf); } ptr = ACL_VSTREAM_PEER(vs); if (full) return ptr; else if (peer_ip_[0] != 0) return peer_ip_; return const_cast<aio_stream*> (this)->get_ip(ptr, const_cast<aio_stream*>(this)->peer_ip_, sizeof(peer_ip_)); }
const char* socket_stream::get_peer_ip() const { if (stream_ == NULL) return dummy_; if (peer_ip_[0] != 0) return peer_ip_; char* ptr = ACL_VSTREAM_PEER(stream_); if (ptr == NULL || *ptr == 0) { char buf[64]; if (acl_getpeername(ACL_VSTREAM_SOCK(stream_), buf, sizeof(buf)) == -1) { return dummy_; } acl_vstream_set_peer(stream_, buf); } return const_cast<socket_stream*> (this)->get_ip( ACL_VSTREAM_PEER(stream_), const_cast<socket_stream*> (this)->peer_ip_, sizeof(peer_ip_)); }
http_status_t read_respond_hdr(void) { acl_assert(vstream_); snprintf(data_.i_addr, sizeof(data_.i_addr), "%s", ACL_VSTREAM_PEER(vstream_)); hdr_res_ = http_hdr_res_new(); int ret = http_hdr_res_get_sync(hdr_res_, vstream_, 60); if (ret < 0) { logger_error("get http respond hdr from %s error %s", data_.i_addr, acl_last_serror()); http_hdr_res_free(hdr_res_); hdr_res_ = NULL; return (HTTP_ERR_READ_HDR); } if (http_hdr_res_parse(hdr_res_) < 0) { logger_error("parse http respond hdr error from %s", data_.i_addr); http_hdr_res_free(hdr_res_); hdr_res_ = NULL; return (HTTP_ERR_INVALID_HDR); } return (HTTP_OK); }
const char* socket_stream::get_peer(bool full /* = false */) const { if (stream_ == NULL) return dummy_; // xxx: acl_vstream 中没有对此地址赋值 char* ptr = ACL_VSTREAM_PEER(stream_); if (ptr == NULL || *ptr == 0) { char buf[64]; if (acl_getpeername(ACL_VSTREAM_SOCK(stream_), buf, sizeof(buf)) == -1) { return dummy_; } acl_vstream_set_peer(stream_, buf); } if (full) return ACL_VSTREAM_PEER(stream_); else return get_peer_ip(); }
static void run(const char *local_addr, const char *peer_addr, int count, int dlen, int inter, int need_read, int quit) { double spent; int ret, i; char buf[4096], data[4096]; struct timeval begin, end; ACL_VSTREAM *stream = acl_vstream_bind(local_addr, 2); /* 绑定 UDP 套接口 */ if (stream == NULL) { printf("acl_vstream_bind %s error %s\r\n", local_addr, acl_last_serror()); return; } if (dlen > (int) sizeof(data) - 1) dlen = (int) sizeof(data) - 1; for (i = 0; i < dlen; i++) data[i] = 'X'; data[dlen] = 0; gettimeofday(&begin, NULL); acl_vstream_set_peer(stream, peer_addr); ACL_VSTREAM_SET_RWTIMO(stream, 1); for (i = 0; i < count; i++) { /* 如果服务端的地址是变化的,则应该在写每次前都需要调用 * acl_vstream_set_peer */ ret = acl_vstream_write(stream, data, dlen); if (ret == ACL_VSTREAM_EOF) { printf("acl_vtream_write error %s\r\n", acl_last_serror()); break; } if (need_read) { ret = acl_vstream_read(stream, buf, sizeof(buf) - 1); if (ret == ACL_VSTREAM_EOF) { if (errno == ETIMEDOUT) { printf("timeout read\r\n"); continue; } printf("acl_vstream_read error %s\r\n", acl_last_serror()); break; } else buf[ret] = 0; if (i % inter == 0) printf("result: %s\r\n", buf); } if (i % inter == 0) { snprintf(buf, sizeof(buf), "total: %d, curr: %d", count, i); ACL_METER_TIME(buf); } } gettimeofday(&end, NULL); spent = stamp_sub(&end, &begin); printf("thread: %lu, total: %d, curr: %d, spent: %.2f, speed: %.2f\r\n", (unsigned long) acl_pthread_self(), count, i, spent, (i * 1000) / (spent > 1 ? spent : 1)); printf("thread: %lu, local addr: %s, peer addr: %s\r\n", (unsigned long) acl_pthread_self(), ACL_VSTREAM_LOCAL(stream), ACL_VSTREAM_PEER(stream)); if (quit) acl_vstream_write(stream, "quit", 4); acl_vstream_close(stream); }
static void enable_connect(EVENT_KERNEL *ev, ACL_EVENT_FDTABLE *fdp) { const char *myname = "enable_connect"; DWORD SentLen = 0; struct sockaddr_in addr; unsigned short port; char *local_ip, *remote, buf[256]; ACL_SOCKET sock = ACL_VSTREAM_SOCK(fdp->stream); LPFN_CONNECTEX lpfnConnectEx = NULL; GUID GuidConnectEx = WSAID_CONNECTEX; int dwErr, dwBytes; static char *any_ip = "0.0.0.0"; memset(&fdp->event_write->overlapped, 0, sizeof(fdp->event_write->overlapped)); ACL_SAFE_STRNCPY(buf, ACL_VSTREAM_PEER(fdp->stream), sizeof(buf)); parse_addr(buf, &port, &local_ip, &remote); if (!local_ip || !*local_ip) local_ip = any_ip; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = inet_addr(local_ip); addr.sin_port = htons(0); if (bind(sock, (struct sockaddr *) &addr, sizeof(struct sockaddr)) < 0) { acl_msg_fatal("%s(%d): bind local ip(%s) error(%s, %d), sock: %d", myname, __LINE__, local_ip, acl_last_serror(), acl_last_error(), (int) sock); } memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons((short) port); addr.sin_addr.s_addr = inet_addr(remote); dwErr = WSAIoctl(sock, SIO_GET_EXTENSION_FUNCTION_POINTER, &GuidConnectEx, sizeof(GuidConnectEx), &lpfnConnectEx, sizeof(lpfnConnectEx), &dwBytes, NULL, NULL); if(dwErr == SOCKET_ERROR) acl_msg_fatal("%s(%d): WSAIoctl error(%s)", myname, __LINE__, acl_last_serror()); if (lpfnConnectEx(sock, (const struct sockaddr *) &addr, sizeof(struct sockaddr), NULL, 0, NULL, &fdp->event_write->overlapped) == FALSE && acl_last_error() !=ERROR_IO_PENDING) { acl_msg_warn("%s(%d): ConnectEx error(%s), sock(%d)", myname, __LINE__, acl_last_serror(), sock); } }