2003-06-11 23:36:48 +00:00
|
|
|
// The contents of this file are subject to the BOINC Public License
|
2002-04-30 22:22:54 +00:00
|
|
|
// Version 1.0 (the "License"); you may not use this file except in
|
|
|
|
// compliance with the License. You may obtain a copy of the License at
|
2003-06-11 23:36:48 +00:00
|
|
|
// http://boinc.berkeley.edu/license_1.0.txt
|
2002-04-30 22:22:54 +00:00
|
|
|
//
|
|
|
|
// 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 Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Contributor(s):
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef _NET_XFER_
|
|
|
|
#define _NET_XFER_
|
|
|
|
|
2004-03-04 11:41:43 +00:00
|
|
|
#ifndef _WIN32
|
2002-04-30 22:22:54 +00:00
|
|
|
#include <stdio.h>
|
2003-02-25 00:20:17 +00:00
|
|
|
#include <time.h>
|
2002-04-30 22:22:54 +00:00
|
|
|
#include <vector>
|
2004-03-04 11:41:43 +00:00
|
|
|
#endif
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
|
|
|
|
// The following classes provide an interface for polling
|
|
|
|
// (non-blocking) network I/O.
|
|
|
|
|
2003-02-26 00:47:57 +00:00
|
|
|
#define MAX_BLOCKSIZE 16384
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
// represents a network connection, either being accessed directly
|
|
|
|
// or being transferred to/from a file
|
|
|
|
//
|
|
|
|
class NET_XFER {
|
2004-02-05 22:36:55 +00:00
|
|
|
char hostname[256];
|
|
|
|
// The host we're connecting to (possibly a proxy)
|
2002-04-30 22:22:54 +00:00
|
|
|
public:
|
|
|
|
int net_xfer_state;
|
|
|
|
// client-defined; better to define state in parent class
|
|
|
|
int socket;
|
|
|
|
bool is_connected;
|
|
|
|
bool want_download; // at most one should be true
|
|
|
|
bool want_upload;
|
|
|
|
bool do_file_io;
|
2003-02-18 03:31:47 +00:00
|
|
|
// whether poll() should transfer data to/from file
|
|
|
|
// (in which case "file" and blocksize are relevant)
|
|
|
|
// or just set io_ready
|
2002-04-30 22:22:54 +00:00
|
|
|
bool io_done; // got error or EOF
|
|
|
|
FILE* file;
|
|
|
|
bool io_ready;
|
2003-02-18 03:31:47 +00:00
|
|
|
// can read or write socket now (used if !do_file_io)
|
2002-04-30 22:22:54 +00:00
|
|
|
int error;
|
|
|
|
int port;
|
|
|
|
int blocksize;
|
2004-04-05 06:55:09 +00:00
|
|
|
double xfer_speed; // computed every 3 seconds
|
2003-12-12 23:13:01 +00:00
|
|
|
double last_speed_update; // when xfer_speed was last computed
|
2004-04-05 06:55:09 +00:00
|
|
|
double recent_bytes_xferred; // bytes xferred since last_speed_update
|
2004-01-31 23:21:07 +00:00
|
|
|
double bytes_xferred; // bytes transferred in this session
|
2003-04-02 19:30:15 +00:00
|
|
|
char file_read_buf[MAX_BLOCKSIZE];
|
|
|
|
int file_read_buf_offset, file_read_buf_len;
|
2002-04-30 22:22:54 +00:00
|
|
|
|
|
|
|
void init(char* host, int port, int blocksize);
|
2004-03-27 00:45:27 +00:00
|
|
|
int get_ip_addr(int &ip_addr);
|
2002-12-18 23:56:25 +00:00
|
|
|
int get_ip_addr(char *hostname, int &ip_addr);
|
2002-04-30 22:22:54 +00:00
|
|
|
int open_server();
|
2002-08-02 18:31:20 +00:00
|
|
|
void close_socket();
|
2002-04-30 22:22:54 +00:00
|
|
|
int do_xfer(int&);
|
2003-02-24 23:59:46 +00:00
|
|
|
void update_speed(int);
|
2002-04-30 22:22:54 +00:00
|
|
|
void got_error();
|
2004-02-05 22:36:55 +00:00
|
|
|
char* get_hostname();
|
2002-04-30 22:22:54 +00:00
|
|
|
};
|
|
|
|
|
2003-02-26 00:47:57 +00:00
|
|
|
// bandwidth limitation is implemented at this level, as follows:
|
|
|
|
// There are limits max_bytes_sec_up and max_bytes_sec_down.
|
|
|
|
// We keep track of the last time and bytes_left_up and bytes_left_down;
|
|
|
|
// Each second we reset these to zero.
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
class NET_XFER_SET {
|
|
|
|
vector<NET_XFER*> net_xfers;
|
|
|
|
public:
|
2003-02-26 00:47:57 +00:00
|
|
|
NET_XFER_SET();
|
|
|
|
double max_bytes_sec_up, max_bytes_sec_down;
|
|
|
|
// user-specified limits on throughput
|
|
|
|
double bytes_left_up, bytes_left_down;
|
|
|
|
// bytes left to transfer in the current second
|
|
|
|
double bytes_up, bytes_down;
|
|
|
|
// total bytes transferred
|
|
|
|
bool up_active, down_active;
|
|
|
|
// has there been transfer activity since last call to check_active()?
|
|
|
|
time_t last_time;
|
2002-04-30 22:22:54 +00:00
|
|
|
int insert(NET_XFER*);
|
|
|
|
int remove(NET_XFER*);
|
2003-02-26 00:47:57 +00:00
|
|
|
bool poll();
|
2003-02-18 03:31:47 +00:00
|
|
|
int net_sleep(double);
|
2003-09-07 03:11:03 +00:00
|
|
|
int do_select(double& bytes_transferred, double timeout);
|
2002-04-30 22:22:54 +00:00
|
|
|
NET_XFER* lookup_fd(int); // lookup by fd
|
2003-02-26 00:47:57 +00:00
|
|
|
void check_active(bool&, bool&);
|
2002-04-30 22:22:54 +00:00
|
|
|
};
|
|
|
|
|
2003-09-04 20:11:47 +00:00
|
|
|
extern int get_socket_error(int fd);
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
#endif
|