static int socket_callback(CURL *e, curl_socket_t s, int what, void *data, void *socket_ptr) { asyn_http_object* aho = (asyn_http_object*)data; int* action_ptr = (int*)socket_ptr; auto it = socket_map.find(s); if (it == socket_map.end()) { if (action_ptr) { log_wrap::net().e("function : ", __FUNCTION__, " socket closed already before remove CURL_POLL_REMOVE event, may be bug? socket id : ", s); return 0; } else { log_wrap::net().e("we don't know how to create asio::ip::tcp::socket without this fd's protocol family, please recompiled libcurl without c-ares"); return 0; } } std::shared_ptr<socket_info> &tcp_socket = it->second; if (!action_ptr) { action_ptr = &(tcp_socket->mask); curl_multi_assign(aho->curlm, s, action_ptr); } if (what == CURL_POLL_REMOVE) { } else { //set socket set_socket(tcp_socket, s, e, what, data); } *action_ptr = what; return 0; }
int main() { int sid = 0, bid = 0, fp; char *send_data = (char *)malloc(1024*sizeof(char)); char temp[1024]; char *receive_data = (char *)malloc(1024*sizeof(char)); int fd, count, cnt=0; struct sockaddr_in server_socket, client_socket; int size = sizeof(client_socket); if((sid = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { printf("Connection error..\n"); exit(1); } set_socket(&server_socket, AF_INET, 6000, INADDR_ANY); if((bid = bind(sid, (struct sockaddr *)&server_socket, sizeof(struct sockaddr))) == -1) { printf("Binding error..\n"); exit(1); } printf("I am waiting for client..\n"); recvfrom(sid, receive_data, 1024, 0,(struct sockaddr *)&client_socket, &size); printf("received data is : %s\n", receive_data); fd = open(receive_data, O_RDONLY); printf("size = %ld\n", sizeof(send_data)); while((count=read(fd, temp, 500)) != 0) { printf("I am inside the loop : %d\n", cnt++); serialize(send_data, count, temp); printf("Serialized : %s\n", send_data); sendto(sid, send_data, 1024, 0, (struct sockaddr *)&client_socket, size); } printf("I am outside the loop : %d\n", count); strcpy(temp, "ENDOFFILE"); serialize(send_data, sizeof(temp), temp); sendto(sid, send_data, 1024, 0, (struct sockaddr *)&client_socket, size); fcloseall(); close(sid); close(fd); return 0; }
int ConnectionServer::create_listen(int &socket_fd, unsigned short port) { sockaddr_in addr; memset(&addr, 0, sizeof addr); addr.sin_family = AF_INET; addr.sin_addr.s_addr = INADDR_ANY; addr.sin_port = htons(port); if ((socket_fd = socket(PF_INET, SOCK_STREAM, 0)) == -1) return -1; if (set_socket(socket_fd, O_NONBLOCK)) return -1; if (bind(socket_fd, (const sockaddr*)&addr, sizeof addr)) return -1; if (listen(socket_fd, MAX_FD_NUM)) return -1; return 0; }
static void event_callback(void* data, std::shared_ptr<socket_info> tcp_socket, curl_socket_t curl_socket, CURL* curl, int action, const boost::system::error_code &err) { asyn_http_object* aho = (asyn_http_object*)data; if (err) { log_wrap::net().e("function : ", __FUNCTION__, ", socket : ", curl_socket, ", action : ", action, " error : ", err.message()); CURLMcode code = curl_multi_socket_action(aho->curlm, tcp_socket->socket.native_handle(), CURL_CSELECT_ERR, &aho->still_running); } else { CURLMcode code = curl_multi_socket_action(aho->curlm, tcp_socket->socket.native_handle(), action, &aho->still_running); } check_multi_curl(data); if (aho->still_running <= 0) { timer.cancel(); } else { int action_continue = (tcp_socket->mask) & action; if (action_continue) { set_socket(tcp_socket, curl_socket, curl, action_continue, data); } } }
SolarisAttachOperation(char* name) : AttachOperation(name) { set_socket(-1); set_next(NULL); }
int ConnectionServer::Svc() { int fd,new_fd; _info("ConnectionServer::Svc() begin !\n"); while (!_stop_task) { pthread_mutex_lock(&_epoll_mutex); if (_stop_task) { pthread_mutex_unlock(&_epoll_mutex); _debug("ConnectionServer::Svc() get exit pipe signal !\n"); break; } if (_epoll_ready_event_num <= 0){ //fprintf(stderr,"ConnectionServer::Svc() #### wait !\n"); _epoll_ready_event_num = epoll_wait(_epoll_fd, _epoll_ready_event, MAX_EPOLL_EVENT_NUM, -1); } if (_epoll_ready_event_num-- < 0) { pthread_mutex_unlock(&_epoll_mutex); if (errno == EINTR){ _debug("ConnectionServer::Svc() errno == EINTR !\n"); continue; }else{ //_debug("ConnectionServer::Svc() errno == EINTR else !\n"); break; } } //fprintf(stderr,"ConnectionServer::Svc() #### 1 !\n"); fd = _epoll_ready_event[_epoll_ready_event_num].data.fd; if (fd == _socket_server_listen) { while ((new_fd = accept(fd, NULL, NULL)) >= 0) { pthread_mutex_lock(&_connections_mutex); if(_now_connections>=MAX_FD_NUM) { _debug("new connection error,because the max connection(%d) is achieved\n",MAX_FD_NUM); ::close(new_fd); //pthread_mutex_unlock(&_connections_mutex); } else if(new_fd >= MAX_FD_NUM) { _debug("new connection error,because the max fd(%d) is achieved\n",new_fd); ::close(new_fd); pthread_mutex_unlock(&_connections_mutex); } else { _now_connections++; pthread_mutex_unlock(&_connections_mutex); set_socket(new_fd, O_NONBLOCK); //create_handle(new_fd,LISTEN_PORT_1); CreateConnection(new_fd,READABLE_SOCKET); add_input_fd(new_fd); //WEBSEARCH_DEBUG((LM_DEBUG,"[new connection on socket(%d)]\n", new_fd)); } } pthread_mutex_unlock(&_epoll_mutex); //fprintf(stderr,"ConnectionServer::Svc() fd == _socket_server_listen continue !\n"); continue; } if (fd == _pipe_read) { pthread_mutex_unlock(&_epoll_mutex); continue; } del_input_fd(fd); pthread_mutex_unlock(&_epoll_mutex); Connection* conn = GetConnection(fd); if (conn && impl_ && !(impl_->Run((void*)conn)<0)){ add_input_fd(fd); }else { if (conn){ _err("Connection error!!\n"); ReleaseConnection(conn); } } } _info("ConnectionServer::Svc() exit !\n"); return 0; }
LinuxAttachOperation(char* name) : AttachOperation(name) { set_socket(-1); }