template <class key_t, class elem_t> void hash_index<key_t, elem_t>::remove( index_handle<key_t, elem_t> the_handle) { hash_bin_type<key_t, elem_t> *this_bin = (hash_bin_type<key_t, elem_t> *)(from_handle(the_handle)); assert(this_bin != 0); if (this_bin->next != 0) { hash_bin_type<key_t, elem_t> *old_bin = this_bin->next; this_bin->the_key = old_bin->the_key; this_bin->the_elem = old_bin->the_elem; this_bin->next = old_bin->next; old_bin->next = 0; hash_bin_type<key_t, elem_t>::kill(old_bin); } else { size_t bin_num = (*_hash_function)(this_bin->the_key, _table_size); hash_bin_type<key_t, elem_t> *first_bin = _table[bin_num]; assert(first_bin != 0); if (first_bin == this_bin) { _table[bin_num] = 0; } else { this_bin->the_key = first_bin->the_key; this_bin->the_elem = first_bin->the_elem; _table[bin_num] = first_bin->next; first_bin->next = 0; } hash_bin_type<key_t, elem_t>::kill(first_bin); } }
sock_result_t socket_send_ex(sock_handle_t sd, const void* buffer, socklen_t len, uint32_t flags, system_tick_t timeout, void* reserved) { sock_result_t result = SOCKET_INVALID; socket_t* socket = from_handle(sd); uint16_t bytes_sent = 0; if (is_open(socket)) { std::lock_guard<socket_t> lk(*socket); wiced_result_t wiced_result = WICED_TCPIP_INVALID_SOCKET; if (is_tcp(socket)) { wiced_tcp_send_flags_t wiced_flags = timeout == 0 ? WICED_TCP_SEND_FLAG_NONBLOCK : WICED_TCP_SEND_FLAG_NONE; bytes_sent = (uint16_t)len; wiced_result = wiced_tcp_send_buffer_ex(tcp(socket), buffer, &bytes_sent, wiced_flags, timeout); } else if (is_client(socket)) { tcp_server_client_t* server_client = client(socket); size_t written = 0; wiced_result = server_client->write(buffer, len, &written, flags, timeout); bytes_sent = (uint16_t)written; } if (!wiced_result) DEBUG("Write %d bytes to socket %d result=%d", (int)len, (int)sd, wiced_result); result = wiced_result ? as_sock_result(wiced_result) : bytes_sent; } return result; }
/** * Connects the given socket to the address. * @param sd The socket handle to connect * @param addr The address to connect to * @param addrlen The length of the address details. * @return 0 on success. */ sock_result_t socket_connect(sock_handle_t sd, const sockaddr_t *addr, long addrlen) { wiced_result_t result = WICED_INVALID_SOCKET; socket_t* socket = from_handle(sd); tcp_socket_t* tcp_socket = tcp(socket); if (tcp_socket) { std::lock_guard<socket_t> lk(*socket); result = wiced_tcp_bind(tcp_socket, WICED_ANY_PORT); if (result==WICED_SUCCESS) { // WICED callbacks are broken //wiced_tcp_register_callbacks(tcp(socket), socket_t::notify_connected, socket_t::notify_received, socket_t::notify_disconnected, (void*)socket); SOCKADDR_TO_PORT_AND_IPADDR(addr, addr_data, port, ip_addr); unsigned timeout = 5*1000; result = wiced_tcp_connect(tcp_socket, &ip_addr, port, timeout); if (result==WICED_SUCCESS) { tcp_socket->connected(); } else { // Work around WICED bug that doesn't set connection handler to NULL after deleting // it, leading to deleting the same memory twice and a crash // WICED/network/LwIP/WICED/tcpip.c:920 tcp_socket->conn_handler = NULL; } } } return as_sock_result(result); }
template <class key_base_t, class elem_t> void ts_ptr_index<key_base_t, elem_t>::remove( index_handle<key_base_t *, elem_t> the_handle) { index_handle<const char *, elem_t> tsi_handle; tsi_handle.set_raw_referenced_item(from_handle(the_handle)); _tsi.remove(tsi_handle); }
template <class key_base_t, class elem_t> elem_t ts_ptr_index<key_base_t, elem_t>::elem( index_handle<key_base_t *, elem_t> the_handle) const { index_handle<const char *, elem_t> tsi_handle; tsi_handle.set_raw_referenced_item(from_handle(the_handle)); return _tsi.elem(tsi_handle); }
template <class key_t, class elem_t> elem_t alist_index<key_t, elem_t>:: elem(index_handle<key_t, elem_t> the_handle) const { alist_holder<key_t, elem_t> *holder = (alist_holder<key_t, elem_t> *)(from_handle(the_handle)); assert(holder != 0); return holder->data.the_elem; }
template <class key_t, class elem_t> elem_t hash_index<key_t, elem_t>::elem( index_handle<key_t, elem_t> the_handle) const { hash_bin_type<key_t, elem_t> *this_bin = (hash_bin_type<key_t, elem_t> *)(from_handle(the_handle)); assert(this_bin != 0); return this_bin->the_elem; }
template <class elem_t, class holder_t> void cdlist_tos_base<elem_t, holder_t>::set_elem_by_handle( tos_handle<elem_t> the_handle, elem_t the_elem) { holder_t *elem_ptr = (holder_t *)(from_handle(the_handle)); if (elem_ptr == 0) return; elem_ptr->data = the_elem; }
template <class key_t, class elem_t> void alist_index<key_t, elem_t>:: remove(index_handle<key_t, elem_t> the_handle) { alist_holder<key_t, elem_t> *holder = (alist_holder<key_t, elem_t> *)(from_handle(the_handle)); assert(holder != 0); tos_handle<alist_item<key_t, elem_t> > list_handle = holder; _list.remove(list_handle); }
template <class elem_t, class holder_t> tos_handle<elem_t> adlist_tos_base<elem_t, holder_t>::next_handle( tos_handle<elem_t> the_handle) const { holder_t *elem_ptr = (holder_t *)(from_handle(the_handle)); if (elem_ptr == 0) return build_handle(0); else return build_handle(elem_ptr->next); }
template <class elem_t, class holder_t> tos_handle<elem_t> cdlist_tos_base<elem_t, holder_t>::previous_handle( tos_handle<elem_t> the_handle) const { holder_t *elem_ptr = (holder_t *)(from_handle(the_handle)); if (elem_ptr == 0) return this->build_handle(0); else return this->build_handle(elem_ptr->previous); }
void *odp_shm_addr(odp_shm_t shm) { uint32_t i; i = from_handle(shm); if (i > (ODP_CONFIG_SHM_BLOCKS - 1)) return NULL; return odp_shm_tbl->block[i].addr; }
sock_result_t socket_shutdown(sock_handle_t sd, int how) { sock_result_t result = WICED_ERROR; socket_t* socket = from_handle(sd); if (socket && is_open(socket) && is_tcp(socket)) { std::lock_guard<socket_t> lk(*socket); result = wiced_tcp_close_shutdown(tcp(socket), (wiced_tcp_shutdown_flags_t)how); LOG_DEBUG(TRACE, "socket shutdown %x %x", sd, how); } return result; }
template <class elem_t, class holder_t> elem_t cdlist_tos_base<elem_t, holder_t>::elem_by_handle( tos_handle<elem_t> the_handle) const { holder_t *elem_ptr = (holder_t *)(from_handle(the_handle)); if (elem_ptr == 0) return zero((elem_t *)0); else return elem_ptr->data; }
/** * Discards a previously allocated socket. If the socket is already invalid, returns silently. * Once a socket has been passed to the client, this is the only time the object is * deleted. Since the client initiates this call, the client is aware can the * socket is no longer valid. * @param handle The handle to discard. * @return SOCKET_INVALID always. */ sock_handle_t socket_dispose(sock_handle_t handle) { if (socket_handle_valid(handle)) { socket_t* socket = from_handle(handle); SocketList& list = list_for(socket); /* IMPORTANT: SocketListLock is acquired first */ SocketListLock lock(list); std::lock_guard<socket_t> lk(*socket); if (list.remove(socket)) delete socket; } return SOCKET_INVALID; }
/** * Closes the socket handle. * @param sock * @return */ sock_result_t socket_close(sock_handle_t sock) { sock_result_t result = WICED_SUCCESS; socket_t* socket = from_handle(sock); if (socket) { { std::lock_guard<socket_t> lk(*socket); socket->close(); } socket_dispose(sock); LOG_DEBUG(TRACE, "socket closed %x", int(sock)); } return result; }
template <class elem_t, class holder_t> void adlist_tos_base<elem_t, holder_t>::insert_before( tos_handle<elem_t> the_handle, elem_t the_elem) { holder_t *elem_ptr = (holder_t *)(from_handle(the_handle)); if (elem_ptr == 0) return; extend_array(); holder_t *new_e = new holder_t(the_elem, elem_ptr, elem_ptr->previous); elem_ptr->previous = new_e; if (_head_e == elem_ptr) _head_e = new_e; else new_e->previous->next = new_e; _index_size = 0; ++_cached_size; }
template <class elem_t, class holder_t> void cdlist_tos_base<elem_t, holder_t>::insert_after( tos_handle<elem_t> the_handle, elem_t the_elem) { holder_t *elem_ptr = (holder_t *)(from_handle(the_handle)); if (elem_ptr == 0) return; holder_t *new_e = new holder_t(the_elem, elem_ptr->next); elem_ptr->next = new_e; if (_tail_e == elem_ptr) _tail_e = new_e; else new_e->next->previous = new_e; _cached_e = _head_e; _cached_index = 0; ++_cached_size; }
int odp_shm_info(odp_shm_t shm, odp_shm_info_t *info) { odp_shm_block_t *block; uint32_t i; i = from_handle(shm); if (i > (ODP_CONFIG_SHM_BLOCKS - 1)) return -1; block = &odp_shm_tbl->block[i]; info->name = block->name; info->addr = block->addr; info->size = block->size; info->page_size = block->page_sz; info->flags = block->flags; return 0; }
/** * Fetch the next waiting client socket from the server * @param sock * @return */ sock_result_t socket_accept(sock_handle_t sock) { sock_result_t result = SOCKET_INVALID; socket_t* socket = from_handle(sock); if (is_open(socket) && is_server(socket)) { std::lock_guard<socket_t> lk(*socket); tcp_server_t* server = socket->s.tcp_server; tcp_server_client_t* client = server->next_accept(); if (client) { socket_t* socket = new socket_t(); socket->set_client(client); { SocketListLock lock(list_for(socket)); add_socket(socket); } result = (sock_result_t)socket; } } return result; }
/** * Receives data from a socket. * @param sd * @param buffer * @param len * @param _timeout * @return The number of bytes read. -1 if the end of the stream is reached. */ sock_result_t socket_receive(sock_handle_t sd, void* buffer, socklen_t len, system_tick_t _timeout) { sock_result_t bytes_read = -1; socket_t* socket = from_handle(sd); if (is_open(socket)) { std::lock_guard<socket_t> lk(*socket); if (is_tcp(socket)) { tcp_socket_t* tcp_socket = tcp(socket); tcp_packet_t& packet = tcp_socket->packet; bytes_read = read_packet_and_dispose(packet, buffer, len, tcp_socket, _timeout); } else if (is_client(socket)) { tcp_server_client_t* server_client = client(socket); bytes_read = read_packet_and_dispose(server_client->packet, buffer, len, server_client->get_socket(), _timeout); } } if (bytes_read<0) DEBUG("socket_receive on %d returned %d", sd, bytes_read); return bytes_read; }
template <class elem_t, class holder_t> void adlist_tos_base<elem_t, holder_t>::remove( tos_handle<elem_t> the_handle) { holder_t *elem_ptr = (holder_t *)(from_handle(the_handle)); if (elem_ptr == 0) return; if (_head_e == elem_ptr) _head_e = elem_ptr->next; else elem_ptr->previous->next = elem_ptr->next; if (_tail_e == elem_ptr) _tail_e = elem_ptr->previous; else elem_ptr->next->previous = elem_ptr->previous; elem_ptr->remove_ref(); if (elem_ptr->delete_me()) delete elem_ptr; _index_size = 0; --_cached_size; }
sock_result_t socket_receivefrom(sock_handle_t sd, void* buffer, socklen_t bufLen, uint32_t flags, sockaddr_t* addr, socklen_t* addrsize) { socket_t* socket = from_handle(sd); volatile wiced_result_t result = WICED_INVALID_SOCKET; uint16_t read_len = 0; if (is_open(socket) && is_udp(socket)) { std::lock_guard<socket_t> lk(*socket); wiced_packet_t* packet = NULL; // UDP receive timeout changed to 0 sec so as not to block if ((result=wiced_udp_receive(udp(socket), &packet, WICED_NO_WAIT))==WICED_SUCCESS) { wiced_ip_address_t wiced_ip_addr; uint16_t port; if ((result=wiced_udp_packet_get_info(packet, &wiced_ip_addr, &port))==WICED_SUCCESS) { uint32_t ipv4 = GET_IPV4_ADDRESS(wiced_ip_addr); addr->sa_data[0] = (port>>8) & 0xFF; addr->sa_data[1] = port & 0xFF; addr->sa_data[2] = (ipv4 >> 24) & 0xFF; addr->sa_data[3] = (ipv4 >> 16) & 0xFF; addr->sa_data[4] = (ipv4 >> 8) & 0xFF; addr->sa_data[5] = ipv4 & 0xFF; result=read_packet(packet, (uint8_t*)buffer, bufLen, &read_len); }
sock_result_t socket_sendto(sock_handle_t sd, const void* buffer, socklen_t len, uint32_t flags, sockaddr_t* addr, socklen_t addr_size) { socket_t* socket = from_handle(sd); wiced_result_t result = WICED_INVALID_SOCKET; if (is_open(socket) && is_udp(socket)) { std::lock_guard<socket_t> lk(*socket); SOCKADDR_TO_PORT_AND_IPADDR(addr, addr_data, port, ip_addr); uint16_t available = 0; wiced_packet_t* packet = NULL; uint8_t* data; if ((result=wiced_packet_create_udp(udp(socket), len, &packet, &data, &available))==WICED_SUCCESS) { size_t size = std::min(available, uint16_t(len)); memcpy(data, buffer, size); /* Set the end of the data portion */ wiced_packet_set_data_end(packet, (uint8_t*) data + size); result = wiced_udp_send(udp(socket), &ip_addr, port, packet); len = size; } } // return negative value on error, or length if successful. return result ? -result : len; }
int odp_shm_free(odp_shm_t shm) { uint32_t i; int ret; odp_shm_block_t *block; char name[ODP_SHM_NAME_LEN + 8]; if (shm == ODP_SHM_INVALID) { ODP_DBG("odp_shm_free: Invalid handle\n"); return -1; } i = from_handle(shm); if (i >= ODP_CONFIG_SHM_BLOCKS) { ODP_DBG("odp_shm_free: Bad handle\n"); return -1; } odp_spinlock_lock(&odp_shm_tbl->lock); block = &odp_shm_tbl->block[i]; if (block->addr == NULL) { ODP_DBG("odp_shm_free: Free block\n"); odp_spinlock_unlock(&odp_shm_tbl->lock); return 0; } /* right now, for this tpye of memory, we do nothing as free */ if (block->flags & ODP_SHM_MONOPOLIZE_CNTNUS_PHY) { int pid = getpid(); snprintf(name, sizeof(name), "%s_%d", block->name, pid); odp_mm_district_unreserve(name); memset(block, 0, sizeof(odp_shm_block_t)); odp_spinlock_unlock(&odp_shm_tbl->lock); return 0; } if (block->flags & ODP_SHM_SHARE_CNTNUS_PHY) { odp_mm_district_unreserve(name); memset(block, 0, sizeof(odp_shm_block_t)); odp_spinlock_unlock(&odp_shm_tbl->lock); return 0; } ret = munmap(block->addr_orig, block->alloc_size); if (0 != ret) { ODP_DBG("odp_shm_free: munmap failed: %s, id %u, addr %p\n", strerror(errno), i, block->addr_orig); odp_spinlock_unlock(&odp_shm_tbl->lock); return -1; } if (block->flags & ODP_SHM_PROC) { ret = shm_unlink(block->name); if (0 != ret) { ODP_DBG("odp_shm_free: shm_unlink failed\n"); odp_spinlock_unlock(&odp_shm_tbl->lock); return -1; } } memset(block, 0, sizeof(odp_shm_block_t)); odp_spinlock_unlock(&odp_shm_tbl->lock); return 0; }
// Initialize from std handle. void from_stdin() { from_handle(stdin_handle()); m_file_name = "[stdin]"; }
void from_stdout() { from_handle(stdout_handle()); m_file_name = "[stdout]"; }
void from_stderr() { from_handle(stderr_handle()); m_file_name = "[stderr]"; }
/** * Determines if a given socket is bound. * @param sd The socket handle to test * @return non-zero if bound, 0 otherwise. */ uint8_t socket_active_status(sock_handle_t sd) { socket_t* socket = from_handle(sd); return (socket && socket->isOpen()) ? SOCKET_STATUS_ACTIVE : SOCKET_STATUS_INACTIVE; }