📄 prlayer.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. *//*** File: prlayer.c** Description: Routines for handling pushable protocol modules on sockets.*/#include "primpl.h"#include "prerror.h"#include "prmem.h"#include "prlock.h"#include "prlog.h"#include "prio.h"#include <string.h> /* for memset() */static PRStatus _PR_DestroyIOLayer(PRFileDesc *stack);void PR_CALLBACK pl_FDDestructor(PRFileDesc *fd){ PR_ASSERT(fd != NULL); if (NULL != fd->lower) fd->lower->higher = fd->higher; if (NULL != fd->higher) fd->higher->lower = fd->lower; PR_DELETE(fd);}/*** Default methods that just call down to the next fd.*/static PRStatus PR_CALLBACK pl_TopClose (PRFileDesc *fd){ PRFileDesc *top, *lower; PRStatus rv; PR_ASSERT(fd != NULL); PR_ASSERT(fd->lower != NULL); PR_ASSERT(fd->secret == NULL); PR_ASSERT(fd->methods->file_type == PR_DESC_LAYERED); if (PR_IO_LAYER_HEAD == fd->identity) { /* * new style stack; close all the layers, before deleting the * stack head */ rv = fd->lower->methods->close(fd->lower); _PR_DestroyIOLayer(fd); return rv; } else if ((fd->higher) && (PR_IO_LAYER_HEAD == fd->higher->identity)) { /* * lower layers of new style stack */ lower = fd->lower; /* * pop and cleanup current layer */ top = PR_PopIOLayer(fd->higher, PR_TOP_IO_LAYER); top->dtor(top); /* * then call lower layer */ return (lower->methods->close(lower)); } else { /* old style stack */ top = PR_PopIOLayer(fd, PR_TOP_IO_LAYER); top->dtor(top); return (fd->methods->close)(fd); }}static PRInt32 PR_CALLBACK pl_DefRead (PRFileDesc *fd, void *buf, PRInt32 amount){ PR_ASSERT(fd != NULL); PR_ASSERT(fd->lower != NULL); return (fd->lower->methods->read)(fd->lower, buf, amount);}static PRInt32 PR_CALLBACK pl_DefWrite ( PRFileDesc *fd, const void *buf, PRInt32 amount){ PR_ASSERT(fd != NULL); PR_ASSERT(fd->lower != NULL); return (fd->lower->methods->write)(fd->lower, buf, amount);}static PRInt32 PR_CALLBACK pl_DefAvailable (PRFileDesc *fd){ PR_ASSERT(fd != NULL); PR_ASSERT(fd->lower != NULL); return (fd->lower->methods->available)(fd->lower);}static PRInt64 PR_CALLBACK pl_DefAvailable64 (PRFileDesc *fd){ PR_ASSERT(fd != NULL); PR_ASSERT(fd->lower != NULL); return (fd->lower->methods->available64)(fd->lower);}static PRStatus PR_CALLBACK pl_DefFsync (PRFileDesc *fd){ PR_ASSERT(fd != NULL); PR_ASSERT(fd->lower != NULL); return (fd->lower->methods->fsync)(fd->lower);}static PRInt32 PR_CALLBACK pl_DefSeek ( PRFileDesc *fd, PRInt32 offset, PRSeekWhence how){ PR_ASSERT(fd != NULL); PR_ASSERT(fd->lower != NULL); return (fd->lower->methods->seek)(fd->lower, offset, how);}static PRInt64 PR_CALLBACK pl_DefSeek64 ( PRFileDesc *fd, PRInt64 offset, PRSeekWhence how){ PR_ASSERT(fd != NULL); PR_ASSERT(fd->lower != NULL); return (fd->lower->methods->seek64)(fd->lower, offset, how);}static PRStatus PR_CALLBACK pl_DefFileInfo (PRFileDesc *fd, PRFileInfo *info){ PR_ASSERT(fd != NULL); PR_ASSERT(fd->lower != NULL); return (fd->lower->methods->fileInfo)(fd->lower, info);}static PRStatus PR_CALLBACK pl_DefFileInfo64 (PRFileDesc *fd, PRFileInfo64 *info){ PR_ASSERT(fd != NULL); PR_ASSERT(fd->lower != NULL); return (fd->lower->methods->fileInfo64)(fd->lower, info);}static PRInt32 PR_CALLBACK pl_DefWritev (PRFileDesc *fd, const PRIOVec *iov, PRInt32 size, PRIntervalTime timeout){ PR_ASSERT(fd != NULL); PR_ASSERT(fd->lower != NULL); return (fd->lower->methods->writev)(fd->lower, iov, size, timeout);}static PRStatus PR_CALLBACK pl_DefConnect ( PRFileDesc *fd, const PRNetAddr *addr, PRIntervalTime timeout){ PR_ASSERT(fd != NULL); PR_ASSERT(fd->lower != NULL); return (fd->lower->methods->connect)(fd->lower, addr, timeout);}static PRStatus PR_CALLBACK pl_DefConnectcontinue ( PRFileDesc *fd, PRInt16 out_flags){ PR_ASSERT(fd != NULL); PR_ASSERT(fd->lower != NULL); return (fd->lower->methods->connectcontinue)(fd->lower, out_flags);}static PRFileDesc* PR_CALLBACK pl_TopAccept ( PRFileDesc *fd, PRNetAddr *addr, PRIntervalTime timeout){ PRStatus rv; PRFileDesc *newfd, *layer = fd; PRFileDesc *newstack; PRBool newstyle_stack = PR_FALSE; PR_ASSERT(fd != NULL); PR_ASSERT(fd->lower != NULL); /* test for new style stack */ while (NULL != layer->higher) layer = layer->higher; newstyle_stack = (PR_IO_LAYER_HEAD == layer->identity) ? PR_TRUE : PR_FALSE; newstack = PR_NEW(PRFileDesc); if (NULL == newstack) { PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); return NULL; } *newstack = *fd; /* make a copy of the accepting layer */ newfd = (fd->lower->methods->accept)(fd->lower, addr, timeout); if (NULL == newfd) { PR_DELETE(newstack); return NULL; } if (newstyle_stack) { newstack->lower = newfd; newfd->higher = newstack; return newstack; } else { /* this PR_PushIOLayer call cannot fail */ rv = PR_PushIOLayer(newfd, PR_TOP_IO_LAYER, newstack); PR_ASSERT(PR_SUCCESS == rv); return newfd; /* that's it */ }}static PRStatus PR_CALLBACK pl_DefBind (PRFileDesc *fd, const PRNetAddr *addr){ PR_ASSERT(fd != NULL); PR_ASSERT(fd->lower != NULL); return (fd->lower->methods->bind)(fd->lower, addr);}static PRStatus PR_CALLBACK pl_DefListen (PRFileDesc *fd, PRIntn backlog){ PR_ASSERT(fd != NULL); PR_ASSERT(fd->lower != NULL); return (fd->lower->methods->listen)(fd->lower, backlog);}static PRStatus PR_CALLBACK pl_DefShutdown (PRFileDesc *fd, PRIntn how){ PR_ASSERT(fd != NULL); PR_ASSERT(fd->lower != NULL); return (fd->lower->methods->shutdown)(fd->lower, how);}static PRInt32 PR_CALLBACK pl_DefRecv ( PRFileDesc *fd, void *buf, PRInt32 amount, PRIntn flags, PRIntervalTime timeout){ PR_ASSERT(fd != NULL); PR_ASSERT(fd->lower != NULL); return (fd->lower->methods->recv)( fd->lower, buf, amount, flags, timeout);}static PRInt32 PR_CALLBACK pl_DefSend ( PRFileDesc *fd, const void *buf, PRInt32 amount, PRIntn flags, PRIntervalTime timeout){ PR_ASSERT(fd != NULL); PR_ASSERT(fd->lower != NULL); return (fd->lower->methods->send)(fd->lower, buf, amount, flags, timeout);}static PRInt32 PR_CALLBACK pl_DefRecvfrom ( PRFileDesc *fd, void *buf, PRInt32 amount, PRIntn flags, PRNetAddr *addr, PRIntervalTime timeout){ PR_ASSERT(fd != NULL); PR_ASSERT(fd->lower != NULL); return (fd->lower->methods->recvfrom)( fd->lower, buf, amount, flags, addr, timeout);}static PRInt32 PR_CALLBACK pl_DefSendto ( PRFileDesc *fd, const void *buf, PRInt32 amount, PRIntn flags, const PRNetAddr *addr, PRIntervalTime timeout){ PR_ASSERT(fd != NULL); PR_ASSERT(fd->lower != NULL); return (fd->lower->methods->sendto)( fd->lower, buf, amount, flags, addr, timeout);}static PRInt16 PR_CALLBACK pl_DefPoll ( PRFileDesc *fd, PRInt16 in_flags, PRInt16 *out_flags){ PR_ASSERT(fd != NULL); PR_ASSERT(fd->lower != NULL); return (fd->lower->methods->poll)(fd->lower, in_flags, out_flags);}static PRInt32 PR_CALLBACK pl_DefAcceptread ( PRFileDesc *sd, PRFileDesc **nd, PRNetAddr **raddr, void *buf, PRInt32 amount, PRIntervalTime t){ PRInt32 nbytes; PRStatus rv; PRFileDesc *newstack; PRFileDesc *layer = sd; PRBool newstyle_stack = PR_FALSE; PR_ASSERT(sd != NULL); PR_ASSERT(sd->lower != NULL); /* test for new style stack */ while (NULL != layer->higher) layer = layer->higher; newstyle_stack = (PR_IO_LAYER_HEAD == layer->identity) ? PR_TRUE : PR_FALSE; newstack = PR_NEW(PRFileDesc); if (NULL == newstack) { PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); return -1; } *newstack = *sd; /* make a copy of the accepting layer */ nbytes = sd->lower->methods->acceptread( sd->lower, nd, raddr, buf, amount, t); if (-1 == nbytes) { PR_DELETE(newstack); return nbytes; } if (newstyle_stack) { newstack->lower = *nd; (*nd)->higher = newstack; *nd = newstack; return nbytes; } else { /* this PR_PushIOLayer call cannot fail */ rv = PR_PushIOLayer(*nd, PR_TOP_IO_LAYER, newstack); PR_ASSERT(PR_SUCCESS == rv); return nbytes; }}static PRInt32 PR_CALLBACK pl_DefTransmitfile ( PRFileDesc *sd, PRFileDesc *fd, const void *headers, PRInt32 hlen, PRTransmitFileFlags flags, PRIntervalTime t){ PR_ASSERT(sd != NULL); PR_ASSERT(sd->lower != NULL); return sd->lower->methods->transmitfile( sd->lower, fd, headers, hlen, flags, t);}static PRStatus PR_CALLBACK pl_DefGetsockname (PRFileDesc *fd, PRNetAddr *addr){ PR_ASSERT(fd != NULL); PR_ASSERT(fd->lower != NULL); return (fd->lower->methods->getsockname)(fd->lower, addr);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -