| 332 | } |
| 333 | |
| 334 | int mt_tcpsend(struct sockaddr_in* dst, void* pkg, int len, int timeout) |
| 335 | { |
| 336 | if (!dst || !pkg || len<1) |
| 337 | { |
| 338 | MTLOG_ERROR("input params invalid, dst[%p], pkg[%p], len[%d]", dst, pkg, len); |
| 339 | return -10; |
| 340 | } |
| 341 | |
| 342 | int ret = 0, rc = 0; |
| 343 | int addr_len = sizeof(struct sockaddr_in); |
| 344 | utime64_t start_ms = MtFrame::Instance()->GetLastClock(); |
| 345 | utime64_t cost_time = 0; |
| 346 | int time_left = timeout; |
| 347 | |
| 348 | int sock = -1; |
| 349 | TcpKeepConn* conn = mt_tcp_get_keep_conn(dst, sock); |
| 350 | if ((conn == NULL) || (sock < 0)) |
| 351 | { |
| 352 | MTLOG_ERROR("socket[%d] get conn failed, ret[%m]", sock); |
| 353 | ret = -1; |
| 354 | goto EXIT_LABEL; |
| 355 | } |
| 356 | |
| 357 | rc = MtFrame::connect(sock, (struct sockaddr *)dst, addr_len, time_left); |
| 358 | if (rc < 0) |
| 359 | { |
| 360 | MTLOG_ERROR("socket[%d] connect failed, ret[%d][%m]", sock, rc); |
| 361 | ret = -4; |
| 362 | goto EXIT_LABEL; |
| 363 | } |
| 364 | |
| 365 | cost_time = MtFrame::Instance()->GetLastClock() - start_ms; |
| 366 | time_left = (timeout > (int)cost_time) ? (timeout - (int)cost_time) : 0; |
| 367 | rc = MtFrame::send(sock, pkg, len, 0, time_left); |
| 368 | if (rc < 0) |
| 369 | { |
| 370 | MTLOG_ERROR("socket[%d] send failed, ret[%d][%m]", sock, rc); |
| 371 | ret = -2; |
| 372 | goto EXIT_LABEL; |
| 373 | } |
| 374 | |
| 375 | ret = 0; |
| 376 | |
| 377 | EXIT_LABEL: |
| 378 | |
| 379 | if (conn != NULL) |
| 380 | { |
| 381 | ConnectionMgr::Instance()->FreeConnection(conn, (ret < 0)); |
| 382 | } |
| 383 | |
| 384 | return ret; |
| 385 | } |
| 386 | |
| 387 | int mt_tcpsend_short(struct sockaddr_in* dst, void* pkg, int len, int timeout) |
| 388 | { |
no test coverage detected