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

📄 core.c

📁 Serveez是一个服务器框架
💻 C
📖 第 1 页 / 共 2 页
字号:
    }#endif /* TCP_CORK */  return 0;}/* * This function transmits data between one file descriptor and another  * where @var{in_fd} is the source and @var{out_fd} the destination. The * @var{offset} argument is a pointer to a variable holding the input file  * pointer position from which reading starts. When this routine returns, * the @var{offset} variable will be set to the offset of the byte following  * the last byte that was read. @var{count} is the number of bytes to copy  * between file descriptors. Returns the number of bytes actually  * read/written or -1 on errors. */intsvz_sendfile (int out_fd, int in_fd, off_t *offset, unsigned int count){#ifndef ENABLE_SENDFILE  svz_log (LOG_ERROR, "sendfile() disabled\n");  return -1;#else#if defined (HAVE_SENDFILE) || defined (__MINGW32__)  int ret;#if defined (__osf__)  /* FIXME:     On True64 sendfile() is said to crash the machine. Moreover the     system call is not documented. Thus we do not know the exact meaning     of the remaining arguments. We definitely know that there must be     some kind of pointer (maybe specifying head and tail). */  ret = sendfile (out_fd, in_fd, *offset, count, NULL, NULL, 0);  *offset += ((ret >= 0) ? ret : 0);#elif defined (__FreeBSD__)  /* This was tested for FreeBSD4.3 on alpha. */  off_t sbytes;  ret = sendfile (in_fd, out_fd, *offset, count, NULL, &sbytes, 0);  *offset += sbytes;  ret = ret ? -1 : (int) sbytes;#elif defined (__MINGW32__)  /* TransmitFile().     This system call provides something likely the Unix sendfile(). It     is a M$ specific entension to Winsock. The function comes from the     MSWSOCK.DLL and should be prototyped in MSWSOCK.H. It operates on     file handles gained from kernel32.CreateFile() only. We experienced     quite low performance on small (less than 4096 byte) file chunks.      Performance is better with about 32 KB per chunk. The function is      available on Windows NT, Windows 2000 and Windows XP only (not W95,      W98 or ME). */  OVERLAPPED overlap = { 0, 0, 0, 0, NULL };  DWORD result;  /* Data transmission via overlapped I/O.      The MSDN documentation tells nothing odd about passing NULL as      overlapped structure argument, but we experienced that this does not     work. Thus we pass the overlapped structure with the Offset member     set to the current file position. */  overlap.Offset = *offset;  if (!TransmitFile ((SOCKET) out_fd, (HANDLE) in_fd, count, 0,                      &overlap, NULL, 0))    {      /* Operation is pending. */      if (GetLastError () == ERROR_IO_PENDING)        {          /* Wait for the operation to complete (blocking). We could either	     wait here for the socket handle itself or for the hEvent member	     of the overlapped structure which must be created previously.	     If waiting for the socket handle we need to ensure that no other	     thread is operating on the socket. This is given since serveez 	     is single threaded. */          if ((result = WaitForSingleObject ((HANDLE) out_fd, INFINITE)) !=               WAIT_OBJECT_0)            {              svz_log (LOG_ERROR, "WaitForSingleObject: %s\n", SYS_ERROR);              ret = -1;            }          /* Finally transmitted the data. */          else            {              *offset += count;              ret = count;            }        }      /* Some error occurred. */      else        {          svz_log (LOG_ERROR, "TransmitFile: %s\n", SYS_ERROR);          ret = -1;        }    }  /* Data transmission completed successfully at once. */  else    {      *offset += count;      ret = count;    }#else   /* Linux here. Works like charme... */  ret = sendfile (out_fd, in_fd, offset, count);#endif  return ret;#else  svz_log (LOG_ERROR, "sendfile() not available\n");  return -1;#endif /* HAVE_SEND_FILE */#endif /* ENABLE_SENDFILE */}#ifndef __MINGW32__/* List for file descriptors. */static svz_array_t *svz_files = NULL;/* Add a file descriptor to the list. */static voidsvz_file_add (int fd){  if (svz_files == NULL)    svz_files = svz_array_create (1);  svz_array_add (svz_files, SVZ_NUM2PTR (fd));}/* Delete a file descriptor from the list. */static voidsvz_file_del (int fd){  void *val;  unsigned long n;  svz_array_foreach (svz_files, val, n)    {      if (val == SVZ_NUM2PTR (fd))	{	  svz_array_del (svz_files, n);	  break;	}    }  if (svz_array_size (svz_files) == 0)    {      svz_array_destroy (svz_files);      svz_files = NULL;    }}/* * Close all file descriptors collected so far by the core API of serveez. * This should be called if @code{fork()} has been called without a  * following @code{exec()}. */voidsvz_file_closeall (void){  void *fd;  unsigned long n;  svz_array_foreach (svz_files, fd, n)    close ((int) SVZ_PTR2NUM (fd));}#endif /* not __MINGW32__ *//* * Open the filename @var{file} and convert it into a file handle. The * given @var{flags} specify the access mode and the @var{mode} argument * the permissions if the @code{O_CREAT} flag is set. */intsvz_open (const char *file, int flags, unsigned int mode){#ifndef __MINGW32__  int fd;  if ((fd = open (file, flags, (mode_t) mode)) < 0)    {      svz_log (LOG_ERROR, "open (%s): %s\n", file, SYS_ERROR);      return -1;    }  if (svz_fd_cloexec (fd) < 0)    {      close (fd);      return -1;    }  svz_file_add (fd);  return fd;#else /* __MINGW32__ */  DWORD access = 0, creation = 0;  HANDLE fd;  /* drop this flag */  flags &= ~O_BINARY;  /* translate access */  if (flags == O_RDONLY)    access = GENERIC_READ;  else if (flags & O_WRONLY)    access = GENERIC_WRITE;  else if (flags & O_RDWR)    access = GENERIC_READ | GENERIC_WRITE;  /* creation necessary ? */  if (flags & O_CREAT)    {      creation |= CREATE_ALWAYS;      if (flags & O_EXCL)        creation |= CREATE_NEW;    }  else    {      creation |= OPEN_EXISTING;      if (flags & O_TRUNC)        creation |= TRUNCATE_EXISTING;    }  if ((fd = CreateFile (file, access, 0, NULL, creation, 0, NULL)) ==       INVALID_HANDLE_VALUE)    {      svz_log (LOG_ERROR, "CreateFile (%s): %s\n", file, SYS_ERROR);      return -1;    }  if (flags & O_APPEND)    SetFilePointer (fd, 0, 0, FILE_END);  return (int) fd;#endif /* not __MINGW32__ */}/* * Close the given file handle @var{fd}. Return -1 on errors. */intsvz_close (int fd){#ifndef __MINGW32__  if (close (fd) < 0)    {      svz_log (LOG_ERROR, "close: %s\n", SYS_ERROR);      return -1;    }  svz_file_del (fd);#else /* __MINGW32__ */  if (!CloseHandle ((HANDLE) fd))    {      svz_log (LOG_ERROR, "CloseHandle: %s\n", SYS_ERROR);      return -1;    }#endif /* not __MINGW32__ */  return 0;}/* * Conversion from FILETIME (100 nano-sec intervals from 1.1.1601) to * UTC time (seconds since 1.1.1970). */#define DIFF_FT_LT                             \  /* there have been 89 years with 366 days */ \  ((((__int64) (1970 - 1601) * 365 + 89) * 24 * 3600) * 10000000L)#define ft2lt(ft)                                                    \  ((time_t) (((ft.dwLowDateTime | (__int64) ft.dwHighDateTime << 32) \               - DIFF_FT_LT) / 10000000L))/* * Return information about the specified file associated with the file * descriptor @var{fd} returned by @code{svz_open()}. Stores available * information in the stat buffer @var{buf}. */intsvz_fstat (int fd, struct stat *buf){#ifndef __MINGW32__  if (fstat (fd, buf) < 0)    {      svz_log (LOG_ERROR, "fstat: %s\n", SYS_ERROR);      return -1;    }#else /* __MINGW32__ */  BY_HANDLE_FILE_INFORMATION info;  if (buf == NULL)    return -1;  if (!GetFileInformationByHandle ((HANDLE) fd, &info))    {      svz_log (LOG_ERROR, "GetFileInformationByHandle: %s\n", SYS_ERROR);      return -1;    }  buf->st_mode = 0;  if (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)    buf->st_mode |= (S_IFDIR | _S_IREAD | _S_IWRITE | _S_IEXEC);  else if (!(info.dwFileAttributes & FILE_ATTRIBUTE_OFFLINE))    buf->st_mode |= (S_IFREG | _S_IREAD | _S_IWRITE | _S_IEXEC);  buf->st_dev = info.dwVolumeSerialNumber;  buf->st_ino = (short) info.nFileIndexLow;  buf->st_nlink = (short) info.nNumberOfLinks;  buf->st_uid = 0;  buf->st_gid = 0;  buf->st_rdev = 0;  buf->st_size = (off_t) (((__int64) info.nFileSizeHigh << 32) | 			  info.nFileSizeLow);  buf->st_atime = ft2lt (info.ftLastAccessTime);  buf->st_mtime = ft2lt (info.ftLastWriteTime);  buf->st_ctime = ft2lt (info.ftCreationTime);#endif  /* not __MINGW32__ */  return 0;}/* * Open the file whose name is the string pointed to by @var{file} and  * associates a stream with it. */FILE *svz_fopen (const char *file, const char *mode){  FILE *f;  if ((f = fopen (file, mode)) == NULL)    {      svz_log (LOG_ERROR, "fopen (%s): %s\n", file, SYS_ERROR);      return NULL;    }#ifndef __MINGW32__  if (svz_fd_cloexec (fileno (f)) < 0)    {      fclose (f);      return NULL;    }  svz_file_add (fileno (f));#endif /* __MINGW32__ */  return f;}/* * Dissociates the named stream @var{f} from its underlying file. */intsvz_fclose (FILE *f){#ifndef __MINGW32__  svz_file_del (fileno (f));#endif  if (fclose (f) < 0)    {      svz_log (LOG_ERROR, "fclose: %s\n", SYS_ERROR);      return -1;    }  return 0;}

⌨️ 快捷键说明

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