Knight KE / Mbed OS Game_Master
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers tcpsocket_recv_timeout.cpp Source File

tcpsocket_recv_timeout.cpp

00001 /*
00002  * Copyright (c) 2018, ARM Limited, All Rights Reserved
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License"); you may
00006  * not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  * http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00013  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #include "mbed.h"
00019 #include MBED_CONF_APP_HEADER_FILE
00020 #include "TCPSocket.h"
00021 #include "greentea-client/test_env.h"
00022 #include "unity/unity.h"
00023 #include "utest.h"
00024 #include "tcp_tests.h"
00025 
00026 using namespace utest::v1;
00027 
00028 namespace
00029 {
00030     static const int SIGNAL_SIGIO = 0x1;
00031     static const int SIGIO_TIMEOUT = 5000; //[ms]
00032 }
00033 
00034 static void _sigio_handler(osThreadId id) {
00035     osSignalSet(id, SIGNAL_SIGIO);
00036 }
00037 
00038 void TCPSOCKET_RECV_TIMEOUT()
00039 {
00040     static const int DATA_LEN = 100;
00041     char buff[DATA_LEN] = {0};
00042 
00043     TCPSocket sock;
00044     tcpsocket_connect_to_echo_srv(sock);
00045     sock.set_timeout(100);
00046     sock.sigio(callback(_sigio_handler, Thread::gettid()));
00047 
00048     int recvd = 0;
00049     int pkt_unrecvd;
00050     Timer timer;
00051     for (int i = 0; i < 5; i++) {
00052         pkt_unrecvd = DATA_LEN;
00053         TEST_ASSERT_EQUAL(DATA_LEN, sock.send(buff, DATA_LEN));
00054 
00055         while (pkt_unrecvd) {
00056             timer.reset();
00057             timer.start();
00058             recvd = sock.recv(&(buff[DATA_LEN-pkt_unrecvd]), pkt_unrecvd);
00059             timer.stop();
00060 
00061             if (recvd == NSAPI_ERROR_WOULD_BLOCK ) {
00062                 if(osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) {
00063                     TEST_FAIL();
00064                 }
00065                 printf("MBED: recv() took: %dms\n", timer.read_ms());
00066                 TEST_ASSERT_INT_WITHIN(50, 150, timer.read_ms());
00067                 continue;
00068             } else if (recvd < 0) {
00069                 printf("[pkt#%02d] network error %d\n", i, recvd);
00070                 TEST_FAIL();
00071             }
00072             pkt_unrecvd -= recvd;
00073         }
00074     }
00075     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK , sock.close());
00076 }