| 1326 | } |
| 1327 | |
| 1328 | ssize_t MtFrame::send(int fd, const void *buf, size_t nbyte, int flags, int timeout) |
| 1329 | { |
| 1330 | MtFrame* mtframe = MtFrame::Instance(); |
| 1331 | utime64_t start = mtframe->GetLastClock(); |
| 1332 | MicroThread* thread = mtframe->GetActiveThread(); |
| 1333 | utime64_t now = 0; |
| 1334 | |
| 1335 | if(fd<0 || !buf || nbyte<1) |
| 1336 | { |
| 1337 | errno = EINVAL; |
| 1338 | MTLOG_ERROR("send failed, errno: %d (%m)", errno); |
| 1339 | return -10; |
| 1340 | } |
| 1341 | |
| 1342 | ssize_t n = 0; |
| 1343 | size_t send_len = 0; |
| 1344 | while (send_len < nbyte) |
| 1345 | { |
| 1346 | now = mtframe->GetLastClock(); |
| 1347 | if ((int)(now - start) > timeout) |
| 1348 | { |
| 1349 | errno = ETIME; |
| 1350 | return -1; |
| 1351 | } |
| 1352 | |
| 1353 | mt_hook_syscall(send); |
| 1354 | n = ff_hook_send(fd, (char*)buf + send_len, nbyte - send_len, flags); |
| 1355 | if (n < 0) |
| 1356 | { |
| 1357 | if (errno == EINTR) { |
| 1358 | continue; |
| 1359 | } |
| 1360 | |
| 1361 | if ((errno != EAGAIN) && (errno != EWOULDBLOCK)) { |
| 1362 | MTLOG_ERROR("write failed, errno: %d", errno); |
| 1363 | return -2; |
| 1364 | } |
| 1365 | } |
| 1366 | else |
| 1367 | { |
| 1368 | send_len += n; |
| 1369 | if (send_len >= nbyte) { |
| 1370 | return nbyte; |
| 1371 | } |
| 1372 | } |
| 1373 | |
| 1374 | KqueuerObj epfd; |
| 1375 | epfd.SetOsfd(fd); |
| 1376 | epfd.EnableOutput(); |
| 1377 | epfd.SetOwnerThread(thread); |
| 1378 | if (!mtframe->KqueueSchedule(NULL, &epfd, timeout)) { |
| 1379 | return -3; |
| 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | return nbyte; |
| 1384 | } |
| 1385 | |