code comments
This commit is contained in:
parent
59b458a2f7
commit
ed7b11726b
20
connection.c
20
connection.c
@ -33,8 +33,12 @@
|
||||
static LIST_HEAD(torrent_list);
|
||||
|
||||
struct torrent_file {
|
||||
/* list of torrent files */
|
||||
struct list_head list;
|
||||
|
||||
/* list of clients in this cloud */
|
||||
struct list_head client_list;
|
||||
|
||||
char *name;
|
||||
};
|
||||
|
||||
@ -45,16 +49,20 @@ struct client_con {
|
||||
struct event_fd *event;
|
||||
struct linebuffer *lbuf;
|
||||
|
||||
/* current bandwidth up/down */
|
||||
int bw_up;
|
||||
int bw_dn;
|
||||
|
||||
/* total bytes up/down */
|
||||
unsigned long long total_up;
|
||||
unsigned long long total_dn;
|
||||
|
||||
/* clients cloud view */
|
||||
int chunk_total;
|
||||
int chunk_avail;
|
||||
int chunk_have;
|
||||
|
||||
/* timestamp when this client completed */
|
||||
long completed;
|
||||
|
||||
struct torrent_file *torrent;
|
||||
@ -62,21 +70,25 @@ struct client_con {
|
||||
|
||||
static struct torrent_file * find_create_torrent(const char *filename)
|
||||
{
|
||||
/* search for this torrent */
|
||||
struct torrent_file *torrent;
|
||||
list_for_each_entry(torrent, &torrent_list, list) {
|
||||
if (strcmp(torrent->name, filename) == 0)
|
||||
return torrent;
|
||||
}
|
||||
|
||||
/* create a new one */
|
||||
torrent = malloc(sizeof(struct torrent_file) + strlen(filename));
|
||||
if (torrent == NULL) {
|
||||
log_print(LOG_WARN, "find_create_torrent(): out of memory");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* init fields */
|
||||
INIT_LIST_HEAD(&torrent->client_list);
|
||||
torrent->name = strdup(filename);
|
||||
|
||||
/* keep torrent list sorted by name */
|
||||
struct torrent_file *search;
|
||||
list_for_each_entry(search, &torrent_list, list)
|
||||
if (strcmp(search->name, torrent->name) > 0)
|
||||
@ -107,6 +119,7 @@ static int data_cb(int fd, void *privdata)
|
||||
{
|
||||
struct client_con *con = (struct client_con *)privdata;
|
||||
|
||||
/* get data from socket */
|
||||
if (linebuffer_readfd(con->lbuf, fd) < 0) {
|
||||
free_client(con);
|
||||
return -1;
|
||||
@ -114,6 +127,7 @@ static int data_cb(int fd, void *privdata)
|
||||
|
||||
char *line;
|
||||
while ((line = linebuffer_getline(con->lbuf, NULL)) != NULL) {
|
||||
/* bandwidth update */
|
||||
if (strncmp(line, "CTBW ", 5) == 0) {
|
||||
int bwup, bwdn, liup, lidn;
|
||||
if (sscanf(line +5, "%d,%d %d,%d", &bwdn, &bwup, &lidn, &liup) == 4) {
|
||||
@ -121,6 +135,7 @@ static int data_cb(int fd, void *privdata)
|
||||
con->bw_dn = bwdn;
|
||||
}
|
||||
|
||||
/* status update */
|
||||
} else if (strncmp(line, "CTSTATUS ", 9) == 0) {
|
||||
int seeds1 = 0, seeds2 = 0, leech1 = 0, leech2 = 0, count = 0;
|
||||
int chunk1 = 0, chunk2 = 0, chunk3 = 0, bwdn = 0, bwup = 0;
|
||||
@ -140,6 +155,7 @@ static int data_cb(int fd, void *privdata)
|
||||
con->chunk_avail = chunk3;
|
||||
}
|
||||
|
||||
/* update torrent-file */
|
||||
} else if (strncmp(line, "CTORRENT ", 9) == 0) {
|
||||
char *filename = strrchr(line +9, ' ');
|
||||
if (filename != NULL) {
|
||||
@ -154,12 +170,13 @@ static int data_cb(int fd, void *privdata)
|
||||
linebuffer_freeline(con->lbuf);
|
||||
}
|
||||
|
||||
/* move completed clients to top of the list, ordered by their timestamp */
|
||||
/* client completed? */
|
||||
if (con->chunk_have == con->chunk_total && con->chunk_total != 0 && con->completed == 0) {
|
||||
con->completed = time(NULL);
|
||||
|
||||
list_del(&con->list);
|
||||
|
||||
/* sorted insert, completed on top of list, ordered by complete-timestamp */
|
||||
struct client_con *search;
|
||||
list_for_each_entry(search, &con->torrent->client_list, list) {
|
||||
if (search->completed == 0)
|
||||
@ -232,6 +249,7 @@ int ctcs_accept_handler(int fd, void *privdata)
|
||||
|
||||
con->event = event_add_readfd(NULL, sockfd, data_cb, con);
|
||||
|
||||
/* assign default torrent */
|
||||
con->torrent = find_create_torrent("[unknown]");
|
||||
list_add_tail(&con->list, &con->torrent->client_list);
|
||||
return 0;
|
||||
|
Loading…
Reference in New Issue
Block a user