MCPcopy Create free account
hub / github.com/Tencent/Hardcoder / loop

Method loop

libapp2sys/src/main/cpp/localsocket.h:119–211  ·  view source on GitHub ↗

#lizard forgives

Source from the content-addressed store, hash-verified

117
118 // #lizard forgives
119 int loop(const int isServer) {
120 fd_set fdset;
121 FD_ZERO(&fdset);
122 FD_SET(m_fd, &fdset);
123 FD_SET(m_pipefd[0], &fdset);
124 int maxfd = m_fd;
125
126 pdbg("loop, m_fd:%d, maxfd:%d, isServer:%d", m_fd, maxfd, isServer);
127
128 while (m_fd) {
129 const int64_t startTime = getMillisecond();
130
131 SendPack *sendPack = m_sendQueue.front();
132 if (sendPack && checkCanWrite(sendPack->fd)) {
133
134 int ret = send(sendPack->fd, sendPack->data + sendPack->offset, sendPack->len - sendPack->offset, 0);
135
136 if (ret < 0) {
137 perr("loop, send failed, fd:%d, m_fd:%d", sendPack->fd, m_fd);
138 if (sendPack->fd == m_fd) { //client
139 notifyError(sendPack->fd);
140 break;
141 } else {//server
142 m_sendQueue.pop();
143 delete[] sendPack->data;
144 FD_CLR(sendPack->fd, &fdset);
145 notifyError(sendPack->fd);
146 delete sendPack;
147 continue;
148 }
149 }
150
151 if (ret > 0) {
152 sendPack->offset += ret;
153 }
154
155 if (sendPack->offset == sendPack->len) {//this sendPack finish
156 m_sendQueue.pop();
157 pdbg("loop, send sendPack finish, ret:%d, fd:%d, len:%d, offset:%d, queue:%d, costtime:%d ", ret, sendPack->fd, sendPack->len, sendPack->offset,
158 m_sendQueue.size(), TOINT(getMillisecond() - startTime) );
159 delete[] sendPack->data;
160 delete sendPack;
161 } else {
162 pdbg("loop, send, ret:%d, fd:%d, len:%d, offset:%d, queue:%d, costtime:%d ", ret, sendPack->fd, sendPack->len, sendPack->offset,
163 m_sendQueue.size(), TOINT(getMillisecond() - startTime) );
164 }
165 }
166 struct timeval timeout;
167 timeout.tv_sec = 1;
168 timeout.tv_usec = 0;
169 fd_set tmpSet = fdset;
170 int selectRet = select(maxfd + 1, &tmpSet, NULL, NULL, &timeout);
171 if (selectRet == -1 && errno != EINTR) {
172 pdbg("loop, select errno:%d", errno);
173 continue;
174 } else if (selectRet == 0) {
175 continue;
176 } else {

Callers

nothing calls this directly

Calls 4

getMillisecondFunction · 0.85
frontMethod · 0.80
popMethod · 0.80
sizeMethod · 0.45

Tested by

no test coverage detected