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

« Back to documentation index

Show/hide line numbers udpsocket_open_limit.cpp Source File

udpsocket_open_limit.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 "greentea-client/test_env.h"
00019 #include "mbed.h"
00020 #include MBED_CONF_APP_HEADER_FILE
00021 #include "udp_tests.h"
00022 #include "UDPSocket.h"
00023 #include "unity/unity.h"
00024 #include "utest.h"
00025 
00026 using namespace utest::v1;
00027 
00028 namespace
00029 {
00030     typedef struct UDPSocketItem {
00031         UDPSocket *sock;
00032         UDPSocketItem *next;
00033     } SocketItem;
00034 }
00035 
00036 void UDPSOCKET_OPEN_LIMIT()
00037 {
00038     int open_sockets[2] = {0};
00039 
00040     for (int i = 0; i < 2; i++) {
00041         UDPSocketItem *socket_list_head = NULL;
00042         UDPSocketItem *it;
00043 
00044         UDPSocket *sock;
00045         int ret;
00046         while (true) {
00047             sock = new UDPSocket;
00048             if (!sock) {
00049                 break;
00050             }
00051             ret = sock->open(get_interface());
00052             if (ret == NSAPI_ERROR_NO_MEMORY  || ret == NSAPI_ERROR_NO_SOCKET ) {
00053                 printf("[round#%02d] unable to open new socket, error: %d\n", i, ret);
00054                 delete sock;
00055                 break;
00056             }
00057 
00058             // Hopefully this doesn't interfere when trying to allocate more sockets
00059             it = new UDPSocketItem;
00060             if (!it) {
00061                 delete sock;
00062                 break;
00063             }
00064 
00065             it->sock = sock;
00066             // Order of items in the list doesn't matter
00067             it->next = socket_list_head;
00068             socket_list_head = it;
00069         }
00070 
00071         if (!socket_list_head) {
00072             break;
00073         }
00074 
00075         UDPSocketItem *tmp;
00076         for(UDPSocketItem *it = socket_list_head; it;) {
00077             ++open_sockets[i];
00078             tmp = it;
00079             it = it->next;
00080             socket_list_head = it;
00081             delete tmp->sock;
00082             delete tmp;
00083         }
00084         printf("[round#%02d] %d sockets opened\n", i, open_sockets[i]);
00085     }
00086     TEST_ASSERT_EQUAL(open_sockets[0], open_sockets[1]);
00087     // In case of lwIP one is taken by DHCP -> reduction by one to three
00088     TEST_ASSERT(open_sockets[0] >= 3);
00089 }