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 %zu", 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 %zu 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()
424 D("LS(%d): enabling exit_on_close", s->id); in create_local_service_socket()
425 s->exit_on_close = 1; in create_local_service_socket()
429 return s; in create_local_service_socket()
434 asocket* s; in create_host_service_socket() local
436 s = host_service_to_socket(name, serial); in create_host_service_socket()
438 if (s != NULL) { in create_host_service_socket()
439 D("LS(%d) bound to '%s'", s->id, name); in create_host_service_socket()
440 return s; in create_host_service_socket()
443 return s; in create_host_service_socket()
447 static int remote_socket_enqueue(asocket* s, apacket* p) { in remote_socket_enqueue() argument
448 D("entered remote_socket_enqueue RS(%d) WRITE fd=%d peer.fd=%d", s->id, s->fd, s->peer->fd); in remote_socket_enqueue()
450 p->msg.arg0 = s->peer->id; in remote_socket_enqueue()
451 p->msg.arg1 = s->id; in remote_socket_enqueue()
453 send_packet(p, s->transport); in remote_socket_enqueue()
457 static void remote_socket_ready(asocket* s) { in remote_socket_ready() argument
458 D("entered remote_socket_ready RS(%d) OKAY fd=%d peer.fd=%d", s->id, s->fd, s->peer->fd); in remote_socket_ready()
461 p->msg.arg0 = s->peer->id; in remote_socket_ready()
462 p->msg.arg1 = s->id; in remote_socket_ready()
463 send_packet(p, s->transport); in remote_socket_ready()
466 static void remote_socket_shutdown(asocket* s) { in remote_socket_shutdown() argument
467 D("entered remote_socket_shutdown RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd, in remote_socket_shutdown()
468 s->peer ? s->peer->fd : -1); in remote_socket_shutdown()
471 if (s->peer) { in remote_socket_shutdown()
472 p->msg.arg0 = s->peer->id; in remote_socket_shutdown()
474 p->msg.arg1 = s->id; in remote_socket_shutdown()
475 send_packet(p, s->transport); in remote_socket_shutdown()
478 static void remote_socket_close(asocket* s) { in remote_socket_close() argument
479 if (s->peer) { in remote_socket_close()
480 s->peer->peer = 0; in remote_socket_close()
481 D("RS(%d) peer->close()ing peer->id=%d peer->fd=%d", s->id, s->peer->id, s->peer->fd); in remote_socket_close()
482 s->peer->close(s->peer); in remote_socket_close()
484 D("entered remote_socket_close RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd, in remote_socket_close()
485 s->peer ? s->peer->fd : -1); in remote_socket_close()
486 D("RS(%d): closed", s->id); in remote_socket_close()
487 free(s); in remote_socket_close()
498 asocket* s = reinterpret_cast<asocket*>(calloc(1, sizeof(asocket))); in create_remote_socket() local
500 if (s == NULL) { in create_remote_socket()
503 s->id = id; in create_remote_socket()
504 s->enqueue = remote_socket_enqueue; in create_remote_socket()
505 s->ready = remote_socket_ready; in create_remote_socket()
506 s->shutdown = remote_socket_shutdown; in create_remote_socket()
507 s->close = remote_socket_close; in create_remote_socket()
508 s->transport = t; in create_remote_socket()
510 D("RS(%d): created", s->id); in create_remote_socket()
511 return s; in create_remote_socket()
514 void connect_to_remote(asocket* s, const char* destination) { in connect_to_remote() argument
515 D("Connect_to_remote call RS(%d) fd=%d", s->id, s->fd); in connect_to_remote()
519 if (len > (s->get_max_payload() - 1)) { in connect_to_remote()
523 D("LS(%d): connect('%s')", s->id, destination); in connect_to_remote()
525 p->msg.arg0 = s->id; in connect_to_remote()
528 send_packet(p, s->transport); in connect_to_remote()
533 static void local_socket_ready_notify(asocket* s) { in local_socket_ready_notify() argument
534 s->ready = local_socket_ready; in local_socket_ready_notify()
535 s->shutdown = NULL; in local_socket_ready_notify()
536 s->close = local_socket_close; in local_socket_ready_notify()
537 SendOkay(s->fd); in local_socket_ready_notify()
538 s->ready(s); in local_socket_ready_notify()
544 static void local_socket_close_notify(asocket* s) { in local_socket_close_notify() argument
545 s->ready = local_socket_ready; in local_socket_close_notify()
546 s->shutdown = NULL; in local_socket_close_notify()
547 s->close = local_socket_close; in local_socket_close_notify()
548 SendFail(s->fd, "closed"); in local_socket_close_notify()
549 s->close(s); in local_socket_close_notify()
552 static unsigned unhex(char* s, int len) { in unhex() argument
556 switch ((c = *s++)) { in unhex()
656 static int smart_socket_enqueue(asocket* s, apacket* p) { in smart_socket_enqueue() argument
664 D("SS(%d): enqueue %zu", s->id, p->len); in smart_socket_enqueue()
666 if (s->pkt_first == 0) { in smart_socket_enqueue()
667 s->pkt_first = p; in smart_socket_enqueue()
668 s->pkt_last = p; in smart_socket_enqueue()
670 if ((s->pkt_first->len + p->len) > s->get_max_payload()) { in smart_socket_enqueue()
671 D("SS(%d): overflow", s->id); in smart_socket_enqueue()
676 memcpy(s->pkt_first->data + s->pkt_first->len, p->data, p->len); in smart_socket_enqueue()
677 s->pkt_first->len += p->len; in smart_socket_enqueue()
680 p = s->pkt_first; in smart_socket_enqueue()
690 D("SS(%d): bad size (%d)", s->id, len); in smart_socket_enqueue()
694 D("SS(%d): len is %d", s->id, len); in smart_socket_enqueue()
697 D("SS(%d): waiting for %zu more bytes", s->id, len + 4 - p->len); in smart_socket_enqueue()
703 D("SS(%d): '%s'", s->id, (char*)(p->data + 4)); in smart_socket_enqueue()
739 if (handle_host_request(service, type, serial, s->peer->fd, s) == 0) { in smart_socket_enqueue()
741 D("SS(%d): handled host service '%s'", s->id, service); in smart_socket_enqueue()
745 D("SS(%d): okay transport", s->id); in smart_socket_enqueue()
756 D("SS(%d): couldn't create host service '%s'", s->id, service); in smart_socket_enqueue()
757 SendFail(s->peer->fd, "unknown host service"); in smart_socket_enqueue()
768 SendOkay(s->peer->fd); in smart_socket_enqueue()
770 s->peer->ready = local_socket_ready; in smart_socket_enqueue()
771 s->peer->shutdown = nullptr; in smart_socket_enqueue()
772 s->peer->close = local_socket_close; in smart_socket_enqueue()
773 s->peer->peer = s2; in smart_socket_enqueue()
774 s2->peer = s->peer; in smart_socket_enqueue()
775 s->peer = 0; in smart_socket_enqueue()
776 D("SS(%d): okay", s->id); in smart_socket_enqueue()
777 s->close(s); in smart_socket_enqueue()
784 if (s->transport == nullptr) { in smart_socket_enqueue()
786 s->transport = acquire_one_transport(kTransportAny, nullptr, nullptr, &error_msg); in smart_socket_enqueue()
787 if (s->transport == nullptr) { in smart_socket_enqueue()
788 SendFail(s->peer->fd, error_msg); in smart_socket_enqueue()
794 if (!s->transport) { in smart_socket_enqueue()
795 SendFail(s->peer->fd, "device offline (no transport)"); in smart_socket_enqueue()
797 } else if (s->transport->connection_state == kCsOffline) { in smart_socket_enqueue()
801 SendFail(s->peer->fd, "device offline (transport offline)"); in smart_socket_enqueue()
810 s->peer->ready = local_socket_ready_notify; in smart_socket_enqueue()
811 s->peer->shutdown = nullptr; in smart_socket_enqueue()
812 s->peer->close = local_socket_close_notify; in smart_socket_enqueue()
813 s->peer->peer = 0; in smart_socket_enqueue()
815 s->peer->transport = s->transport; in smart_socket_enqueue()
817 connect_to_remote(s->peer, (char*)(p->data + 4)); in smart_socket_enqueue()
818 s->peer = 0; in smart_socket_enqueue()
819 s->close(s); in smart_socket_enqueue()
827 s->close(s); in smart_socket_enqueue()
831 static void smart_socket_ready(asocket* s) { in smart_socket_ready() argument
832 D("SS(%d): ready", s->id); in smart_socket_ready()
835 static void smart_socket_close(asocket* s) { in smart_socket_close() argument
836 D("SS(%d): closed", s->id); in smart_socket_close()
837 if (s->pkt_first) { in smart_socket_close()
838 put_apacket(s->pkt_first); in smart_socket_close()
840 if (s->peer) { in smart_socket_close()
841 s->peer->peer = 0; in smart_socket_close()
842 s->peer->close(s->peer); in smart_socket_close()
843 s->peer = 0; in smart_socket_close()
845 free(s); in smart_socket_close()
850 asocket* s = reinterpret_cast<asocket*>(calloc(1, sizeof(asocket))); in create_smart_socket() local
851 if (s == NULL) fatal("cannot allocate socket"); in create_smart_socket()
852 s->enqueue = smart_socket_enqueue; in create_smart_socket()
853 s->ready = smart_socket_ready; in create_smart_socket()
854 s->shutdown = NULL; in create_smart_socket()
855 s->close = smart_socket_close; in create_smart_socket()
857 D("SS(%d)", s->id); in create_smart_socket()
858 return s; in create_smart_socket()
861 void connect_to_smartsocket(asocket* s) { in connect_to_smartsocket() argument
864 s->peer = ss; in connect_to_smartsocket()
865 ss->peer = s; in connect_to_smartsocket()
866 s->ready(s); in connect_to_smartsocket()