📄 accept.c
字号:
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- *//* * The contents of this file are subject to the Mozilla Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * The Original Code is the Netscape Portable Runtime (NSPR). * * The Initial Developer of the Original Code is Netscape * Communications Corporation. Portions created by Netscape are * Copyright (C) 1998-2000 Netscape Communications Corporation. All * Rights Reserved. * * Contributor(s): * * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the * "GPL"), in which case the provisions of the GPL are applicable * instead of those above. If you wish to allow use of your * version of this file only under the terms of the GPL and not to * allow others to use your version of this file under the MPL, * indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by * the GPL. If you do not delete the provisions above, a recipient * may use your version of this file under either the MPL or the * GPL. *//************************************************************************* 1996 - Netscape Communications Corporation**** Name: accept.c**** Description: Run accept() sucessful connection tests.**** Modification History:** 04-Jun-97 AGarcia - Reconvert test file to return a 0 for PASS and a 1 for FAIL** 13-May-97 AGarcia- Converted the test to accomodate the debug_mode ** The debug mode will print all of the printfs associated with this test.** The regress mode will be the default mode. Since the regress tool limits** the output to a one line status:PASS or FAIL,all of the printf statements** have been handled with an if (debug_mode) statement.** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to** recognize the return code from tha main program.** 12-June-97 Revert to return code 0 and 1.***********************************************************************//************************************************************************* Includes***********************************************************************/#include "nspr.h"#include "prpriv.h"#include <stdlib.h>#include <string.h>#include "plgetopt.h"#include "plerror.h"#define BASE_PORT 10000#define CLIENT_DATA 128#define ACCEPT_NORMAL 0x1#define ACCEPT_FAST 0x2#define ACCEPT_READ 0x3#define ACCEPT_READ_FAST 0x4#define ACCEPT_READ_FAST_CB 0x5#define CLIENT_NORMAL 0x1#define CLIENT_TIMEOUT_ACCEPT 0x2#define CLIENT_TIMEOUT_SEND 0x3#define SERVER_MAX_BIND_COUNT 100#if defined(XP_MAC) || defined(XP_OS2)#define TIMEOUTSECS 10#else#define TIMEOUTSECS 2#endifPRIntervalTime timeoutTime;static PRInt32 count = 1;static PRFileDesc *output;static PRNetAddr serverAddr;static PRThreadScope thread_scope = PR_LOCAL_THREAD;static PRInt32 clientCommand;static PRInt32 iterations;static PRStatus rv;static PRFileDesc *listenSock;static PRFileDesc *clientSock = NULL;static PRNetAddr listenAddr;static PRNetAddr clientAddr;static PRThread *clientThread;static PRNetAddr *raddr;static char buf[4096 + 2*sizeof(PRNetAddr) + 32];static PRInt32 status;static PRInt32 bytesRead;PRIntn failed_already=0;PRIntn debug_mode;void Test_Assert(const char *msg, const char *file, PRIntn line){ failed_already=1; if (debug_mode) { PR_fprintf(output, "@%s:%d ", file, line); PR_fprintf(output, msg); }} /* Test_Assert */#define TEST_ASSERT(expr) \ if (!(expr)) Test_Assert(#expr, __FILE__, __LINE__)#ifdef WINNT#define CALLBACK_MAGIC 0x12345678void timeout_callback(void *magic){ TEST_ASSERT(magic == (void *)CALLBACK_MAGIC); if (debug_mode) PR_fprintf(output, "timeout callback called okay\n");}#endifstatic void PR_CALLBACKClientThread(void *_action){ PRInt32 action = * (PRInt32 *) _action; PRInt32 iterations = count; PRFileDesc *sock = NULL; serverAddr.inet.family = PR_AF_INET; serverAddr.inet.port = listenAddr.inet.port; serverAddr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK); for (; iterations--;) { PRInt32 rv; char buf[CLIENT_DATA]; memset(buf, 0xaf, sizeof(buf)); /* initialize with arbitrary data */ sock = PR_NewTCPSocket(); if (!sock) { if (!debug_mode) failed_already=1; else PR_fprintf(output, "client: unable to create socket\n"); return; } if (action != CLIENT_TIMEOUT_ACCEPT) { if ((rv = PR_Connect(sock, &serverAddr, timeoutTime)) < 0) { if (!debug_mode) failed_already=1; else PR_fprintf(output, "client: unable to connect to server (%ld, %ld, %ld, %ld)\n", iterations, rv, PR_GetError(), PR_GetOSError()); goto ErrorExit; } if (action != CLIENT_TIMEOUT_SEND) { if ((rv = PR_Send(sock, buf, CLIENT_DATA, 0, timeoutTime))< 0) { if (!debug_mode) failed_already=1; else PR_fprintf(output, "client: unable to send to server (%d, %ld, %ld)\n", CLIENT_DATA, rv, PR_GetError()); goto ErrorExit; } } else { PR_Sleep(PR_SecondsToInterval(TIMEOUTSECS + 1)); } } else { PR_Sleep(PR_SecondsToInterval(TIMEOUTSECS + 1)); } if (debug_mode) PR_fprintf(output, "."); PR_Close(sock); sock = NULL; } if (debug_mode) PR_fprintf(output, "\n");ErrorExit: if (sock != NULL) PR_Close(sock);}static void RunTest(PRInt32 acceptType, PRInt32 clientAction){int i; /* First bind to the socket */ listenSock = PR_NewTCPSocket(); if (!listenSock) { failed_already=1; if (debug_mode) PR_fprintf(output, "unable to create listen socket\n"); return; } memset(&listenAddr, 0 , sizeof(listenAddr)); listenAddr.inet.family = PR_AF_INET; listenAddr.inet.port = PR_htons(BASE_PORT); listenAddr.inet.ip = PR_htonl(PR_INADDR_ANY); /* * try a few times to bind server's address, if addresses are in * use */ i = 0; while (PR_Bind(listenSock, &listenAddr) == PR_FAILURE) { if (PR_GetError() == PR_ADDRESS_IN_USE_ERROR) { listenAddr.inet.port += 2; if (i++ < SERVER_MAX_BIND_COUNT) continue; } failed_already=1; if (debug_mode) PR_fprintf(output,"accept: ERROR - PR_Bind failed\n"); return; } rv = PR_Listen(listenSock, 100); if (rv == PR_FAILURE) { failed_already=1; if (debug_mode) PR_fprintf(output, "unable to listen\n"); return; } clientCommand = clientAction; clientThread = PR_CreateThread(PR_USER_THREAD, ClientThread, (void *)&clientCommand, PR_PRIORITY_NORMAL, thread_scope, PR_JOINABLE_THREAD, 0); if (!clientThread) { failed_already=1; if (debug_mode) PR_fprintf(output, "error creating client thread\n"); return; } iterations = count; for (;iterations--;) { switch (acceptType) { case ACCEPT_NORMAL: clientSock = PR_Accept(listenSock, &clientAddr,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -