Lines Matching refs:s
61 asocket* s; in find_local_socket() local
65 for (s = local_socket_list.next; s != &local_socket_list; s = s->next) { in find_local_socket()
66 if (s->id != local_id) { in find_local_socket()
69 if (peer_id == 0 || (s->peer && s->peer->id == peer_id)) { in find_local_socket()
70 result = s; in find_local_socket()
78 static void insert_local_socket(asocket* s, asocket* list) { in insert_local_socket() argument
79 s->next = list; in insert_local_socket()
80 s->prev = s->next->prev; in insert_local_socket()
81 s->prev->next = s; in insert_local_socket()
82 s->next->prev = s; in insert_local_socket()
85 void install_local_socket(asocket* s) { in install_local_socket() argument
88 s->id = local_socket_next_id++; in install_local_socket()
95 insert_local_socket(s, &local_socket_list); in install_local_socket()
98 void remove_socket(asocket* s) { in remove_socket() argument
100 if (s->prev && s->next) { in remove_socket()
101 s->prev->next = s->next; in remove_socket()
102 s->next->prev = s->prev; in remove_socket()
103 s->next = 0; in remove_socket()
104 s->prev = 0; in remove_socket()
105 s->id = 0; in remove_socket()
110 asocket* s; in close_all_sockets() local
117 for (s = local_socket_list.next; s != &local_socket_list; s = s->next) { in close_all_sockets()
118 if (s->transport == t || (s->peer && s->peer->transport == t)) { in close_all_sockets()
119 s->close(s); in close_all_sockets()
125 static int local_socket_enqueue(asocket* s, apacket* p) { in local_socket_enqueue() argument
126 D("LS(%d): enqueue %d", s->id, p->len); in local_socket_enqueue()
134 if (s->pkt_first) { in local_socket_enqueue()
142 int r = adb_write(s->fd, p->ptr, p->len); in local_socket_enqueue()
149 D("LS(%d): not ready, errno=%d: %s", s->id, errno, strerror(errno)); in local_socket_enqueue()
151 s->has_write_error = true; in local_socket_enqueue()
152 s->close(s); in local_socket_enqueue()
166 if (s->pkt_first) { in local_socket_enqueue()
167 s->pkt_last->next = p; in local_socket_enqueue()
169 s->pkt_first = p; in local_socket_enqueue()
171 s->pkt_last = p; in local_socket_enqueue()
174 fdevent_add(&s->fde, FDE_WRITE); in local_socket_enqueue()
179 static void local_socket_ready(asocket* s) { in local_socket_ready() argument
182 fdevent_add(&s->fde, FDE_READ); in local_socket_ready()
186 static void local_socket_destroy(asocket* s) { in local_socket_destroy() argument
188 int exit_on_close = s->exit_on_close; in local_socket_destroy()
190 D("LS(%d): destroying fde.fd=%d", s->id, s->fde.fd); in local_socket_destroy()
195 fdevent_remove(&s->fde); in local_socket_destroy()
198 for (p = s->pkt_first; p; p = n) { in local_socket_destroy()
199 D("LS(%d): discarding %d bytes", s->id, p->len); in local_socket_destroy()
203 remove_socket(s); in local_socket_destroy()
204 free(s); in local_socket_destroy()
212 static void local_socket_close(asocket* s) { in local_socket_close() argument
213 D("entered local_socket_close. LS(%d) fd=%d", s->id, s->fd); in local_socket_close()
215 if (s->peer) { in local_socket_close()
216 D("LS(%d): closing peer. peer->id=%d peer->fd=%d", s->id, s->peer->id, s->peer->fd); in local_socket_close()
221 if (s->peer->shutdown) { in local_socket_close()
222 s->peer->shutdown(s->peer); in local_socket_close()
224 s->peer->peer = nullptr; in local_socket_close()
225 s->peer->close(s->peer); in local_socket_close()
226 s->peer = nullptr; in local_socket_close()
232 if (s->closing || s->has_write_error || s->pkt_first == NULL) { in local_socket_close()
233 int id = s->id; in local_socket_close()
234 local_socket_destroy(s); in local_socket_close()
241 D("LS(%d): closing", s->id); in local_socket_close()
242 s->closing = 1; in local_socket_close()
243 fdevent_del(&s->fde, FDE_READ); in local_socket_close()
244 remove_socket(s); in local_socket_close()
245 D("LS(%d): put on socket_closing_list fd=%d", s->id, s->fd); in local_socket_close()
246 insert_local_socket(s, &local_socket_closing_list); in local_socket_close()
247 CHECK_EQ(FDE_WRITE, s->fde.state & FDE_WRITE); in local_socket_close()
251 asocket* s = reinterpret_cast<asocket*>(_s); in local_socket_event_func() local
252 D("LS(%d): event_func(fd=%d(==%d), ev=%04x)", s->id, s->fd, fd, ev); in local_socket_event_func()
259 while ((p = s->pkt_first) != nullptr) { in local_socket_event_func()
276 s->has_write_error = true; in local_socket_event_func()
277 s->close(s); in local_socket_event_func()
282 s->pkt_first = p->next; in local_socket_event_func()
283 if (s->pkt_first == 0) { in local_socket_event_func()
284 s->pkt_last = 0; in local_socket_event_func()
293 if (s->closing) { in local_socket_event_func()
295 s->close(s); in local_socket_event_func()
303 fdevent_del(&s->fde, FDE_WRITE); in local_socket_event_func()
304 s->peer->ready(s->peer); in local_socket_event_func()
310 const size_t max_payload = s->get_max_payload(); in local_socket_event_func()
317 D("LS(%d): post adb_read(fd=%d,...) r=%d (errno=%d) avail=%zu", s->id, s->fd, r, in local_socket_event_func()
333 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_event_func()
334 s->fde.force_eof); in local_socket_event_func()
335 if ((avail == max_payload) || (s->peer == 0)) { in local_socket_event_func()
342 unsigned saved_id = s->id; in local_socket_event_func()
343 int saved_fd = s->fd; in local_socket_event_func()
344 r = s->peer->enqueue(s->peer, p); in local_socket_event_func()
364 fdevent_del(&s->fde, FDE_READ); in local_socket_event_func()
368 if ((s->fde.force_eof && !r) || is_eof) { in local_socket_event_func()
369 D(" closing because is_eof=%d r=%d s->fde.force_eof=%d", is_eof, r, s->fde.force_eof); in local_socket_event_func()
370 s->close(s); in local_socket_event_func()
380 D("LS(%d): FDE_ERROR (fd=%d)", s->id, s->fd); in local_socket_event_func()
386 asocket* s = reinterpret_cast<asocket*>(calloc(1, sizeof(asocket))); in create_local_socket() local
387 if (s == NULL) { in create_local_socket()
390 s->fd = fd; in create_local_socket()
391 s->enqueue = local_socket_enqueue; in create_local_socket()
392 s->ready = local_socket_ready; in create_local_socket()
393 s->shutdown = NULL; in create_local_socket()
394 s->close = local_socket_close; in create_local_socket()
395 install_local_socket(s); in create_local_socket()
397 fdevent_install(&s->fde, fd, local_socket_event_func, s); in create_local_socket()
398 D("LS(%d): created (fd=%d)", s->id, s->fd); in create_local_socket()
399 return s; in create_local_socket()
416 asocket* s = create_local_socket(fd); in create_local_service_socket() local
417 D("LS(%d): bound to '%s' via %d", s->id, name, fd); in create_local_service_socket()
429 D("LS(%d): enabling exit_on_close", s->id); in create_local_service_socket()
430 s->exit_on_close = 1; in create_local_service_socket()
434 return s; in create_local_service_socket()
439 asocket* s; in create_host_service_socket() local
441 s = host_service_to_socket(name, serial); in create_host_service_socket()
443 if (s != NULL) { in create_host_service_socket()
444 D("LS(%d) bound to '%s'", s->id, name); in create_host_service_socket()
445 return s; in create_host_service_socket()
448 return s; in create_host_service_socket()
452 static int remote_socket_enqueue(asocket* s, apacket* p) { in remote_socket_enqueue() argument
453 D("entered remote_socket_enqueue RS(%d) WRITE fd=%d peer.fd=%d", s->id, s->fd, s->peer->fd); in remote_socket_enqueue()
455 p->msg.arg0 = s->peer->id; in remote_socket_enqueue()
456 p->msg.arg1 = s->id; in remote_socket_enqueue()
458 send_packet(p, s->transport); in remote_socket_enqueue()
462 static void remote_socket_ready(asocket* s) { in remote_socket_ready() argument
463 D("entered remote_socket_ready RS(%d) OKAY fd=%d peer.fd=%d", s->id, s->fd, s->peer->fd); in remote_socket_ready()
466 p->msg.arg0 = s->peer->id; in remote_socket_ready()
467 p->msg.arg1 = s->id; in remote_socket_ready()
468 send_packet(p, s->transport); in remote_socket_ready()
471 static void remote_socket_shutdown(asocket* s) { in remote_socket_shutdown() argument
472 D("entered remote_socket_shutdown RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd, in remote_socket_shutdown()
473 s->peer ? s->peer->fd : -1); in remote_socket_shutdown()
476 if (s->peer) { in remote_socket_shutdown()
477 p->msg.arg0 = s->peer->id; in remote_socket_shutdown()
479 p->msg.arg1 = s->id; in remote_socket_shutdown()
480 send_packet(p, s->transport); in remote_socket_shutdown()
483 static void remote_socket_close(asocket* s) { in remote_socket_close() argument
484 if (s->peer) { in remote_socket_close()
485 s->peer->peer = 0; in remote_socket_close()
486 D("RS(%d) peer->close()ing peer->id=%d peer->fd=%d", s->id, s->peer->id, s->peer->fd); in remote_socket_close()
487 s->peer->close(s->peer); in remote_socket_close()
489 D("entered remote_socket_close RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd, in remote_socket_close()
490 s->peer ? s->peer->fd : -1); in remote_socket_close()
491 D("RS(%d): closed", s->id); in remote_socket_close()
492 free(s); in remote_socket_close()
503 asocket* s = reinterpret_cast<asocket*>(calloc(1, sizeof(asocket))); in create_remote_socket() local
505 if (s == NULL) { in create_remote_socket()
508 s->id = id; in create_remote_socket()
509 s->enqueue = remote_socket_enqueue; in create_remote_socket()
510 s->ready = remote_socket_ready; in create_remote_socket()
511 s->shutdown = remote_socket_shutdown; in create_remote_socket()
512 s->close = remote_socket_close; in create_remote_socket()
513 s->transport = t; in create_remote_socket()
515 D("RS(%d): created", s->id); in create_remote_socket()
516 return s; in create_remote_socket()
519 void connect_to_remote(asocket* s, const char* destination) { in connect_to_remote() argument
520 D("Connect_to_remote call RS(%d) fd=%d", s->id, s->fd); in connect_to_remote()
524 if (len > (s->get_max_payload() - 1)) { in connect_to_remote()
528 D("LS(%d): connect('%s')", s->id, destination); in connect_to_remote()
530 p->msg.arg0 = s->id; in connect_to_remote()
533 send_packet(p, s->transport); in connect_to_remote()
538 static void local_socket_ready_notify(asocket* s) { in local_socket_ready_notify() argument
539 s->ready = local_socket_ready; in local_socket_ready_notify()
540 s->shutdown = NULL; in local_socket_ready_notify()
541 s->close = local_socket_close; in local_socket_ready_notify()
542 SendOkay(s->fd); in local_socket_ready_notify()
543 s->ready(s); in local_socket_ready_notify()
549 static void local_socket_close_notify(asocket* s) { in local_socket_close_notify() argument
550 s->ready = local_socket_ready; in local_socket_close_notify()
551 s->shutdown = NULL; in local_socket_close_notify()
552 s->close = local_socket_close; in local_socket_close_notify()
553 SendFail(s->fd, "closed"); in local_socket_close_notify()
554 s->close(s); in local_socket_close_notify()
557 static unsigned unhex(unsigned char* s, int len) { in unhex() argument
561 switch ((c = *s++)) { in unhex()
650 static int smart_socket_enqueue(asocket* s, apacket* p) { in smart_socket_enqueue() argument
658 D("SS(%d): enqueue %d", s->id, p->len); in smart_socket_enqueue()
660 if (s->pkt_first == 0) { in smart_socket_enqueue()
661 s->pkt_first = p; in smart_socket_enqueue()
662 s->pkt_last = p; in smart_socket_enqueue()
664 if ((s->pkt_first->len + p->len) > s->get_max_payload()) { in smart_socket_enqueue()
665 D("SS(%d): overflow", s->id); in smart_socket_enqueue()
670 memcpy(s->pkt_first->data + s->pkt_first->len, p->data, p->len); in smart_socket_enqueue()
671 s->pkt_first->len += p->len; in smart_socket_enqueue()
674 p = s->pkt_first; in smart_socket_enqueue()
684 D("SS(%d): bad size (%d)", s->id, len); in smart_socket_enqueue()
688 D("SS(%d): len is %d", s->id, len); in smart_socket_enqueue()
691 D("SS(%d): waiting for %d more bytes", s->id, len + 4 - p->len); in smart_socket_enqueue()
697 D("SS(%d): '%s'", s->id, (char*)(p->data + 4)); in smart_socket_enqueue()
733 if (handle_host_request(service, type, serial, s->peer->fd, s) == 0) { in smart_socket_enqueue()
735 D("SS(%d): handled host service '%s'", s->id, service); in smart_socket_enqueue()
739 D("SS(%d): okay transport", s->id); in smart_socket_enqueue()
750 D("SS(%d): couldn't create host service '%s'", s->id, service); in smart_socket_enqueue()
751 SendFail(s->peer->fd, "unknown host service"); in smart_socket_enqueue()
762 SendOkay(s->peer->fd); in smart_socket_enqueue()
764 s->peer->ready = local_socket_ready; in smart_socket_enqueue()
765 s->peer->shutdown = nullptr; in smart_socket_enqueue()
766 s->peer->close = local_socket_close; in smart_socket_enqueue()
767 s->peer->peer = s2; in smart_socket_enqueue()
768 s2->peer = s->peer; in smart_socket_enqueue()
769 s->peer = 0; in smart_socket_enqueue()
770 D("SS(%d): okay", s->id); in smart_socket_enqueue()
771 s->close(s); in smart_socket_enqueue()
778 if (s->transport == nullptr) { in smart_socket_enqueue()
780 s->transport = acquire_one_transport(kTransportAny, nullptr, nullptr, &error_msg); in smart_socket_enqueue()
781 if (s->transport == nullptr) { in smart_socket_enqueue()
782 SendFail(s->peer->fd, error_msg); in smart_socket_enqueue()
788 if (!(s->transport) || (s->transport->connection_state == kCsOffline)) { in smart_socket_enqueue()
792 SendFail(s->peer->fd, "device offline (x)"); in smart_socket_enqueue()
801 s->peer->ready = local_socket_ready_notify; in smart_socket_enqueue()
802 s->peer->shutdown = nullptr; in smart_socket_enqueue()
803 s->peer->close = local_socket_close_notify; in smart_socket_enqueue()
804 s->peer->peer = 0; in smart_socket_enqueue()
806 s->peer->transport = s->transport; in smart_socket_enqueue()
808 connect_to_remote(s->peer, (char*)(p->data + 4)); in smart_socket_enqueue()
809 s->peer = 0; in smart_socket_enqueue()
810 s->close(s); in smart_socket_enqueue()
818 s->close(s); in smart_socket_enqueue()
822 static void smart_socket_ready(asocket* s) { in smart_socket_ready() argument
823 D("SS(%d): ready", s->id); in smart_socket_ready()
826 static void smart_socket_close(asocket* s) { in smart_socket_close() argument
827 D("SS(%d): closed", s->id); in smart_socket_close()
828 if (s->pkt_first) { in smart_socket_close()
829 put_apacket(s->pkt_first); in smart_socket_close()
831 if (s->peer) { in smart_socket_close()
832 s->peer->peer = 0; in smart_socket_close()
833 s->peer->close(s->peer); in smart_socket_close()
834 s->peer = 0; in smart_socket_close()
836 free(s); in smart_socket_close()
841 asocket* s = reinterpret_cast<asocket*>(calloc(1, sizeof(asocket))); in create_smart_socket() local
842 if (s == NULL) fatal("cannot allocate socket"); in create_smart_socket()
843 s->enqueue = smart_socket_enqueue; in create_smart_socket()
844 s->ready = smart_socket_ready; in create_smart_socket()
845 s->shutdown = NULL; in create_smart_socket()
846 s->close = smart_socket_close; in create_smart_socket()
848 D("SS(%d)", s->id); in create_smart_socket()
849 return s; in create_smart_socket()
852 void connect_to_smartsocket(asocket* s) { in connect_to_smartsocket() argument
855 s->peer = ss; in connect_to_smartsocket()
856 ss->peer = s; in connect_to_smartsocket()
857 s->ready(s); in connect_to_smartsocket()