• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C++ sendfile函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C++中sendfile函数的典型用法代码示例。如果您正苦于以下问题:C++ sendfile函数的具体用法?C++ sendfile怎么用?C++ sendfile使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了sendfile函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。

示例1: client_func


//.........这里部分代码省略.........
      struct put *petition = (struct put *) &bytes;

      // Create file from received data

      // receive size
      char size[10];
      ssize_t bytes_read;
      if((bytes_read = read(client_sd, size, 10)) == -1) {
        perror("c> Error receiving data from client.\n");
        break;
      } else if(bytes_read == 0) {
        printf("c> Client has closed the connection.\n");
        close(client_sd);
        break;
      }                         

      int filesize = atoi(size);
      printf("c> File size - %d\n", filesize);

      //receive actual file data

      char file_data[filesize];
      if((bytes_read = read(client_sd, file_data, filesize)) == -1) {
        perror("c> Error receiving data from client.\n");
        break;
      } else if(bytes_read == 0) {
        printf("c> Client has closed the connection.\n");
        close(client_sd);
        break;
      } 

      //create file
      FILE *file = fopen(petition->src, "wb");
      fwrite(file_data, filesize, 1, file);
      fclose(file);

      // Create graphic
      generate_graphics(petition->title, 
                        petition->xLabel,
                        petition->yLabel,
                        petition->style,
                        petition->src);

      printf("s> %s:%d OK gnuplot %s\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), petition->title);
   
      // Send ACK to client.
      char byte[1];
      byte[0] = PUT_CODE;
			if((bytes_written = write(client_sd, byte, 1)) == -1) {
				perror("s> Could not send data to client.\n");
				break;
			} 

    } else if (bytes[0] == GET_CODE) {
      struct get *petition = (struct get *) &bytes;
      printf("s> %s:%d init get graphic %s\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), petition->file);

      //Open file
      FILE *file;
      char *file_data;
      if ((file = fopen(petition->file, "rb")) != 0){
        fseek(file, 0L, SEEK_END);
        int size = ftell(file);
        fseek(file, 0L, SEEK_SET);

        //Allocate memory
        file_data=(char *)malloc(size+1);

        //Read file contents into buffer
        fread(file_data, size, 1, file);
        fclose(file);

        char filesize[10];
        sprintf(filesize, "%d", size);

        // SEND size
        if((bytes_written = send(client_sd, filesize, 10, 0)) == -1) {
          perror("s> Could not send file size to client.\n");
          break;
        }

        //SEND file
        if((bytes_written = sendfile(client_sd, open(petition->file, 0), 0, size)) == -1) {
          perror("s> Could not send data to client.\n");
          break;
        }

        printf("s> %s:%d end get graphic %s\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), petition->file); 
      } else {
        printf("s> %s:%d error sending file %s\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), petition->file);  
      }
     
    } 

		memset(bytes, 0, sizeof(bytes));
	}

	close(client_sd);
	pthread_exit(0);
} 
开发者ID:alfongj,项目名称:ASAFACMS,代码行数:101,代码来源:server.c


示例2: do_sendfile

static int do_sendfile(const int out_fd, const int in_fd,
                       unsigned int num_send, filesize_t start_pos)
{
  /* Probably should one day be shared with instance in ftpdataio.c */
  static char* p_recvbuf;
  unsigned int total_written = 0;
  int retval;
  enum EVSFSysUtilError error;
  (void) start_pos;
  (void) error;
#if defined(VSF_SYSDEP_HAVE_LINUX_SENDFILE) || \
    defined(VSF_SYSDEP_HAVE_FREEBSD_SENDFILE) || \
    defined(VSF_SYSDEP_HAVE_HPUX_SENDFILE) || \
    defined(VSF_SYSDEP_HAVE_AIX_SENDFILE) || \
    defined(VSF_SYSDEP_HAVE_SOLARIS_SENDFILE)
  if (tunable_use_sendfile)
  {
    static int s_sendfile_checked;
    static int s_runtime_sendfile_works;
    if (!s_sendfile_checked || s_runtime_sendfile_works)
    {
      do
      {
  #ifdef VSF_SYSDEP_HAVE_LINUX_SENDFILE
        retval = sendfile(out_fd, in_fd, NULL, num_send);
  #elif defined(VSF_SYSDEP_HAVE_FREEBSD_SENDFILE)
        {
          /* XXX - start_pos will truncate on 32-bit machines - can we
           * say "start from current pos"?
           */
          off_t written = 0;
          retval = sendfile(in_fd, out_fd, start_pos, num_send, NULL,
                            &written, 0);
          /* Translate to Linux-like retval */
          if (written > 0)
          {
            retval = (int) written;
          }
        }
  #elif defined(VSF_SYSDEP_HAVE_SOLARIS_SENDFILE)
        {
          size_t written = 0;
          struct sendfilevec the_vec;
          vsf_sysutil_memclr(&the_vec, sizeof(the_vec));
          the_vec.sfv_fd = in_fd;
          the_vec.sfv_off = start_pos;
          the_vec.sfv_len = num_send;
          retval = sendfilev(out_fd, &the_vec, 1, &written);
          /* Translate to Linux-like retval */
          if (written > 0)
          {
            retval = (int) written;
          }
        }
  #elif defined(VSF_SYSDEP_HAVE_AIX_SENDFILE)
        {
          struct sf_parms sf_iobuf;
          vsf_sysutil_memclr(&sf_iobuf, sizeof(sf_iobuf));
          sf_iobuf.header_data = NULL;
          sf_iobuf.header_length = 0;
          sf_iobuf.trailer_data = NULL;
          sf_iobuf.trailer_length = 0;
          sf_iobuf.file_descriptor = in_fd;
          sf_iobuf.file_offset = start_pos;
          sf_iobuf.file_bytes = num_send;

          retval = send_file((int*)&out_fd, &sf_iobuf, 0);
          if (retval >= 0)
          {
            retval = sf_iobuf.bytes_sent;
          }
        }
  #else /* must be VSF_SYSDEP_HAVE_HPUX_SENDFILE */
        {
          retval = sendfile(out_fd, in_fd, start_pos, num_send, NULL, 0);
        }
  #endif /* VSF_SYSDEP_HAVE_LINUX_SENDFILE */
        error = vsf_sysutil_get_error();
        vsf_sysutil_check_pending_actions(kVSFSysUtilIO, retval, out_fd);
      }
      while (vsf_sysutil_retval_is_error(retval) &&
             error == kVSFSysUtilErrINTR);
      if (!s_sendfile_checked)
      {
        s_sendfile_checked = 1;
        if (!vsf_sysutil_retval_is_error(retval) ||
            error != kVSFSysUtilErrNOSYS)
        {
          s_runtime_sendfile_works = 1;
        }
      }
      if (!vsf_sysutil_retval_is_error(retval))
      {
        return retval;
      }
      if (s_runtime_sendfile_works && error != kVSFSysUtilErrINVAL &&
          error != kVSFSysUtilErrOPNOTSUPP)
      {
        return retval;
      }
//.........这里部分代码省略.........
开发者ID:wingedboar,项目名称:rtl819x-toolchain,代码行数:101,代码来源:sysdeputil.c


示例3: http_response_static_proc

//response static resource
void http_response_static_proc( httpHeader* reqHeader )
{
	int len, cllen , ctlen ;
	char path[1024] = {0};
	header_out_t  header_out;
	memset( &header_out , 0 , sizeof( header_out ));
	header_out.req = reqHeader;

	get_file_path( reqHeader->uri , path );
	struct stat stat_file;
	int ret =  stat( path , &stat_file );

	if ( ret < 0 )
	{
		//printf( "not found page=%s \n" , path );
		resp_error_page( &header_out , 404 );
		return;
	}

	create_common_header( &header_out , 200 );
	header_append_length( &header_out , stat_file.st_size );
	resp_append_header( &header_out , HEADER_END_LINE );

	int nwritten = write( reqHeader->connfd , header_out.data , header_out.length );
	if (nwritten <= 0)
	{
		printf( "I/O error writing to client connfd=%d,len=%d: %s \n", reqHeader->connfd , header_out.length , strerror(errno));
		return;
	}

	int fd = open( path , O_RDONLY );
	if ( fd < 0 )
	{
		printf( "Open file Error:%s,errno=%d \n" , strerror(errno) , errno );
		return;
	}
    //优化
	//setsockopt (fd, SOL_TCP, TCP_CORK, &on, sizeof (on));
	off_t offset = 0;
	int force_close = 0;
	while ( offset < stat_file.st_size  )
	{
		int sendn = sendfile( reqHeader->connfd , fd , &offset , stat_file.st_size - offset );
		if ( sendn < 0 )
		{
			//如果socket缓冲区不可用,则挂起等待可用
			if (errno == EAGAIN || errno == EINTR  )
			{
				if( anetHandup(  reqHeader->connfd , 1000 , AE_WRITABLE ) < 0 )
				{
					//如果超时,退出
					printf( "Sendfile anetHandup timeout.......\n" );
					force_close = 1;
					break;
				}
				else
				{
					//否则继续发送
					continue;
				}
			}
			else
			{
				break;
			}
		}
		//printf( "Response uri=%s, connfd=%d,len=%d,send=%d \n", reqHeader->uri , reqHeader->connfd ,stat_file.st_size  , sendn );
	}
	close( fd );
	http_close( reqHeader , force_close );
}
开发者ID:tla001,项目名称:appnet_php7,代码行数:72,代码来源:http_response.c


示例4: cherokee_socket_sendfile

ret_t
cherokee_socket_sendfile (cherokee_socket_t *socket,
                          int      fd,
                          size_t   size,
                          off_t   *offset,
                          ssize_t *sent)
{
	static cherokee_boolean_t no_sys = false;
#if DARWIN_SENDFILE_API || FREEBSD_SENDFILE_API
	int                       re;
#endif
#if DARWIN_SENDFILE_API
	off_t                     _sent  = size;
#endif

	/* Exit if there is no sendfile() function in the system
	 */
	if (unlikely (no_sys))
		return ret_no_sys;

	/* If there is nothing to send then return now, this may be
	 * needed in some systems (i.e. *BSD) because value 0 may have
	 * special meanings or trigger occasional hidden bugs.
	 */
	if (unlikely (size == 0))
		return ret_ok;

	/* Limit size of data that has to be sent.
	 */
	if (size > MAX_SF_BLK_SIZE2)
		size = MAX_SF_BLK_SIZE;

#if defined(LINUX_BROKEN_SENDFILE_API)
	UNUSED(socket);
	UNUSED(fd);
	UNUSED(offset);
	UNUSED(sent);

	/* Large file support is set but native Linux 2.2 or 2.4 sendfile()
	 * does not support _FILE_OFFSET_BITS 64
	 */
	return ret_no_sys;

#elif defined(LINUX_SENDFILE_API)

	/* Linux sendfile
	 *
	 * ssize_t
	 * sendfile (int out_fd, int in_fd, off_t *offset, size_t *count);
	 *
	 * ssize_t
	 * sendfile64 (int out_fd, int in_fd, off64_t *offset, size_t *count);
	 */
	*sent = sendfile (SOCKET_FD(socket),     /* int     out_fd */
	                  fd,                    /* int     in_fd  */
	                  offset,                /* off_t  *offset */
	                  size);                 /* size_t  count  */

	if (*sent < 0) {
		switch (errno) {
		case EINTR:
		case EAGAIN:
#if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
		case EWOULDBLOCK:
#endif
			return ret_eagain;
		case EINVAL:
			/* maybe sendfile is not supported by this FS (no mmap available),
			 * since more than one FS can be used (ext2, ext3, ntfs, etc.)
			 * we should retry with emulated sendfile (read+write).
			 */
			return ret_no_sys;
		case ENOSYS:
			/* This kernel does not support sendfile at all.
			 */
			no_sys = true;
			return ret_no_sys;
		}

		return ret_error;

	} else if (*sent == 0) {
		/* It isn't an error, but it wrote nothing */
		return ret_error;
	}

#elif DARWIN_SENDFILE_API

	/* MacOS X: BSD-like System Call
	 *
	 * int
	 * sendfile (int fd, int s, off_t offset, off_t *len,
	 *           struct sf_hdtr *hdtr, int flags);
	 */
	re = sendfile (fd,                        /* int             fd     */
	               SOCKET_FD(socket),         /* int             s      */
	               *offset,                   /* off_t           offset */
	               &_sent,                    /* off_t          *len    */
	               NULL,                      /* struct sf_hdtr *hdtr   */
	               0);                        /* int             flags  */
//.........这里部分代码省略.........
开发者ID:304471720,项目名称:webserver,代码行数:101,代码来源:socket.c


示例5: cacheopen

static void* cacheopen(char const* url) {
    const char* basename = strrchr(url,'/');
    assert(basename);

    static char dest[508];
    snprintf(dest,508,"%s/%s",cacheDir,basename);

    bool didtemp = false;

    int doopen(void) {
        int result = open(dest,O_RDONLY);
        if(result<0) {
            char temp[512];
            snprintf(temp,512,"%s.temp",dest);

            if(xmlIOHTTPMatch(url)) {
                void* ctx = NULL;
#if W3_ARE_NOT_MORONS
                ctx = xmlNanoHTTPOpen(url, NULL);
                if(xmlNanoHTTPReturnCode(ctx) != 200)
#else
                if(true)
#endif
                {
                    // XXX: it always is... w3.org dies on HTTP/1.0
#if W3_ARE_NOT_MORONS
                    xmlNanoHTTPClose(ctx);
#endif
                    fprintf(stderr,"Curl fallback for %s\n",url);
                    int pid = fork();
                    if(pid == 0) {
                        execlp("curl","curl","-o",temp,url,NULL);
                        abort();
                    }
                    int status = 0;
                    waitpid(pid,&status,0);
                    if(!(WIFEXITED(status) && (0 == WEXITSTATUS(status)))) {
                        fprintf(stderr,"CUrl failed! %x %d\n",status,WEXITSTATUS(status));
                        abort();
                    }
                } else {
                    assert(0==xmlNanoHTTPSave(ctx,temp));
                }
            } else if(xmlIOFTPMatch(url)) {
                void* ftp = xmlNanoFTPOpen(url);
                int out = open(temp,O_WRONLY|O_TRUNC|O_CREAT,0644);
                assert(out>0);
                char buf[0x1000];
                for(;;) {
                    int amt = xmlNanoFTPRead(ftp, buf, 0x1000);
                    if(amt==0) break;
                    assert(amt>0);
                    write(out,buf,amt);
                }
                close(out);            
            } else {
                FILE* fp = xmlFileOpen(url);
                struct stat fpstat;
                if(!fp) {
                    fprintf(stderr,"No idea what to do with url %s\n",url);
                    abort();
                }
                int inp = fileno(fp);
                assert(0==fstat(inp,&fpstat));
                off_t left = fpstat.st_size;
                int out = open(temp,O_WRONLY|O_TRUNC|O_CREAT,0644);
                assert(out>0);
                do {
                    ssize_t amt = sendfile(out,inp,NULL,left);
                    if(amt<0) {
                        perror(url);
                    }
                    assert(amt>=0);
                    left -= amt;
                } while(left > 0);
                fclose(fp);
                close(out);
            }
            rename(temp,dest); // doesn't matter if fails
            unlink(temp); // in case it failed
            return doopen();
        }
        return result;
    }
开发者ID:cyisfor,项目名称:htmlish,代码行数:84,代码来源:input.c


示例6: sendfile_full

ssize_t sendfile_full(int out_fd, const char *fn) {
        _cleanup_fclose_ FILE *f;
        struct stat st;
        int r;
        ssize_t s;

        size_t n, l;
        _cleanup_free_ char *buf = NULL;

        assert(out_fd > 0);
        assert(fn);

        f = fopen(fn, "re");
        if (!f)
                return -errno;

        r = fstat(fileno(f), &st);
        if (r < 0)
                return -errno;

        s = sendfile(out_fd, fileno(f), NULL, st.st_size);
        if (s < 0)
                if (errno == EINVAL || errno == ENOSYS) {
                        /* continue below */
                } else
                        return -errno;
        else
                return s;

        /* sendfile() failed, fall back to read/write */

        /* Safety check */
        if (st.st_size > 4*1024*1024)
                return -E2BIG;

        n = st.st_size > 0 ? st.st_size : LINE_MAX;
        l = 0;

        while (true) {
                char *t;
                size_t k;

                t = realloc(buf, n);
                if (!t)
                        return -ENOMEM;

                buf = t;
                k = fread(buf + l, 1, n - l, f);

                if (k <= 0) {
                        if (ferror(f))
                                return -errno;

                        break;
                }

                l += k;
                n *= 2;

                /* Safety check */
                if (n > 4*1024*1024)
                        return -E2BIG;
        }

        r = write(out_fd, buf, l);
        if (r < 0)
                return -errno;

        return (ssize_t) l;
}
开发者ID:Drakey83,项目名称:steamlink-sdk,代码行数:70,代码来源:fileio.c


示例7: client_test


//.........这里部分代码省略.........
    fdin = sockfd = -1;
    write_count = 0;

    size = getsize(filename);

    /* Save errno, as writing to a stream can change it in the
     * interim.
     */

    tmperr = errno;

    if(size <= 0)
    {
        switch(size)
        {
        case 0:
            die(err, EXIT_FAILURE,
                "%s: %s: Cowardly refusing to send an empty file!\n",
                program, filename);
            break;

        case -1:
            die(err, tmperr,
                "%s: Could not open \"%s\": %s\n",
                program, filename, strerror(errno));
            break;

        default:
            die(err, EXIT_FAILURE,
                "%s: Could not open \"%s\"\n",
                program, filename);
            break;
        }
    }

    if(!tcp_connect(host, atoi(port), &sockfd))
    {
        tmperr = errno;
        switch(sockfd)
        {
        case -1:
            die(err, EXIT_FAILURE,
                "Error opening socket.\n");
            break;

        case -2:
            die(err, EXIT_FAILURE,
                "%s: Hostname lookup failure: %s\n",
                program, host);
            break;

        case -3:
            die(err, tmperr,
                "%s: error connecting to %s, port %s.\n",
                program, host, port);
            break;

        default:    /* If we get here, the API is busted. */
            die(err, EXIT_FAILURE,
                "%s: Unknown error!\n", program);
            break;

        }
    }

    /* Read file and send to server */

    if((fdin = open(filename, O_RDONLY)) == -1)
    {
        die(err, errno,
            "%s: couldn't open %s for reading: %s\n",
            program, filename, strerror(errno));
    }
    else
    {
        /* Toss file to server. Linux syscall, but its fast. :D*/
        sendfile(sockfd, fdin, &offset, size);

        if(wait_for_ack)
        {
            ack = tcp_recv(sockfd, 5000, MAX_READ, &req);
            if(ack==NULL)
                return false;

            if((ack!=NULL) &&
                    (strlen(ack) > 0))
            {
                fprintf(stderr, "%s\n", ack);
                check = parse(ack);
            }
            free(ack);
            ack = NULL;
            close(sockfd);

            return check;
        } /* if waiting for ack ... */
        close(sockfd);
    } /* if able to open filestream... */
    return true;
}
开发者ID:87fabio,项目名称:hl7libc,代码行数:101,代码来源:client_test.c


示例8: do_sendfile

void do_sendfile(OFF_T offset, int i)
{
	int in_fd;
	struct stat sb;
	int wait_status;
	int wait_stat;
	off_t before_pos, after_pos;

	out_fd = create_server();

	if ((in_fd = open(in_file, O_RDONLY)) < 0) {
		tst_brkm(TBROK, cleanup, "open failed: %d", errno);
	 /*NOTREACHED*/}
	if (stat(in_file, &sb) < 0) {
		tst_brkm(TBROK, cleanup, "stat failed: %d", errno);
	 /*NOTREACHED*/}

	if ((before_pos = lseek(in_fd, 0, SEEK_CUR)) < 0) {
		tst_brkm(TBROK, cleanup,
			 "lseek before invoking sendfile failed: %d", errno);
	 /*NOTREACHED*/}

	TEST(sendfile(out_fd, in_fd, &offset, sb.st_size - offset));

	if ((after_pos = lseek(in_fd, 0, SEEK_CUR)) < 0) {
		tst_brkm(TBROK, cleanup,
			 "lseek after invoking sendfile failed: %d", errno);
	 /*NOTREACHED*/}

	if (STD_FUNCTIONAL_TEST) {
		/* Close the sockets */
		shutdown(sockfd, SHUT_RDWR);
		shutdown(s, SHUT_RDWR);
		if (TEST_RETURN != testcases[i].exp_retval) {
			tst_resm(TFAIL, "sendfile(2) failed to return "
				 "expected value, expected: %d, "
				 "got: %ld", testcases[i].exp_retval,
				 TEST_RETURN);
			kill(child_pid, SIGKILL);
		} else if (offset != testcases[i].exp_updated_offset) {
			tst_resm(TFAIL, "sendfile(2) failed to update "
				 "OFFSET parameter to expected value, "
				 "expected: %d, got: %"PRId64,
				 testcases[i].exp_updated_offset, (int64_t)offset);
		} else if (before_pos != after_pos) {
			tst_resm(TFAIL, "sendfile(2) updated the file position "
				 " of in_fd unexpectedly, expected file position: %"PRId64", "
				 " actual file position %"PRId64,
				 (int64_t)before_pos, (int64_t)after_pos);
		} else {
			tst_resm(TPASS, "functionality of sendfile() is "
				 "correct");
			wait_status = waitpid(-1, &wait_stat, 0);
		}
	} else {
		tst_resm(TPASS, "call succeeded");
		/* Close the sockets */
		shutdown(sockfd, SHUT_RDWR);
		shutdown(s, SHUT_RDWR);
		if (TEST_RETURN != testcases[i].exp_retval) {
			kill(child_pid, SIGKILL);
		} else {
			wait_status = waitpid(-1, &wait_stat, 0);
		}
	}

	close(in_fd);

}
开发者ID:ystk,项目名称:debian-ltp,代码行数:69,代码来源:sendfile02.c


示例9: network_write_chunkqueue_linuxsendfile

int network_write_chunkqueue_linuxsendfile(server *srv, connection *con, int fd, chunkqueue *cq, off_t max_bytes) {
	chunk *c;

	for(c = cq->first; (max_bytes > 0) && (NULL != c); c = c->next) {
		int chunk_finished = 0;

		switch(c->type) {
		case MEM_CHUNK: {
			char * offset;
			off_t toSend;
			ssize_t r;

			size_t num_chunks, i;
			struct iovec chunks[UIO_MAXIOV];
			chunk *tc;
			size_t num_bytes = 0;

			/* build writev list
			 *
			 * 1. limit: num_chunks < UIO_MAXIOV
			 * 2. limit: num_bytes < max_bytes
			 */
			for (num_chunks = 0, tc = c;
			     tc && tc->type == MEM_CHUNK && num_chunks < UIO_MAXIOV;
			     tc = tc->next, num_chunks++);

			for (tc = c, i = 0; i < num_chunks; tc = tc->next, i++) {
				if (buffer_string_is_empty(tc->mem)) {
					chunks[i].iov_base = tc->mem->ptr;
					chunks[i].iov_len  = 0;
				} else {
					offset = tc->mem->ptr + tc->offset;
					toSend = buffer_string_length(tc->mem) - tc->offset;

					chunks[i].iov_base = offset;

					/* protect the return value of writev() */
					if (toSend > max_bytes ||
					    (off_t) num_bytes + toSend > max_bytes) {
						chunks[i].iov_len = max_bytes - num_bytes;

						num_chunks = i + 1;
						break;
					} else {
						chunks[i].iov_len = toSend;
					}

					num_bytes += toSend;
				}
			}

			if ((r = writev(fd, chunks, num_chunks)) < 0) {
				switch (errno) {
				case EAGAIN:
				case EINTR:
					r = 0;
					break;
				case EPIPE:
				case ECONNRESET:
					return -2;
				default:
					log_error_write(srv, __FILE__, __LINE__, "ssd",
							"writev failed:", strerror(errno), fd);

					return -1;
				}
			}

			/* check which chunks have been written */
			cq->bytes_out += r;
			max_bytes -= r;

			for(i = 0, tc = c; i < num_chunks; i++, tc = tc->next) {
				if (r >= (ssize_t)chunks[i].iov_len) {
					/* written */
					r -= chunks[i].iov_len;
					tc->offset += chunks[i].iov_len;

					if (chunk_finished) {
						/* skip the chunks from further touches */
						c = c->next;
					} else {
						/* chunks_written + c = c->next is done in the for()*/
						chunk_finished = 1;
					}
				} else {
					/* partially written */

					tc->offset += r;
					chunk_finished = 0;

					break;
				}
			}

			break;
		}
		case FILE_CHUNK: {
			ssize_t r;
			off_t offset;
//.........这里部分代码省略.........
开发者ID:automatical,项目名称:lighttpd1.4,代码行数:101,代码来源:network_linux_sendfile.c


示例10: portable_sendfile

 ssize_t portable_sendfile(int out_fd, int in_fd) {
   off_t len = SENDFILE_CHUNK_SIZE;
   if(sendfile(in_fd, out_fd, 0, &len, NULL, 0) == -1)
     return -1;
   return len;
 }
开发者ID:Shumen,项目名称:bjoern,代码行数:6,代码来源:portable_sendfile.c


示例11: main

int main(int argc, char *argv[]){

	int listenfd,connfd,n;
	int epoll_fd,cur_fd,newfd,number,stop;
	struct epoll_event ev;
	struct epoll_event events[size];
	struct sockaddr_in server_address,client_address;
	struct stat stat_buf;
	const char* file_name=argv[1];
	char buffer[BUF];

	int file_ptr=open(file_name,O_RDONLY);
	fstat(file_ptr,&stat_buf);
	close(file_ptr);
	bzero(&server_address,sizeof(server_address));
	server_address.sin_family=AF_INET;
	server_address.sin_addr.s_addr=htonl(INADDR_ANY);
	server_address.sin_port=htons(PORT);

	if((listenfd=socket(AF_INET,SOCK_STREAM,0))==-1){
		exit(0);
	}
	setnonblocking(listenfd);
	int opt=1;
	setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
	if(bind(listenfd,(struct sockaddr*)&server_address,sizeof(server_address))==-1){
		exit(0);
	}
	if(listen(listenfd,BACKLOG)==-1){
		exit(0);
	}

	epoll_fd=epoll_create(10);
	ev.data.fd=listenfd;
	ev.events=EPOLLIN | EPOLLET;
	epoll_ctl(epoll_fd,EPOLL_CTL_ADD,listenfd,&ev);

	cur_fd=1;stop=FALSE;
	socklen_t len=sizeof(struct sockaddr_in);

	while(!stop){
		number=epoll_wait(epoll_fd,events,cur_fd,-1);
		assert(number!=-1);
		for(n=0;n<number;n++){

			connfd=events[n].data.fd;

			if(connfd==listenfd){

				while((newfd=accept(listenfd,(struct sockaddr*)&client_address,&len))>0){
					printf("connect with %s ,socket index %d\n",\
								inet_ntoa(client_address.sin_addr),newfd);
					setnonblocking(newfd);
					ev.data.fd=newfd;
					ev.events=EPOLLIN|EPOLLET|EPOLLHUP;
					if(epoll_ctl(epoll_fd,EPOLL_CTL_ADD,newfd,&ev)==-1){
						perror("epoll_ctl:add");
						stop=TRUE;
					}
					cur_fd++;
				}

				if(newfd==-1){
					if(errno!=EAGAIN&&errno!=ECONNABORTED\
								&&errno!=EPROTO&&errno!=EINTR){
						perror("accept");
					}
				}

				continue;

			}else if(events[n].events & EPOLLOUT){

				printf("start to sendfile !\n");
				int ret=0,left=stat_buf.st_size;
				file_ptr=open(file_name,O_RDONLY);

				while(left>0){
					ret=sendfile(connfd,file_ptr,NULL,BUF);
					if(ret<0 && errno==EAGAIN){
						continue;
					}else if(ret==0){
						break;
					}else{
						left-=ret;
					}
				}

				printf("sendfile over !\n");
				close(file_ptr);

				ev.data.fd=connfd;
				epoll_ctl(epoll_fd,EPOLL_CTL_DEL,connfd,&ev);

				cur_fd--;
				close(connfd);

			}else if(events[n].events & EPOLLIN){

				char msg[100];
//.........这里部分代码省略.........
开发者ID:Sim-szm,项目名称:Some_Code_Gather,代码行数:101,代码来源:server.c


示例12: spamdscan_socket

static int 
spamdscan_socket(const char *file, const struct spamd_server *srv, struct config_file *cfg, rspamd_result_t *res)
{
#ifdef HAVE_PATH_MAX
	char buf[PATH_MAX + 10];
#elif defined(HAVE_MAXPATHLEN)
	char buf[MAXPATHLEN + 10];
#else
#error "neither PATH_MAX nor MAXPATHEN defined"
#endif
	char *c, *err;
	struct sockaddr_un server_un;
	struct sockaddr_in server_in;
	int s, r, fd, ofl, size = 0;
	struct stat sb;
	struct rspamd_metric_result *cur = NULL;
	struct rspamd_symbol *cur_symbol;

	/* somebody doesn't need reply... */
	if (!srv)
		return 0;

	if (srv->sock_type == AF_LOCAL) {

		memset(&server_un, 0, sizeof(server_un));
		server_un.sun_family = AF_UNIX;
		strncpy(server_un.sun_path, srv->sock.unix_path, sizeof(server_un.sun_path));

		if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
			msg_warn("spamd: socket %s, %s", srv->sock.unix_path,
					strerror (errno));
			return -1;
		}
		if (connect_t(s, (struct sockaddr *) & server_un, sizeof(server_un), cfg->spamd_connect_timeout) < 0) {
			msg_warn("spamd: connect %s, %s", srv->sock.unix_path,
					strerror (errno));
			close(s);
			return -1;
		}
	} else {
		/* inet hostname, send stream over tcp/ip */

		memset(&server_in, 0, sizeof(server_in));
		server_in.sin_family = AF_INET;
		server_in.sin_port = srv->sock.inet.port;
		memcpy((char *)&server_in.sin_addr, &srv->sock.inet.addr, sizeof(struct in_addr));

		if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
			msg_warn("spamd: socket %s", strerror (errno));
			return -1;
		}
		if (connect_t(s, (struct sockaddr *) & server_in, sizeof(server_in), cfg->spamd_connect_timeout) < 0) {
			msg_warn("spamd: connect %s, %s", srv->name, strerror (errno));
			close(s);
			return -1;
		}
	}
	/* Get file size */
	fd = open(file, O_RDONLY);
	if (fstat (fd, &sb) == -1) {
		msg_warn ("spamd: stat failed: %s", strerror (errno));
		close(s);
		return -1;
	}
	
	if (poll_fd(s, cfg->spamd_connect_timeout, POLLOUT) < 1) {
		msg_warn ("spamd: timeout waiting writing, %s", srv->name);
		close (s);
		return -1;
	}
	/* Set blocking again */
	ofl = fcntl(s, F_GETFL, 0);
	fcntl(s, F_SETFL, ofl & (~O_NONBLOCK));

	r = snprintf (buf, sizeof (buf), "SYMBOLS SPAMC/1.2\r\nContent-length: %ld\r\n\r\n", (long int)sb.st_size);
	if (write (s, buf, r) == -1) {
		msg_warn("spamd: write (%s), %s", srv->name, strerror (errno));
		close(fd);
		close(s);
		return -1;
	}

#ifdef HAVE_SENDFILE
#if defined(FREEBSD)
	if (sendfile(fd, s, 0, 0, 0, 0, 0) != 0) {
		msg_warn("spamd: sendfile (%s), %s", srv->name, strerror (errno));
		close(fd);
		close(s);
		return -1;
	}
#elif defined(LINUX)
	off_t off = 0;
	if (sendfile(s, fd, &off, sb.st_size) == -1) {
		msg_warn("spamd: sendfile (%s), %s", srv->name, strerror (errno));
		close(fd);
		close(s);
		return -1;		
	}
#endif
#else 
//.........这里部分代码省略.........
开发者ID:heartshare,项目名称:rmilter,代码行数:101,代码来源:libspamd.c


示例13: rspamdscan_socket


//.........这里部分代码省略.........
	}
	if (priv->priv_user[0] != '\0') {
		to_write = sizeof (buf) - r;
		written = snprintf (buf + r, to_write, "User: %s\r\n", priv->priv_user);
		if (written > to_write) {
			msg_warn("rspamd: buffer overflow while filling buffer (%s)", srv->name);
			close(fd);
			close(s);
			return -1;
		}
		r += written;
	}
	to_write = sizeof (buf) - r;
	written = snprintf (buf + r, to_write, "Queue-ID: %s\r\n\r\n", priv->mlfi_id);
	if (written > to_write) {
		msg_warn("rspamd: buffer overflow while filling buffer (%s)", srv->name);
		close(fd);
		close(s);
		return -1;
	}
	r += written;



	if (write (s, buf, r) == -1) {
		msg_warn("rspamd: write (%s), %s", srv->name, strerror (errno));
		close(fd);
		close(s);
		return -1;
	}

#ifdef HAVE_SENDFILE
#if defined(FREEBSD)
	if (sendfile(fd, s, 0, 0, 0, 0, 0) != 0) {
		msg_warn("rspamd: sendfile (%s), %s", srv->name, strerror (errno));
		close(fd);
		close(s);
		return -1;
	}
#elif defined(LINUX)
	off_t off = 0;
	if (sendfile(s, fd, &off, sb.st_size) == -1) {
		msg_warn("rspamd: sendfile (%s), %s", srv->name, strerror (errno));
		close(fd);
		close(s);
		return -1;		
	}
#endif
#else 
	while ((r = read (fd, buf, sizeof (buf))) > 0) {
		write (s, buf, r);
	}
#endif

	fcntl(s, F_SETFL, ofl);
	close(fd);

	/* wait for reply */

	if (poll_fd(s, cfg->spamd_results_timeout, POLLIN) < 1) {
		msg_warn("rspamd: timeout waiting results %s", srv->name);
		close(s);
		return -1;
	}
	
	/*
开发者ID:heartshare,项目名称:rmilter,代码行数:67,代码来源:libspamd.c


示例14: file_read_checks

static void file_read_checks(const char *path)
{
	char buf1[16],buf2[8],buf3[32];
	struct iovec vec[3];
	int rc, f, cnt;
	off_t i;

	mlog("+open");
	f = open(path, O_RDONLY);
	if (f < 0)
		return;

	// first look at the file's attributes
	mlog("+fstat");
	rc = fstat(f, &sbuf);
#ifdef CHECK_XATTR
	mlog("+flistxattr");
	flistxattr(f, lbuf, MAXLISTBUF);
	mlog("+fgetxattr");
	fgetxattr(f, "selinux", lbuf, MAXLISTBUF);
#endif
//	readahead(f, NULL

	// Check reading
	mlog("+read");
	cnt = 0;
	while((read(f, buf3, sizeof(buf3)) > 0) && cnt < 1000)
		cnt++;

	// lseek around
	mlog("+lseek");
	if (rc == 0) 
		lseek(f, sbuf.st_size, SEEK_SET);
	lseek(f, 100000, SEEK_SET);
	lseek(f, 0, SEEK_SET);

	// vectored reads
	vec[0].iov_base = (void*)&buf1;
	vec[0].iov_len = sizeof(buf1);
	vec[1].iov_base = (void*)&buf2;
	vec[1].iov_len = sizeof(buf2);
	vec[2].iov_base = (void*)&buf3;
	vec[2].iov_len = sizeof(buf3);
	mlog("+readv");
	cnt = 0;
	while((readv(f, vec, 3) > 0) && cnt < 1000)
		cnt++;

	// check out pread syscall
	i = 0;
	mlog("+pread");
	cnt = 0;
	while ((pread(f, buf1, sizeof(buf1), i) > 0) && cnt < 1000) {
		i += sizeof(buf1)*2;
		cnt++;
	}

	// flock
	mlog("+flock");
	flock(f, LOCK_SH|LOCK_NB);
	flock(f, LOCK_UN);

	// fcntl ?

	// sendfile to localhost:discard
#if defined(__linux__)
	setup_socket();
	if (sfd >= 0) {
		mlog("+sendfile");
		lseek(f, 0, SEEK_SET);
		sendfile(sfd, f, NULL, rc ? 100000 : sbuf.st_size);
		close(sfd);
		sfd = -1;
	}
#endif

	// mmap each file
	if (rc == 0) {
		char *src;
		mlog("+mmap");
		src = mmap(0, sbuf.st_size, PROT_READ, MAP_FILE | MAP_SHARED,
			f, 0);
		if (src != (char *)-1) {
			// Don't touch memory...or Mr. SIGBUS will visit you
			mlog("+munmap");
			munmap(src, sbuf.st_size);
		}
	}

	close(f);
}
开发者ID:cherry-wb,项目名称:segvault,代码行数:91,代码来源:fstest.c


示例15: do_retr

static void do_retr(session_t *sess)
{
	if(get_transfer_fd(sess) == 0)
                return;
	
	int fd = open(sess->arg, O_RDONLY);
	if(fd == -1)
	{
		ftp_reply(sess, FTP_FILEFAIL, "1Failed to open file.");
		return;
	}

	int ret;
	ret = lock_file_read(fd);	
	if(ret == -1)
	{
		ftp_reply(sess, FTP_FILEFAIL, "Failed to open file.");
                return;		
	}

	//device file can not be downloaded
	struct stat sbuf;
	ret = fstat(fd, &sbuf);
	if(!S_ISREG(sbuf.st_mode))
	{
		ftp_reply(sess, FTP_FILEFAIL, "Failed to open file.");
                return;
	}
	
	long long offset = sess->restart_pos;
        sess->restart_pos = 0;

	if(offset != 0)
	{
		ret = lseek(fd, offset, SEEK_SET);
		if(ret == -1)
		{
			ftp_reply(sess, FTP_FILEFAIL, "Failed to open file.");
	                return;
		}
	}

	char text[1024] = {0};
	if(sess->is_ascii)
	{
		sprintf(text, "Opening ASCII mode data conection for %s (%lld bytes).",
                sess->arg, (long long)sbuf.st_size);
	}
	else
	{
		sprintf(text, "Opening BINARY mode data conection for %s (%lld bytes).",
		sess->arg, (long long)sbuf.st_size);
	}	

        ftp_reply(sess, FTP_DATACONN, text);

	int flag = 0;
	long long bytes_to_send = sbuf.st_size;

	if(offset > bytes_to_send)
		bytes_to_send = 0;
	else
		bytes_to_send -= offset;

	sess->bw_transfer_start_sec = get_time_sec();
	sess->bw_transfer_start_usec = get_time_usec();

	while(bytes_to_send)
	{
		int num_this_time = bytes_to_send > 65536 ? 65536 : bytes_to_send;
		ret = sendfile(sess->data_fd, fd, NULL, num_this_time);
		if(ret == -1)
		{
			flag = 2;
			break;
		}
		
		limit_rate(sess, ret, 0);
		if(sess->abor_received)
		{
			flag = 2;
			break;
		}	

		bytes_to_send -= ret;
	}

        close(sess->data_fd);
        sess->data_fd = -1;
	close(fd);

	if(flag == 0)
        	ftp_reply(sess, FTP_TRANSFEROK, "Transfer complete.");

	else if(flag == 1)
		ftp_reply(sess, FTP_BADSENDFILE, "Failure reading from local file.");

	else if(flag == 2)
		ftp_reply(sess, FTP_BADSENDNET, "Failure writing to network stream.");

//.........这里部分代码省略.........
开发者ID:dansutj,项目名称:miniftpd,代码行数:101,代码来源:ftpproto.c


示例16: tcpsendfile_ex

int tcpsendfile_ex(int sock, const char *filename, const int64_t file_offset, \
	const int64_t file_bytes, const int timeout, int64_t *total_send_bytes)
{
	int fd;
	int64_t send_bytes;
	int result;
	int flags;
#ifdef USE_SENDFILE
   #if defined(OS_FREEBSD) || defined(OS_LINUX)
	off_t offset;
	#ifdef OS_LINUX
	int64_t remain_bytes;
	#endif
   #endif
#else
	int64_t remain_bytes;
#endif

	fd = open(filename, O_RDONLY);
	if (fd < 0)
	{
		*total_send_bytes = 0;
		return errno != 0 ? errno : EACCES;
	}

	flags = fcntl(sock, F_GETFL, 0);
	if (flags < 0)
	{
		*total_send_bytes = 0;
		return errno != 0 ? errno : EACCES;
	}

#ifdef USE_SENDFILE

	if (flags & O_NONBLOCK)
	{
		if (fcntl(sock, F_SETFL, flags & ~O_NONBLOCK) == -1)
		{
			*total_send_bytes = 0;
			return errno != 0 ? errno : EACCES;
		}
	}

#ifdef OS_LINUX
	/*
	result = 1;
	if (setsockopt(sock, SOL_TCP, TCP_CORK, &result, sizeof(int)) < 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"setsockopt failed, errno: %d, error info: %s.", \
			__LINE__, errno, STRERROR(errno));
		close(fd);
		*total_send_bytes = 0;
		return errno != 0 ? errno : EIO;
	}
	*/

#define FILE_1G_SIZE    (1 * 1024 * 1024 * 1024)

	result = 0;
	offset = file_offset;
	remain_bytes = file_bytes;
	while (remain_bytes > 0)
	{
		if (remain_bytes > FILE_1G_SIZE)
		{
			send_bytes = sendfile(sock, fd, &offset, FILE_1G_SIZE);
		}
		else
		{
			send_bytes = sendfile(sock, fd, &offset, remain_bytes);
		}

		if (send_bytes <= 0)
		{
			result = errno != 0 ? errno : EIO;
			break;
		}

		remain_bytes -= send_bytes;
	}

	*total_send_bytes = file_bytes - remain_bytes;
#else
#ifdef OS_FREEBSD
	offset = file_offset;
	if (sendfile(fd, sock, offset, file_bytes, NULL, NULL, 0) != 0)
	{
		*total_send_bytes = 0;
		result = errno != 0 ? errno : EIO;
	}
	else
	{
		*total_send_bytes = file_bytes;
		result = 0;
	}
#endif
#endif

	if (flags & O_NONBLOCK)  //restore
//.........这里部分代码省略.........
开发者ID:aducode,项目名称:fastdfs,代码行数:101,代码来源:sockopt.c


示例17: main

void main(int argc,char *argv[]) {
    int going=TRUE,forsok=2,car=1,x,connectbps, i, tmp;
    struct NodeType *nt;
    char *tmppscreen,commandstring[100], configname[50] = "NiKom:DatoCfg/SerNode.cfg";
    FILE *fil;
    if(argc>1) for(x=1; x<argc; x++) {
            if(argv[x][0]=='-') {
                if(argv[x][1]=='G') getty=TRUE;
                else if(argv[x][1]=='B') gettybps=atoi(&argv[x][2]);
                else if(argv[x][1]=='C') connectbps = atoi(&argv[x][2]);
            } else strcpy(configname,argv[x]);
        }
    if(!(IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",0)))
        cleanup(ERROR,"Kunde inte öppna intuition.library\n");
    if(!(UtilityBase=OpenLibrary("utility.library",37L)))
        cleanup(ERROR,"Kunde inte öppna utility.library\n");
    if(!(NiKomBase=OpenLibrary("nikom.library",0L)))
        cleanup(ERROR,"Kunde inte öppna nikom.library\n");
    if(!initnode(NODSER)) cleanup(ERROR,"Kunde inte registrera noden i Servern\n");
    if(!(nikomnodeport = CreateMsgPort()))
        cleanup(ERROR,"Kunde inte skapa NiKomNode-porten");
    sprintf(nikomnodeportnamn,"NiKomNode%d",nodnr);
    nikomnodeport->mp_Node.ln_Name = nikomnodeportnamn;
    nikomnodeport->mp_Node.ln_Pri = 1;
    AddPort(nikomnodeport);
    sprintf(rexxportnamn,"NiKomPreRexx%d",nodnr);
    if(!(rexxport=(struct MsgPort *)CreateMsgPort()))
        cleanup(ERROR,"Kunde inte öppna RexxPort\n");
    rexxport->mp_Node.ln_Name=rexxportnamn;
    rexxport->mp_Node.ln_Pri=50;
    AddPort(rexxport);
    if(!(RexxSysBase=(struct RsxLib *)OpenLibrary("rexxsyslib.library",0L)))
        cleanup(ERROR,"Kunde inte öppna rexxsyslib.library\n");
    getnodeconfig(configname);
    if(pubscreen[0]=='-') tmppscreen=NULL;
    else tmppscreen=pubscreen;
    if(!(NiKwind=openmywindow(tmppscreen)))
        cleanup(ERROR,"Kunde inte öppna fönstret\n");
    if(getty) dtespeed = gettybps;
    else dtespeed = highbaud;
    if(!OpenIO(NiKwind)) cleanup(ERROR,"Couldn't setup IO");
    strcpy(Servermem->nodid[nodnr],nodid);
    conreqtkn();
    serreqtkn();
    Delay(50);
    for(;;) {
        inloggad=-1;
        Servermem->idletime[nodnr] = time(NULL);
        Servermem->inloggad[nodnr]=-1;
        if(getty) Servermem->connectbps[nodnr] = connectbps;
        else waitconnect();
        Servermem->idletime[nodnr] = time(NULL);
        Servermem->inloggad[nodnr]=-2; /* Sätt till <Uppringd> för att även hantera -getty-fallet */
reloginspec:
        updateinactive();
        Servermem->inne[nodnr].flaggor = Servermem->cfg.defaultflags;
        if(!getty) Delay(100);
        Servermem->inne[nodnr].rader=0;
        Servermem->inne[nodnr].chrset = CHRS_LATIN1;
        sendfile("NiKom:Texter/Inlogg.txt");
        if(Servermem->cfg.ar.preinlogg) sendrexx(Servermem->cfg.ar.preinlogg);
        car=TRUE;
        Servermem->inne[nodnr].chrset = 0;
        memset(commandhistory,0,1000);
        going=1;
        while(going && going<=Servermem->cfg.logintries) {
            putstring("\r\nNamn: ",-1,0);
            if(getstring(EKO,40,NULL)) {
                car=FALSE;
                break;
            }
            if(!stricmp(inmat,Servermem->cfg.ny)
                    && !(Servermem->cfg.cfgflags & NICFG_CLOSEDBBS)) {
                tmp = RegisterNewUser();
                if(tmp == 2) {
                    goto panik;
                }
                car = tmp ? 0 :  

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++ sendmessage函数代码示例发布时间:2022-05-30
下一篇:
C++ sendf函数代码示例发布时间:2022-05-30
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap