MCPcopy Create free account
hub / github.com/LemonOSProject/LemonOS / Poll

Method Poll

LibLemon/src/ipc/interfaceuds.cpp:55–126  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

53 }
54
55 std::shared_ptr<LemonMessageInfo> MessageServer::Poll(){
56 retry:
57 int fd = 0;
58 while((fd = accept(sock.fd, nullptr, nullptr)) > 0){
59 fds.push_back({ .fd = fd, .events = POLLIN, .revents = 0 });
60 }
61
62 if(queue.size() > 0){
63 auto element = queue.front();
64 queue.pop_front();
65 return element;
66 }
67
68 if(!fds.size()) {
69 return std::shared_ptr<LemonMessageInfo>(nullptr);
70 }
71
72 int evCount = poll(fds.data(), fds.size(), 0);
73 if(evCount > 0){
74 for(size_t i = 0; i < fds.size(); i++){
75 if(fds[i].revents & (POLLNVAL | POLLHUP)){
76 int fd = fds[i].fd;
77 fds.erase(fds.begin() + i);
78
79 std::shared_ptr<LemonMessageInfo> newMsg = std::shared_ptr<LemonMessageInfo>((LemonMessageInfo*)malloc(sizeof(LemonMessageInfo)));
80 newMsg->msg.protocol = 0; // Disconnected
81 newMsg->clientFd = fd;
82
83 return newMsg;
84 }
85
86 if(!(fds[i].revents & POLLIN)) continue; // We only care about POLLIN
87 LemonMessage msg;
88
89 ssize_t len = recv(fds[i].fd, &msg, sizeof(LemonMessage), 0);
90 if(len < static_cast<ssize_t>(sizeof(LemonMessage))){
91 printf("invalid length: %ld\n", len);
92 continue;
93 }
94
95 if(msg.magic != LEMON_MESSAGE_MAGIC){
96 printf("Invalid magic: %x, discarding data.\n", msg.magic);
97 while(msg.magic != LEMON_MESSAGE_MAGIC && len >= static_cast<ssize_t>(sizeof(LemonMessage))){
98 len = recv(fds[i].fd, &msg, sizeof(msg), MSG_DONTWAIT); // Discard everything until we find a message
99 }
100 }
101
102 if(msg.magic != LEMON_MESSAGE_MAGIC){
103 printf("invalid magic: %x\n", msg.magic);
104 continue;
105 }
106
107 std::shared_ptr<LemonMessageInfo> newMsg((LemonMessageInfo*)malloc(sizeof(LemonMessageInfo) + msg.length));
108 newMsg->msg = msg;
109 newMsg->clientFd = fds[i].fd;
110 len = recv(fds[i].fd, newMsg->msg.data, msg.length, 0);
111
112 if(len < msg.length){

Callers

nothing calls this directly

Calls 4

pollClass · 0.85
sizeMethod · 0.80
dataMethod · 0.80
beginMethod · 0.45

Tested by

no test coverage detected