//============================================================================== // // Copyright (C) 2004 Dick van Oudheusden // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public // License as published by the Free Software Foundation; either // version 2 of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // General Public License for more details. // // You should have received a copy of the GNU General Public // License along with this program; if not, write to the Free // Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. // //============================================================================== // // $Date: 2004/06/21 08:32:07 $ $Revision: 1.5 $ // //============================================================================== #include #include #include "ofc/config.h" #include "ofc/DUDPServer.h" #include "ofc/DUDPClient.h" #include "ofc/DTimer.h" #include "DTest.h" #ifdef WIN32 #define WITH_THREADS #endif //-Wrapper--------------------------------------------------------------------- @interface UDPTestServer : DUDPServer { } - start :(id ) address; - (BOOL) handleRequest :(DData *) request :(DData *) response; @end @implementation UDPTestServer - start :(id ) address { #ifdef WITH_THREADS [super start :address]; objc_thread_exit(); #else int child = fork(); if (child == 0) { TEST([super start :address]); exit(0); } #endif return self; } - (BOOL) handleRequest :(DData *) request :(DData *) response { BOOL shouldStop = NO; long value = 0; value = [request readLong]; if (value == 0) shouldStop = YES; value++; // return the long increased by one [response writeLong :value]; return shouldStop; } @end //---------------------------------------------------------------------------- void DUdpServerClient_test() { DInetSocketAddress *address1 = [DInetSocketAddress new]; DInetSocketAddress *address2 = [DInetSocketAddress new]; DData *send = [DData new]; DData *receive = nil; UDPTestServer *server = [UDPTestServer new]; DUDPClient *client = [DUDPClient new]; DSocket *socket = nil; STARTTEST(); [address1 any :6000]; TEST([server open :[address1 family] :[DSocket protocol :"udp"]]); #ifdef WITH_THREADS if (objc_thread_detach(@selector(start:), server, address1) == NULL) { printf("Could not start thread..\n"); } #else [server start :address1]; #endif [DTimer delay :200]; [address2 loopback :6000]; TEST([client open :[address2 family] :[DSocket protocol :"udp"]]); TEST([client start :address2]); socket = [client socket]; // printf("Error: %d\n", [socket error]); [send writeLong :8]; receive = [client doRequest :address2 :[send data] :[send length] :50]; TEST(receive != nil); TEST([receive readLong] == 9); [receive free]; [send clear]; [send writeLong :90]; receive = [client doRequest :address2 :[send data] :[send length] :50]; TEST(receive != nil); TEST([receive readLong] == 91); [receive free]; [send clear]; [send writeLong :0]; // Server stop request.. receive = [client doRequest :address2 :[send data] :[send length] :50]; TEST(receive != nil); TEST([receive readLong] == 1); [send free]; [receive free]; [client stop]; [server free]; [address2 free]; [address1 free]; STOPTEST(); }