Exemplo n.º 1
0
void download_p2p(int sockfd){

    int count;
    char bufferFST[4];
    count = recv(sockfd, bufferFST, 4, MSG_PEEK);
    if(count == -1)
        perror("Recv with error");

    google::protobuf::uint32 pkg_size = readHdr(bufferFST);
    int byteCount;
    file::File request_file;
    char buffer[pkg_size + HDR_SIZE];
    if( ( byteCount = recv(sockfd, (void *)buffer, pkg_size + HDR_SIZE, MSG_WAITALL) ) == -1 ){
        perror("Error recviving data");
    }
    google::protobuf::io::ArrayInputStream ais(buffer, pkg_size + HDR_SIZE);
    google::protobuf::io::CodedInputStream coded_input(&ais);
    coded_input.ReadVarint32(&pkg_size);
    google::protobuf::io::CodedInputStream::Limit msgLimit = coded_input.PushLimit(pkg_size);
    request_file.ParseFromCodedStream(&coded_input);

    for(auto it=file_sets.begin(); it!=file_sets.end(); ++it){
        if( it-> first == request_file.file_name() && !it->second.empty() ){
            sendCheck(sockfd, true);
            send_download_info(sockfd, it->second);
            return;
        }
    }
    sendCheck(sockfd, false);
    return;
}
Exemplo n.º 2
0
void delete_account(int sockfd){

    int count;
    char bufferFST[4];
    count = recv(sockfd, bufferFST, 4, MSG_PEEK);
    if(count == -1)
        perror("Recv with error");

    login::DeleteInfo info = readDeleteInfo(sockfd, readHdr(bufferFST) );
    std::cout << info.DebugString();

    Data::Data newInfo;
    newInfo.set_id( info.id() );
    newInfo.set_password( info.passwd() );

    for(int i=0; i<loginData.logindata_size(); ++i){
        if( loginData.logindata(i).id() == newInfo.id() &&
            loginData.logindata(i).password() == newInfo.password() ){
            loginData.mutable_logindata(i)->set_id("");
            loginData.mutable_logindata(i)->set_password("");
            loginData.mutable_logindata(i)->set_online(false);
            sendCheck(sockfd, true);
            update();
        }
    }

    sendCheck(sockfd, false);
    return;
}
Exemplo n.º 3
0
void logout(int sockfd, std::map<std::string, std::set<std::string> > &file_sets){
    //recv logout info
    int count;
    char bufferFST[4];
    count = recv(sockfd, bufferFST, 4, MSG_PEEK);
    if(count == -1)
        perror("Recv with error");

    login::Login login = readLogin(sockfd, readHdr(bufferFST) );
    //std::cout << login.DebugString();

    //check id and password
    Data::Data newLogin;
    newLogin.set_id( login.id() );
    newLogin.set_password( login.passwd() );

    for(int i=0; i<loginData.logindata_size(); ++i){
        if( loginData.logindata(i).id() == newLogin.id() &&
            loginData.logindata(i).password() == newLogin.password() ){
            sendCheck(sockfd, true);
            tm* ptr_now;
            time_t loc_now = 0;
            time(&loc_now);
            std::string id = loginData.logindata(i).id();
            loginData.mutable_logindata(i)->set_online(false);
            ptr_now = localtime(&loc_now);
            printf("%s[%d:%d:%d] : [%s] logout %s\n", ANSI_COLOR_RED
            , ptr_now->tm_hour, ptr_now->tm_min, ptr_now->tm_sec, id.c_str(), ANSI_COLOR_RESET);
            logout_update(sockfd, file_sets, login.id());
            return;
        }
    }
    return;
}
Exemplo n.º 4
0
bool fileHandler::readFile(){
	FILE *fp;
	if ((fp = fopen(mPath.c_str(), "rb+")) == NULL)
	{
		fprintf(stderr, "Can't open:<%s>\n", mPath);
		return false;
	}
	
	bool res = false;

	switch (mType){
	case img:
		res = readImg(fp);
		break;
	case hdr:
		readHdr(fp);
		res = true;
		break;
	default:
		std::cerr << "not supported file type!" << std::endl;
		break;
	}

	fclose(fp);
	return res;
}
Exemplo n.º 5
0
void regist_check(int sockfd){
    //recv regist
    int count;
    char bufferFST[4];
    count = recv(sockfd, bufferFST, 4, MSG_PEEK);
    if(count == -1)
        perror("Recv with error");

    google::protobuf::uint32 size = readHdr(bufferFST);
    int byteCount;
    regist::Regist regist;
    char buffer[size + HDR_SIZE];
    if( ( byteCount = recv(sockfd, (void *)buffer, size + HDR_SIZE, MSG_WAITALL) ) == -1 ){
        perror("Error recviving data");
    }
    google::protobuf::io::ArrayInputStream ais(buffer, size + HDR_SIZE);
    google::protobuf::io::CodedInputStream coded_input(&ais);
    coded_input.ReadVarint32(&size);
    google::protobuf::io::CodedInputStream::Limit msgLimit = coded_input.PushLimit(size);
    regist.ParseFromCodedStream(&coded_input);
    coded_input.PopLimit(msgLimit);
    //std::cout << regist.DebugString();

    //check id and password
    Data::Data newRegist;
    newRegist.set_id( regist.id() );
    newRegist.set_password( regist.passwd() );

    for(int i=0; i<loginData.logindata_size(); ++i){
        if( loginData.logindata(i).id() == newRegist.id() ){
            sendCheck(sockfd, false);
            return;
        }
    }

    Data::Data* data = loginData.add_logindata();
    data -> set_id(regist.id());
    data -> set_password(regist.passwd());

    update();
    sendCheck(sockfd, true);
}
Exemplo n.º 6
0
void chat(int sockfd){

    // recv name
    int count;
    char bufferFST[4];
    count = recv(sockfd, bufferFST, 4, MSG_PEEK);
    if(count == -1)
        perror("Recv with error");

    google::protobuf::uint32 pkg_size = readHdr(bufferFST);
    int byteCount;
    online::OnlinePerson person;
    char buffer[pkg_size + HDR_SIZE];
    if( ( byteCount = recv(sockfd, (void *)buffer, pkg_size + HDR_SIZE, MSG_WAITALL) ) == -1 ){
        perror("Error recviving data");
    }
    google::protobuf::io::ArrayInputStream ais(buffer, pkg_size + HDR_SIZE);
    google::protobuf::io::CodedInputStream coded_input(&ais);
    coded_input.ReadVarint32(&pkg_size);
    google::protobuf::io::CodedInputStream::Limit msgLimit = coded_input.PushLimit(pkg_size);
    person.ParseFromCodedStream(&coded_input);

    // check whether online
    for(int i=0; i<loginData.logindata_size(); ++i){
        if( loginData.logindata(i).online() == true ){
            if( loginData.logindata(i).id() != "" &&
                loginData.logindata(i).id() == person.name() ){
                sendCheck(sockfd, true);
                send_port(sockfd, loginData.logindata(i).port(), loginData.logindata(i).ip());
                return;
            }
        }
    }
    sendCheck(sockfd, false);
    printf("!ok\n");

}
Exemplo n.º 7
0
void recv_file_info(int sockfd, std::map<std::string, std::set<std::string> > &file_sets, login::Login user){
    int count;
    char bufferFST[4];
    count = recv(sockfd, bufferFST, 4, MSG_PEEK);
    if(count == -1)
        perror("Recv with error");

    google::protobuf::uint32 pkg_size = readHdr(bufferFST);
    int byteCount;
    file::Files files;
    char buffer[pkg_size + HDR_SIZE];
    if( ( byteCount = recv(sockfd, (void *)buffer, pkg_size + HDR_SIZE, MSG_WAITALL) ) == -1 ){
        perror("Error recviving data");
    }
    google::protobuf::io::ArrayInputStream ais(buffer, pkg_size + HDR_SIZE);
    google::protobuf::io::CodedInputStream coded_input(&ais);
    coded_input.ReadVarint32(&pkg_size);
    google::protobuf::io::CodedInputStream::Limit msgLimit = coded_input.PushLimit(pkg_size);
    files.ParseFromCodedStream(&coded_input);

    printf("%sFile update: \n", ANSI_COLOR_GREEN);
    for(int i=0; i<files.files_size(); ++i){
        file::File file = files.files(i);
        if( file_sets.find( file.file_name() ) == file_sets.end() ){
            file_sets[file.file_name()].insert(user.id());
            std::cout << file.file_name() << ":";
            print_file(file_sets[file.file_name()]);
        }
        else{
            file_sets[file.file_name()].insert(user.id());
            std::cout << file.file_name() << ":";
            print_file(file_sets[file.file_name()]);
        }
    }
    printf("%s\n", ANSI_COLOR_RESET);
    update();
}
Exemplo n.º 8
0
void* client_connect(void* info){
    thread_info* new_info = (thread_info* )info;
    int sockfd = new_info -> sockfd;
    std::string addr = new_info -> address;
    int port = new_info -> port;

    std::cout << sockfd << " " << addr << " " << port << '\n';
    /*int read_size;
    char client_message[2000];
    char welcome_message[200];
    strcpy(welcome_message, "Welcome to simple P2P server !\n");

    write(sockfd, welcome_message, strlen(welcome_message));
	//test block

	login::Login login;
	login.set_id("testID");
	login.set_passwd("testPasswd");
	int pkg_size = login.ByteSize() + 4;
	char* pkt = new char[pkg_size];
	google::protobuf::io::ArrayOutputStream aos(pkt, pkg_size);
	google::protobuf::io::CodedOutputStream* coded_output = new google::protobuf::io::CodedOutputStream(&aos);
	coded_output -> WriteVarint32(login.ByteSize());
	login.SerializeToCodedStream(coded_output);

	write(sockfd, pkt, pkg_size);*/
	//test block
    update();
    login::Login login;
    while( true ){
        int count;
        char buffer[4];
        count = recv(sockfd, buffer, 4, MSG_PEEK);
        if(count == -1)
            perror("Recv with error");
        else{
            ACTION request;
            request = readAction(sockfd, readHdr(buffer));
            if ( request == LOGIN ) {
                login = login_check(sockfd, addr, port);
            } else if ( request == REGIST ) {
                regist_check(sockfd);
            } else if ( request == DELETEACCOUNT ) {
                delete_account(sockfd);
            } else if ( request == SEARCHINFO ) {
                search_info(sockfd);
            } else if ( request == DOWNLOAD ) {
                download_p2p(sockfd);
            } else if ( request == CHAT ) {
                chat(sockfd);
            } else if (request == LOGOUT ) {
                logout(sockfd, file_sets);
                break;
            } else if (request == RECVFILEINFO ) {
                recv_file_info(sockfd, file_sets, login);
            } else if (request == ONLINEINFO){
                send_online_info(sockfd);
            } else if (request == PORTREQUEST) {
                //send_port(sockfd);
            } else {
                printf("%sBugssssssssssss%s\n", ANSI_COLOR_RED, ANSI_COLOR_RESET);
            }
        }
    }


    /*while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 ){
		client_message[read_size] = '\0';
        write(sock , client_message , strlen(client_message));
		memset(client_message, 0, 2000);
    }

    if(read_size == 0) {
        puts("Client disconnected");
        fflush(stdout);
    }else if(read_size == -1){
        perror("recv failed");
    }*/
    //printf("Connection left\n");
    return 0;
}