2005-01-20 23:22:22 +00:00
|
|
|
// Berkeley Open Infrastructure for Network Computing
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2005 University of California
|
2002-04-30 22:22:54 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// This is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation;
|
|
|
|
// either version 2.1 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This software is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
// See the GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// To view the GNU Lesser General Public License visit
|
|
|
|
// http://www.gnu.org/copyleft/lesser.html
|
|
|
|
// or write to the Free Software Foundation, Inc.,
|
|
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2002-04-30 22:22:54 +00:00
|
|
|
|
|
|
|
#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
|
|
|
|
2004-06-12 04:45:36 +00:00
|
|
|
// The following classes implement polling (non-blocking) I/O
|
|
|
|
// between a disk file (or memory block) and a socket
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2003-02-26 00:47:57 +00:00
|
|
|
#define MAX_BLOCKSIZE 16384
|
|
|
|
|
2004-06-16 23:16:08 +00:00
|
|
|
|
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 {
|
|
|
|
public:
|
2005-02-16 23:17:43 +00:00
|
|
|
char hostname[256]; // The host we're connecting to (possibly a proxy)
|
2002-04-30 22:22:54 +00:00
|
|
|
int socket;
|
|
|
|
bool is_connected;
|
|
|
|
bool want_download; // at most one should be true
|
|
|
|
bool want_upload;
|
|
|
|
bool do_file_io;
|
2004-12-10 00:57:20 +00:00
|
|
|
// If true: poll() should transfer data to/from file
|
2003-02-18 03:31:47 +00:00
|
|
|
// (in which case "file" and blocksize are relevant)
|
2004-12-10 00:57:20 +00:00
|
|
|
// If false: set io_ready (higher layers will do I/O)
|
|
|
|
bool io_done;
|
|
|
|
// set to true when the current transfer is over:
|
|
|
|
// - the transfer timed out (not activity for a long time)
|
|
|
|
// - network connect failed
|
|
|
|
// - got EOF on socket read (0 bytes, select indicated I/O ready)
|
|
|
|
// - error on disk write (e.g. volume full)
|
|
|
|
// - reached end of disk file on upload
|
|
|
|
// - got file read error on upload
|
|
|
|
// - write to socket failed on upload
|
2002-04-30 22:22:54 +00:00
|
|
|
FILE* file;
|
|
|
|
bool io_ready;
|
2004-12-10 00:57:20 +00:00
|
|
|
// Signals higher layers that they 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-20 02:47:51 +00:00
|
|
|
double start_time;
|
|
|
|
double xfer_speed;
|
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;
|
2004-06-16 23:16:08 +00:00
|
|
|
int seconds_until_timeout;
|
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-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&);
|
2004-09-22 21:08:26 +00:00
|
|
|
void update_speed();
|
2002-04-30 22:22:54 +00:00
|
|
|
void got_error();
|
2004-02-05 22:36:55 +00:00
|
|
|
char* get_hostname();
|
2004-06-16 23:16:08 +00:00
|
|
|
bool check_timeout(bool);
|
|
|
|
void reset_timeout();
|
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 {
|
2005-03-07 06:09:04 +00:00
|
|
|
std::vector<NET_XFER*> net_xfers;
|
2002-04-30 22:22:54 +00:00
|
|
|
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()?
|
2004-11-15 21:20:58 +00:00
|
|
|
int last_time;
|
2002-04-30 22:22:54 +00:00
|
|
|
int insert(NET_XFER*);
|
|
|
|
int remove(NET_XFER*);
|
2004-10-14 22:01:05 +00:00
|
|
|
bool poll(double);
|
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
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|