Добавил:
korayakov
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:
/*
netFileCopyer
Copyright (C) 2004 Dmitry S. Vasilchenko.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
The GNU GPL is contained in /usr/doc/copyright/GPL on a Debian
system and in the file COPYING in the Linux kernel source.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <resolv.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>
#include "os_spec.h"
ssize_t get_fsize(const char *fname)
{
struct stat mstat;
if (stat(fname,&mstat) < 0)
return -1;
return mstat.st_size;
}
/* Create socket and init some struct */
oi_connect::oi_connect()
{
err_state = 0;
stop = 0; /* stop flag */
}
oi_connect::~oi_connect()
{
stop = 1;
if (sd) delete sd;
}
int oi_connect::d_connect(const char *p_ip, unsigned int p_port)
{
sd = new QSocketDevice ( QSocketDevice::Stream );
QHostAddress addr = QHostAddress();
addr.setAddress(p_ip);
if ( !sd->connect(addr, p_port) ){
hold_error("d_connect:: connect");
return -1;
}
sd->setBlocking(false);
return 0;
}
int oi_connect::d_accept(unsigned int p_port)
{
sd = new QSocketDevice ( QSocketDevice::Stream );
QHostAddress addr = QHostAddress();
addr.setAddress("127.0.0.1");
if ( !sd->bind(addr, p_port) ){
hold_error("d_accept:: bind");
return -1;
}
sd->setBlocking(false);
if ( !sd->listen(5) ){
hold_error("d_accept:: listen");
return -1;
}
int tsd;
while ( ((tsd = sd->accept())<0) && !stop ) usleep(100);
if (stop) return -1;
delete sd;
sd = new QSocketDevice ( tsd, QSocketDevice::Stream );
sd->setBlocking(false);
return 0;
}
void oi_connect::d_disconnect()
{
}
int oi_connect::d_send(const void *buf, size_t len)
{
if ( sd->writeBlock((char *)buf, len) < 0 ){
hold_error("d_send:: send data");
return -1;
}
return len;
}
int oi_connect::d_recv(void *buf, size_t len)
{
ssize_t r_len = 0;
size_t r_count = 0;
while (r_count < len && !stop){
r_len = sd->readBlock((char *)buf+r_count, len-r_count);
if (r_len > 0){
r_count += r_len;
}
else
usleep(100);
}
return r_count;
}
void oi_connect::d_stop()
{
stop ++;
}
QString oi_connect::get_last_error()
{
return err_str;
}
int oi_connect::estate()
{
return err_state;
}
void oi_connect::hold_error(const char *str)
{
printf("Error:: %s\n", str);
perror("");
err_str.append(str);
err_str.append(":: ");
err_state = 1;
}
Соседние файлы в папке net_copy