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_
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2003-02-25 00:20:17 +00:00
|
|
|
#include <time.h>
|
2002-04-30 22:22:54 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
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;
|
|
|
|
char hostname[256];
|
|
|
|
int port;
|
|
|
|
int blocksize;
|
2003-02-26 07:45:21 +00:00
|
|
|
double xfer_speed,recent_bytes; // in bytes per second
|
2003-02-24 23:59:46 +00:00
|
|
|
clock_t last_speed_update;
|
2003-02-27 09:21:22 +00:00
|
|
|
double bytes_xferred;
|
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);
|
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();
|
|
|
|
};
|
|
|
|
|
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-02-26 00:47:57 +00:00
|
|
|
int do_select(double& bytes_transferred, struct timeval& 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
|