Lines Matching refs:s
67 for (asocket* s : local_socket_list) { in find_local_socket()
68 if (s->id != local_id) { in find_local_socket()
71 if (peer_id == 0 || (s->peer && s->peer->id == peer_id)) { in find_local_socket()
72 result = s; in find_local_socket()
80 void install_local_socket(asocket* s) { in install_local_socket() argument
83 s->id = local_socket_next_id++; in install_local_socket()
90 local_socket_list.push_back(s); in install_local_socket()
93 void remove_socket(asocket* s) { in remove_socket() argument
96 list->erase(std::remove_if(list->begin(), list->end(), [s](asocket* x) { return x == s; }), in remove_socket()
107 for (asocket* s : local_socket_list) { in close_all_sockets()
108 if (s->transport == t || (s->peer && s->peer->transport == t)) { in close_all_sockets()
109 s->close(s); in close_all_sockets()
121 static SocketFlushResult local_socket_flush_incoming(asocket* s) { in local_socket_flush_incoming() argument
122 if (!s->packet_queue.empty()) { in local_socket_flush_incoming()
123 std::vector<adb_iovec> iov = s->packet_queue.iovecs(); in local_socket_flush_incoming()
124 ssize_t rc = adb_writev(s->fd, iov.data(), iov.size()); in local_socket_flush_incoming()
125 if (rc > 0 && static_cast<size_t>(rc) == s->packet_queue.size()) { in local_socket_flush_incoming()
126 s->packet_queue.clear(); in local_socket_flush_incoming()
128 s->packet_queue.drop_front(rc); in local_socket_flush_incoming()
129 fdevent_add(s->fde, FDE_WRITE); in local_socket_flush_incoming()
132 fdevent_add(s->fde, FDE_WRITE); in local_socket_flush_incoming()
137 s->has_write_error = true; in local_socket_flush_incoming()
142 if (s->closing) { in local_socket_flush_incoming()
143 s->close(s); in local_socket_flush_incoming()
147 fdevent_del(s->fde, FDE_WRITE); in local_socket_flush_incoming()
152 static bool local_socket_flush_outgoing(asocket* s) { in local_socket_flush_outgoing() argument
153 const size_t max_payload = s->get_max_payload(); in local_socket_flush_outgoing()
162 r = adb_read(s->fd, x, avail); in local_socket_flush_outgoing()
163 D("LS(%d): post adb_read(fd=%d,...) r=%d (errno=%d) avail=%zu", s->id, s->fd, r, in local_socket_flush_outgoing()
179 D("LS(%d): fd=%d post avail loop. r=%d is_eof=%d forced_eof=%d", s->id, s->fd, r, is_eof, in local_socket_flush_outgoing()
180 s->fde->force_eof); in local_socket_flush_outgoing()
182 if (avail != max_payload && s->peer) { in local_socket_flush_outgoing()
187 unsigned saved_id = s->id; in local_socket_flush_outgoing()
188 int saved_fd = s->fd; in local_socket_flush_outgoing()
189 r = s->peer->enqueue(s->peer, std::move(data)); in local_socket_flush_outgoing()
207 fdevent_del(s->fde, FDE_READ); in local_socket_flush_outgoing()
212 if ((s->fde->force_eof && !r) || is_eof) { in local_socket_flush_outgoing()
213 D(" closing because is_eof=%d r=%d s->fde.force_eof=%d", is_eof, r, s->fde->force_eof); in local_socket_flush_outgoing()
214 s->close(s); in local_socket_flush_outgoing()
221 static int local_socket_enqueue(asocket* s, apacket::payload_type data) { in local_socket_enqueue() argument
222 D("LS(%d): enqueue %zu", s->id, data.size()); in local_socket_enqueue()
224 s->packet_queue.append(std::move(data)); in local_socket_enqueue()
225 switch (local_socket_flush_incoming(s)) { in local_socket_enqueue()
236 return !s->packet_queue.empty(); in local_socket_enqueue()
239 static void local_socket_ready(asocket* s) { in local_socket_ready() argument
242 fdevent_add(s->fde, FDE_READ); in local_socket_ready()
272 if (duration > 1s) { in deferred_close()
293 fdevent_set_timeout(fde, 1s); in deferred_close()
297 static void local_socket_destroy(asocket* s) { in local_socket_destroy() argument
298 int exit_on_close = s->exit_on_close; in local_socket_destroy()
300 D("LS(%d): destroying fde.fd=%d", s->id, s->fd); in local_socket_destroy()
302 deferred_close(fdevent_release(s->fde)); in local_socket_destroy()
304 remove_socket(s); in local_socket_destroy()
305 delete s; in local_socket_destroy()
313 static void local_socket_close(asocket* s) { in local_socket_close() argument
314 D("entered local_socket_close. LS(%d) fd=%d", s->id, s->fd); in local_socket_close()
316 if (s->peer) { in local_socket_close()
317 D("LS(%d): closing peer. peer->id=%d peer->fd=%d", s->id, s->peer->id, s->peer->fd); in local_socket_close()
322 if (s->peer->shutdown) { in local_socket_close()
323 s->peer->shutdown(s->peer); in local_socket_close()
325 s->peer->peer = nullptr; in local_socket_close()
326 s->peer->close(s->peer); in local_socket_close()
327 s->peer = nullptr; in local_socket_close()
333 if (s->closing || s->has_write_error || s->packet_queue.empty()) { in local_socket_close()
334 int id = s->id; in local_socket_close()
335 local_socket_destroy(s); in local_socket_close()
342 D("LS(%d): closing", s->id); in local_socket_close()
343 s->closing = 1; in local_socket_close()
344 fdevent_del(s->fde, FDE_READ); in local_socket_close()
345 remove_socket(s); in local_socket_close()
346 D("LS(%d): put on socket_closing_list fd=%d", s->id, s->fd); in local_socket_close()
347 local_socket_closing_list.push_back(s); in local_socket_close()
348 CHECK_EQ(FDE_WRITE, s->fde->state & FDE_WRITE); in local_socket_close()
352 asocket* s = reinterpret_cast<asocket*>(_s); in local_socket_event_func() local
353 D("LS(%d): event_func(fd=%d(==%d), ev=%04x)", s->id, s->fd, fd, ev); in local_socket_event_func()
359 switch (local_socket_flush_incoming(s)) { in local_socket_event_func()
367 s->peer->ready(s->peer); in local_socket_event_func()
373 if (!local_socket_flush_outgoing(s)) { in local_socket_event_func()
383 D("LS(%d): FDE_ERROR (fd=%d)", s->id, s->fd); in local_socket_event_func()
390 asocket* s = new asocket(); in create_local_socket() local
391 s->fd = fd; in create_local_socket()
392 s->enqueue = local_socket_enqueue; in create_local_socket()
393 s->ready = local_socket_ready; in create_local_socket()
394 s->shutdown = nullptr; in create_local_socket()
395 s->close = local_socket_close; in create_local_socket()
396 install_local_socket(s); in create_local_socket()
398 s->fde = fdevent_create(fd, local_socket_event_func, s); in create_local_socket()
399 D("LS(%d): created (fd=%d)", s->id, s->fd); in create_local_socket()
400 return s; in create_local_socket()
405 if (asocket* s = daemon_service_to_socket(name); s) { in create_local_service_socket() local
406 return s; in create_local_service_socket()
415 asocket* s = create_local_socket(std::move(fd)); in create_local_service_socket() local
416 LOG(VERBOSE) << "LS(" << s->id << "): bound to '" << name << "' via " << fd_value; in create_local_service_socket()
422 D("LS(%d): enabling exit_on_close", s->id); in create_local_service_socket()
423 s->exit_on_close = 1; in create_local_service_socket()
427 return s; in create_local_service_socket()
430 static int remote_socket_enqueue(asocket* s, apacket::payload_type data) { in remote_socket_enqueue() argument
431 D("entered remote_socket_enqueue RS(%d) WRITE fd=%d peer.fd=%d", s->id, s->fd, s->peer->fd); in remote_socket_enqueue()
435 p->msg.arg0 = s->peer->id; in remote_socket_enqueue()
436 p->msg.arg1 = s->id; in remote_socket_enqueue()
446 send_packet(p, s->transport); in remote_socket_enqueue()
450 static void remote_socket_ready(asocket* s) { in remote_socket_ready() argument
451 D("entered remote_socket_ready RS(%d) OKAY fd=%d peer.fd=%d", s->id, s->fd, s->peer->fd); in remote_socket_ready()
454 p->msg.arg0 = s->peer->id; in remote_socket_ready()
455 p->msg.arg1 = s->id; in remote_socket_ready()
456 send_packet(p, s->transport); in remote_socket_ready()
459 static void remote_socket_shutdown(asocket* s) { in remote_socket_shutdown() argument
460 D("entered remote_socket_shutdown RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd, in remote_socket_shutdown()
461 s->peer ? s->peer->fd : -1); in remote_socket_shutdown()
464 if (s->peer) { in remote_socket_shutdown()
465 p->msg.arg0 = s->peer->id; in remote_socket_shutdown()
467 p->msg.arg1 = s->id; in remote_socket_shutdown()
468 send_packet(p, s->transport); in remote_socket_shutdown()
471 static void remote_socket_close(asocket* s) { in remote_socket_close() argument
472 if (s->peer) { in remote_socket_close()
473 s->peer->peer = nullptr; in remote_socket_close()
474 D("RS(%d) peer->close()ing peer->id=%d peer->fd=%d", s->id, s->peer->id, s->peer->fd); in remote_socket_close()
475 s->peer->close(s->peer); in remote_socket_close()
477 D("entered remote_socket_close RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd, in remote_socket_close()
478 s->peer ? s->peer->fd : -1); in remote_socket_close()
479 D("RS(%d): closed", s->id); in remote_socket_close()
480 delete s; in remote_socket_close()
491 asocket* s = new asocket(); in create_remote_socket() local
492 s->id = id; in create_remote_socket()
493 s->enqueue = remote_socket_enqueue; in create_remote_socket()
494 s->ready = remote_socket_ready; in create_remote_socket()
495 s->shutdown = remote_socket_shutdown; in create_remote_socket()
496 s->close = remote_socket_close; in create_remote_socket()
497 s->transport = t; in create_remote_socket()
499 D("RS(%d): created", s->id); in create_remote_socket()
500 return s; in create_remote_socket()
503 void connect_to_remote(asocket* s, std::string_view destination) { in connect_to_remote() argument
504 D("Connect_to_remote call RS(%d) fd=%d", s->id, s->fd); in connect_to_remote()
507 LOG(VERBOSE) << "LS(" << s->id << ": connect(" << destination << ")"; in connect_to_remote()
509 p->msg.arg0 = s->id; in connect_to_remote()
518 CHECK_LE(p->msg.data_length, s->get_max_payload()); in connect_to_remote()
520 send_packet(p, s->transport); in connect_to_remote()
526 static void local_socket_ready_notify(asocket* s) { in local_socket_ready_notify() argument
527 s->ready = local_socket_ready; in local_socket_ready_notify()
528 s->shutdown = nullptr; in local_socket_ready_notify()
529 s->close = local_socket_close; in local_socket_ready_notify()
530 SendOkay(s->fd); in local_socket_ready_notify()
531 s->ready(s); in local_socket_ready_notify()
537 static void local_socket_close_notify(asocket* s) { in local_socket_close_notify() argument
538 s->ready = local_socket_ready; in local_socket_close_notify()
539 s->shutdown = nullptr; in local_socket_close_notify()
540 s->close = local_socket_close; in local_socket_close_notify()
541 SendFail(s->fd, "closed"); in local_socket_close_notify()
542 s->close(s); in local_socket_close_notify()
545 static unsigned unhex(const char* s, int len) { in unhex() argument
549 switch ((c = *s++)) { in unhex()
716 static int smart_socket_enqueue(asocket* s, apacket::payload_type data) { in smart_socket_enqueue() argument
722 D("SS(%d): enqueue %zu", s->id, data.size()); in smart_socket_enqueue()
724 if (s->smart_socket_data.empty()) { in smart_socket_enqueue()
726 s->smart_socket_data.assign(data.begin(), data.end()); in smart_socket_enqueue()
728 std::copy(data.begin(), data.end(), std::back_inserter(s->smart_socket_data)); in smart_socket_enqueue()
732 if (s->smart_socket_data.size() < 4) { in smart_socket_enqueue()
736 uint32_t len = unhex(s->smart_socket_data.data(), 4); in smart_socket_enqueue()
738 D("SS(%d): bad size (%u)", s->id, len); in smart_socket_enqueue()
742 D("SS(%d): len is %u", s->id, len); in smart_socket_enqueue()
744 if ((len + 4) > s->smart_socket_data.size()) { in smart_socket_enqueue()
745 D("SS(%d): waiting for %zu more bytes", s->id, len + 4 - s->smart_socket_data.size()); in smart_socket_enqueue()
749 s->smart_socket_data[len + 4] = 0; in smart_socket_enqueue()
751 D("SS(%d): '%s'", s->id, (char*)(s->smart_socket_data.data() + 4)); in smart_socket_enqueue()
753 service = std::string_view(s->smart_socket_data).substr(4); in smart_socket_enqueue()
759 LOG(ERROR) << "SS(" << s->id << "): failed to parse host service: " << service; in smart_socket_enqueue()
764 LOG(ERROR) << "SS(" << s->id << "): failed to parse host transport id: " << service; in smart_socket_enqueue()
768 LOG(ERROR) << "SS(" << s->id << "): host-transport-id without command"; in smart_socket_enqueue()
788 s->peer->fd, s); in smart_socket_enqueue()
792 LOG(VERBOSE) << "SS(" << s->id << "): handled host service '" << service << "'"; in smart_socket_enqueue()
796 D("SS(%d): okay transport", s->id); in smart_socket_enqueue()
797 s->smart_socket_data.clear(); in smart_socket_enqueue()
811 LOG(VERBOSE) << "SS(" << s->id << "): couldn't create host service '" << service << "'"; in smart_socket_enqueue()
812 SendFail(s->peer->fd, "unknown host service"); in smart_socket_enqueue()
823 SendOkay(s->peer->fd); in smart_socket_enqueue()
825 s->peer->ready = local_socket_ready; in smart_socket_enqueue()
826 s->peer->shutdown = nullptr; in smart_socket_enqueue()
827 s->peer->close = local_socket_close; in smart_socket_enqueue()
828 s->peer->peer = s2; in smart_socket_enqueue()
829 s2->peer = s->peer; in smart_socket_enqueue()
830 s->peer = nullptr; in smart_socket_enqueue()
831 D("SS(%d): okay", s->id); in smart_socket_enqueue()
832 s->close(s); in smart_socket_enqueue()
839 if (!s->transport) { in smart_socket_enqueue()
840 SendFail(s->peer->fd, "device offline (no transport)"); in smart_socket_enqueue()
842 } else if (!ConnectionStateIsOnline(s->transport->GetConnectionState())) { in smart_socket_enqueue()
846 SendFail(s->peer->fd, "device offline (transport offline)"); in smart_socket_enqueue()
855 s->peer->ready = local_socket_ready_notify; in smart_socket_enqueue()
856 s->peer->shutdown = nullptr; in smart_socket_enqueue()
857 s->peer->close = local_socket_close_notify; in smart_socket_enqueue()
858 s->peer->peer = nullptr; in smart_socket_enqueue()
860 s->peer->transport = s->transport; in smart_socket_enqueue()
862 connect_to_remote(s->peer, std::string_view(s->smart_socket_data).substr(4)); in smart_socket_enqueue()
863 s->peer = nullptr; in smart_socket_enqueue()
864 s->close(s); in smart_socket_enqueue()
872 s->close(s); in smart_socket_enqueue()
876 static void smart_socket_ready(asocket* s) { in smart_socket_ready() argument
877 D("SS(%d): ready", s->id); in smart_socket_ready()
880 static void smart_socket_close(asocket* s) { in smart_socket_close() argument
881 D("SS(%d): closed", s->id); in smart_socket_close()
882 if (s->peer) { in smart_socket_close()
883 s->peer->peer = nullptr; in smart_socket_close()
884 s->peer->close(s->peer); in smart_socket_close()
885 s->peer = nullptr; in smart_socket_close()
887 delete s; in smart_socket_close()
892 asocket* s = new asocket(); in create_smart_socket() local
893 s->enqueue = smart_socket_enqueue; in create_smart_socket()
894 s->ready = smart_socket_ready; in create_smart_socket()
895 s->shutdown = nullptr; in create_smart_socket()
896 s->close = smart_socket_close; in create_smart_socket()
898 D("SS(%d)", s->id); in create_smart_socket()
899 return s; in create_smart_socket()
902 void connect_to_smartsocket(asocket* s) { in connect_to_smartsocket() argument
905 s->peer = ss; in connect_to_smartsocket()
906 ss->peer = s; in connect_to_smartsocket()
907 s->ready(s); in connect_to_smartsocket()