⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 w95sock.c

📁 Netscape NSPR库源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* -*- 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. *//* Win95 Sockets module * */#include "primpl.h"#define READ_FD     1#define WRITE_FD    2#define CONNECT_FD  3static PRInt32 socket_io_wait(    PRInt32 osfd,     PRInt32 fd_type,    PRIntervalTime timeout);/* --- SOCKET IO --------------------------------------------------------- */PRInt32_PR_MD_SOCKET(int af, int type, int flags){    SOCKET sock;    u_long one = 1;    sock = socket(af, type, flags);    if (sock == INVALID_SOCKET )     {        _PR_MD_MAP_SOCKET_ERROR(WSAGetLastError());        return (PRInt32)sock;    }    /*    ** Make the socket Non-Blocking    */    if (ioctlsocket( sock, FIONBIO, &one) != 0)    {        PR_SetError(PR_UNKNOWN_ERROR, WSAGetLastError());        closesocket(sock);        return -1;    }    return (PRInt32)sock;}/*** _MD_CloseSocket() -- Close a socket***/PRInt32_MD_CloseSocket(PRInt32 osfd){    PRInt32 rv;    rv = closesocket((SOCKET) osfd );    if (rv < 0)        _PR_MD_MAP_CLOSE_ERROR(WSAGetLastError());    return rv;}PRInt32_MD_SocketAvailable(PRFileDesc *fd){    PRInt32 result;    if (ioctlsocket(fd->secret->md.osfd, FIONREAD, &result) < 0) {        PR_SetError(PR_BAD_DESCRIPTOR_ERROR, WSAGetLastError());        return -1;    }    return result;}PRInt32 _MD_Accept(    PRFileDesc *fd,     PRNetAddr *raddr,     PRUint32 *rlen,    PRIntervalTime timeout ){    PRInt32 osfd = fd->secret->md.osfd;    PRInt32 rv, err;    while ((rv = accept(osfd, (struct sockaddr *) raddr, rlen)) == -1)     {        err = WSAGetLastError();        if ((err == WSAEWOULDBLOCK) && (!fd->secret->nonblocking))        {            if ((rv = socket_io_wait(osfd, READ_FD, timeout)) < 0)            {                return(-1);            }        }        else        {            _PR_MD_MAP_ACCEPT_ERROR(err);            break;        }    }    return(rv);} /* end _MD_accept() */PRInt32_PR_MD_CONNECT(PRFileDesc *fd, const PRNetAddr *addr, PRUint32 addrlen,                PRIntervalTime timeout){    PRInt32 osfd = fd->secret->md.osfd;    PRInt32 rv;    int     err;    if ((rv = connect(osfd, (struct sockaddr *) addr, addrlen)) == -1)     {        err = WSAGetLastError();        if ((!fd->secret->nonblocking) && (err == WSAEWOULDBLOCK))        {            rv = socket_io_wait(osfd, CONNECT_FD, timeout);            if ( rv < 0 )            {                return(-1);            }            else            {                PR_ASSERT(rv > 0);                /* it's connected */                return(0);            }         }        _PR_MD_MAP_CONNECT_ERROR(err);    }    return rv;}PRInt32_PR_MD_BIND(PRFileDesc *fd, const PRNetAddr *addr, PRUint32 addrlen){    PRInt32 rv;    rv = bind(fd->secret->md.osfd, (const struct sockaddr *)&(addr->inet), addrlen);    if (rv == SOCKET_ERROR)  {        _PR_MD_MAP_BIND_ERROR(WSAGetLastError());        return -1;    }    return 0;}PRInt32_PR_MD_LISTEN(PRFileDesc *fd, PRIntn backlog){    PRInt32 rv;    rv = listen(fd->secret->md.osfd, backlog);    if (rv == SOCKET_ERROR)  {        _PR_MD_MAP_DEFAULT_ERROR(WSAGetLastError());        return -1;    }    return 0;}PRInt32_PR_MD_RECV(PRFileDesc *fd, void *buf, PRInt32 amount, PRIntn flags,             PRIntervalTime timeout){    PRInt32 osfd = fd->secret->md.osfd;    PRInt32 rv, err;    int osflags;    if (0 == flags) {        osflags = 0;    } else {        PR_ASSERT(PR_MSG_PEEK == flags);        osflags = MSG_PEEK;    }    while ((rv = recv( osfd, buf, amount, osflags)) == -1)     {        if (((err = WSAGetLastError()) == WSAEWOULDBLOCK)             && (!fd->secret->nonblocking))        {            rv = socket_io_wait(osfd, READ_FD, timeout);            if ( rv < 0 )            {                return -1;            }         }         else         {            _PR_MD_MAP_RECV_ERROR(err);            break;        }    } /* end while() */    return(rv);}PRInt32_PR_MD_SEND(PRFileDesc *fd, const void *buf, PRInt32 amount, PRIntn flags,            PRIntervalTime timeout){    PRInt32 osfd = fd->secret->md.osfd;    PRInt32 rv, err;    PRInt32 bytesSent = 0;    while(bytesSent < amount )     {        while ((rv = send( osfd, buf, amount, 0 )) == -1)         {            if (((err = WSAGetLastError()) == WSAEWOULDBLOCK)                 && (!fd->secret->nonblocking))            {                rv = socket_io_wait(osfd, WRITE_FD, timeout);                if ( rv < 0 )                {                    return -1;                }            }             else             {                _PR_MD_MAP_SEND_ERROR(err);                return -1;            }        }        bytesSent += rv;        if (fd->secret->nonblocking)        {            break;        }        if (bytesSent < amount)         {            rv = socket_io_wait(osfd, WRITE_FD, timeout);            if ( rv < 0 )            {                return -1;            }        }    }    return bytesSent;}PRInt32_PR_MD_SENDTO(PRFileDesc *fd, const void *buf, PRInt32 amount, PRIntn flags,              const PRNetAddr *addr, PRUint32 addrlen, PRIntervalTime timeout){    PRInt32 osfd = fd->secret->md.osfd;    PRInt32 rv, err;    PRInt32 bytesSent = 0;    while(bytesSent < amount)     {        while ((rv = sendto( osfd, buf, amount, 0, (struct sockaddr *) addr,                addrlen)) == -1)         {            if (((err = WSAGetLastError()) == WSAEWOULDBLOCK)                 && (!fd->secret->nonblocking))            {                rv = socket_io_wait(osfd, WRITE_FD, timeout);                if ( rv < 0 )                {                    return -1;                }            }             else             {                _PR_MD_MAP_SENDTO_ERROR(err);                return -1;            }        }        bytesSent += rv;        if (fd->secret->nonblocking)        {            break;        }        if (bytesSent < amount)         {            rv = socket_io_wait(osfd, WRITE_FD, timeout);            if (rv < 0)             {                return -1;            }        }    }    return bytesSent;}PRInt32_PR_MD_RECVFROM(PRFileDesc *fd, void *buf, PRInt32 amount, PRIntn flags,                PRNetAddr *addr, PRUint32 *addrlen, PRIntervalTime timeout){    PRInt32 osfd = fd->secret->md.osfd;    PRInt32 rv, err;    while ((rv = recvfrom( osfd, buf, amount, 0, (struct sockaddr *) addr,            addrlen)) == -1) 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -