NEINT32 ne_connector_openex(ne_handle net_handle, NEINT8 *host, NEINT32 port) { if(net_handle->nodetype == NE_TCP){ struct ne_tcp_node* socket_addr=(struct ne_tcp_node*)net_handle ; socket_addr->myerrno = NEERR_SUCCESS ; if(-1 == ne_tcpnode_connect(host, port,socket_addr ) ) { socket_addr->myerrno = NEERR_OPENFILE ; return -1; } // ne_socket_nonblock(socket_addr->fd,0); ne_tcpnode_sendlock_init(socket_addr) ; // socket_addr->write_entry = (write_packet_entry)_tcp_connector_send ; return 0 ; } else { ne_udt_node* socket_addr = (ne_udt_node*)net_handle ; ne_netui_handle n_handle = (ne_netui_handle) udt_connect(host, (NEINT16)port,socket_addr) ; if(n_handle){ u_32 val = 1 ; udt_ioctl(socket_addr,UDT_IOCTRL_DRIVER_MOD,&val); //val =(NE_UDT_DATAGRAM==protocol) ; val = 0 ; udt_ioctl(socket_addr,UDT_IOCTRL_SET_STREAM_TPYE,&val) ; udt_init_sendlock(socket_addr) ; //client socket need send lock // udt_set_nonblock(socket_addr,0); return 0 ; } socket_addr->myerrno = NEERR_OPENFILE ; return -1 ; } }
static PRStatus _udt_connect(PRFileDesc *fd, const PRNetAddr *addr, PRIntervalTime timeout){ ///fprintf(stdout, "%s:%d\n", __func__, __LINE__); UDTSOCKET s = get_socket_from_fd(fd); struct sockaddr_in peer_addr; peer_addr.sin_family = AF_INET;///(addr->raw.family == PR_AF_INET6)? AF_INET6:AF_INET; peer_addr.sin_port = addr->inet.port; peer_addr.sin_addr.s_addr = addr->inet.ip; bool sync = true, old; int optlen; if (udt_getsockopt(s, SOL_SOCKET, UDT_UDT_RCVSYN, &old, &optlen) < 0) return PR_FAILURE; if (udt_setsockopt(s, SOL_SOCKET, UDT_UDT_RCVSYN, &sync, sizeof(sync)) < 0) return PR_FAILURE; if (udt_connect(s, (sockaddr*)&peer_addr, sizeof(peer_addr)) == 0) { // check for sync connect if (udt_getsockstate(s) == UDT_CONNECTED) { if (udt_setsockopt(s, SOL_SOCKET, UDT_UDT_RCVSYN, &old, sizeof(old)) < 0) return PR_FAILURE; return PR_SUCCESS; } else { return PR_FAILURE; } } else { return PR_FAILURE; } }
static PRStatus _udt_connect(PRFileDesc *fd, const PRNetAddr *addr, PRIntervalTime timeout){ UDTSOCKET s = get_socket_from_fd(fd); struct sockaddr_in peer_addr; peer_addr.sin_family = AF_INET; peer_addr.sin_port = addr->inet.port; peer_addr.sin_addr.s_addr = addr->inet.ip; if (UDT_ERRSUCCESS != udt_connect(s, (sockaddr*)&peer_addr, sizeof(peer_addr))) return PR_FAILURE; return PR_SUCCESS; }
int main(int argc, char* argv[]) { if ((3 != argc) || (0 == atoi(argv[2]))) { printf("usage: client server_ip server_port\n"); return -1; } if (udt_startup() != 0) { printf("udt : startup failed:%s\n", udt_getlasterror_desc()); return -1; } struct addrinfo hints, *local, *peer; memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_flags = AI_PASSIVE; hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; //hints.ai_socktype = SOCK_DGRAM; if (0 != getaddrinfo(NULL, argv[2], &hints, &local)) { printf("incorrect network address.\n"); return -1; } UDTSOCKET client = udt_socket(local->ai_family, local->ai_socktype, local->ai_protocol); // UDT Options //UDT::setsockopt(client, 0, UDT_CC, new CCCFactory<CUDPBlast>, sizeof(CCCFactory<CUDPBlast>)); //UDT::setsockopt(client, 0, UDT_MSS, new int(9000), sizeof(int)); //UDT::setsockopt(client, 0, UDT_SNDBUF, new int(10000000), sizeof(int)); //UDT::setsockopt(client, 0, UDP_SNDBUF, new int(10000000), sizeof(int)); //UDT::setsockopt(client, 0, UDT_MAXBW, new int64_t(12500000), sizeof(int)); // for rendezvous connection, enable the code below /* UDT::setsockopt(client, 0, UDT_RENDEZVOUS, new bool(true), sizeof(bool)); if (UDT::ERROR == UDT::bind(client, local->ai_addr, local->ai_addrlen)) { cout << "bind: " << UDT::getlasterror().getErrorMessage() << endl; return 0; } */ freeaddrinfo(local); if (0 != getaddrinfo(argv[1], argv[2], &hints, &peer)) { printf("incorrect server/peer address. %s:%s\n", argv[1], argv[2]); return -1; } // connect to the server, implict bind if (UDT_ERROR == udt_connect(client, peer->ai_addr, peer->ai_addrlen)) { printf("connect: %s\n", udt_getlasterror_desc()); return 0; } freeaddrinfo(peer); // using CC method //CUDPBlast* cchandle = NULL; //int temp; //UDT::getsockopt(client, 0, UDT_CC, &cchandle, &temp); //if (NULL != cchandle) // cchandle->setRate(500); char* data = malloc(sizeof(char) * DATA_LEN); memset(data, 'a', DATA_LEN); struct timeval t1, t2; gettimeofday(&t1, NULL); for (int i = 0; i < N_SEND; i ++) { //printf("i = %d\n", i); if (UDT_ERROR == udt_send(client, data, DATA_LEN, 0)) { printf("send:%s\n", udt_getlasterror_desc()); break; } } gettimeofday(&t2, NULL); int elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0; // sec to ms elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0; // us to ms printf("finished to write data in %d milliseconds\n", elapsedTime); udt_close(client); free(data); if (udt_cleanup() != 0) { printf("cleanup:%s", udt_getlasterror_desc()); } return 0; }
bool process(params_t* args) { ipk_sock_t udt = IPK_SOCK_INVALID; rdt_t* rdt = NULL; #ifndef _WIN32 if (!ipk_fd_block_mode(STDIN_FILENO, false)) { return false; } #endif udt = udt_init(args->src_port); if (!udt_connect(udt, args->dst_port)) { goto return_false; } rdt = rdt_new(); if (!rdt) { goto return_false; } if (!rdt_client_start(rdt)) { goto return_false; } bool eof = false; char buf[BUF_MAXLEN]; char rcvbuf[UDT_PACKET_MAXLEN]; char sndbuf[UDT_PACKET_MAXLEN]; size_t nbuf = 0; size_t nrcvbuf = 0; size_t nsndbuf = 0; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 100000; #ifdef _WIN32 int nfds = 0; // ignored on win32 #else int nfds = max(STDIN_FILENO, udt) + 1; #endif fd_set rfds, wfds; while (!_terminate) { FD_ZERO(&rfds); FD_ZERO(&wfds); if (nsndbuf == 0) { nsndbuf = _countof(sndbuf); rdt_client_step(rdt, rcvbuf, nrcvbuf, sndbuf, &nsndbuf); } else { rdt_client_step(rdt, rcvbuf, nrcvbuf, NULL, NULL); } nrcvbuf = 0; FD_SET(udt, &rfds); if (nsndbuf > 0) { FD_SET(udt, &wfds); } #ifndef _WIN32 if (!eof && nbuf < _countof(buf)) { FD_SET(STDIN_FILENO, &rfds); } #endif if (nbuf > 0 && rdt_data_write(rdt, buf, nbuf)) { nbuf = 0; } else if (eof) { if (rdt_client_done(rdt)) break; } int numfd = select(nfds, &rfds, &wfds, NULL, &tv); if (numfd == -1) { ipk_sock_perror(ipk_sock_errno); } else if (numfd == 0) { continue; } else { #ifndef _WIN32 if (FD_ISSET(STDIN_FILENO, &rfds)) { #else if (!eof) { #endif size_t erecv = _countof(buf) - nbuf; size_t nrecv = fread(buf + nbuf, sizeof(char), erecv, stdin); _ipk_verbose_log("Readed %zu from stdin.", nrecv); nbuf += nrecv; if (nrecv != erecv) { if (feof(stdin)) { eof = true; } else if (ferror(stdin) && !ipk_sock_is_recoverable(errno)) { ipk_perror(errno); // send RST } } } if (FD_ISSET(udt, &wfds)) { int nsnd = udt_send(udt, sndbuf, nsndbuf); if (nsnd == -1) { ipk_sock_perror(ipk_sock_errno); goto return_false; } else if (nsnd > 0) { nsndbuf = 0; sndbuf[nsnd] = '\0'; _ipk_debug_log("#%x <<<\n%s", udt, sndbuf); _ipk_debug_log("#%x /<<<", udt); } else { // send again } } if (FD_ISSET(udt, &rfds)) { int nrcv = udt_recv(udt, rcvbuf, _countof(rcvbuf)); if (nrcv == -1) { ipk_sock_perror(ipk_sock_errno); goto return_false; } else if (nrcv > 0) { nrcvbuf = nrcv; rcvbuf[nrcv] = '\0'; _ipk_debug_log("#%x >>>\n%s", udt, rcvbuf); _ipk_debug_log("#%x />>>", udt); } else { // read again } } } } rdt_dispose(rdt); ipk_sock_close(udt); return true; return_false: rdt_dispose(rdt); ipk_sock_close(udt); return false; } int main(int argc, char* argv[]) { params_t params; memset(¶ms, '\0', sizeof(params_t)); setlocale(LC_ALL, ""); ipk_set_log_func(&mylog); ipk_set_log_level(1); if (!parseargs(argc, argv, ¶ms)) { _ipk_error_log("%s", "Chyba: nebyl zadan povinny parametr."); fprintf(stdout, "%s", "Synopsis: rdtclient -s source_port -d dest_port\n"); return EXIT_FAILURE; } ipk_set_log_level(params.verbose_lvl); if (params.action == ACT_HELP) { fprintf(stdout, "%s", "Spolehliva komunikace, projekt c. 3 pro predmet IPK.\n" "Autor: Radek Sevcik, [email protected]\n" "\n" "Synopsis: rdtclient -s source_port -d dest_port\n" "\n" "Parametry:\n" " -h\t\t\tVypise napovedu.\n" " -s source_port\tNasloucha na portu source_port.\n" " -d dest_port\t\tPripoji se na port dest_port.\n"); return EXIT_SUCCESS; } // sigint (ctrl+c), sigterm #ifdef _WIN32 if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE)&on_console_handler, true)) { ipk_perror_win(GetLastError()); return EXIT_FAILURE; } #else if (SIG_ERR == signal(SIGINT, &on_signal)) { ipk_perror(errno); return EXIT_FAILURE; } if (SIG_ERR == signal(SIGTERM, &on_signal)) { ipk_perror(errno); return EXIT_FAILURE; } #endif if (!ipk_sock_init()) { return EXIT_FAILURE; } if (0 != atexit(&cleanup)) { _ipk_error_log("%s", "Chyba: nedostatek pameti"); return EXIT_FAILURE; } return process(¶ms) ? EXIT_SUCCESS : EXIT_FAILURE; }