Coder Social home page Coder Social logo

nftp-codec's Introduction

nftp-codec

Workflows

It's a lightweight ftp parser, and provide a convenient maker & handler mechanism to make or handle the package comply to nftp.

Features

  • Support P to P / N to 1 / 1 to N

  • Lightweight and no runtime

  • Embeddable

  • Support asynchronous send / recv

  • NFTP Version 1.1 standard docs are Here

Quick start

Sender

#include "nftp.h"

int sender() {
	int rv;
	char * fname = "demo.txt";
	uint8_t * s; // something to send
	uint8_t * r; // something to recv
	size_t slen; // length of s
	size_t rlen; // length of r

	nftp_proto_init();
	nftp_proto_send_start(fname);
	nftp_proto_maker(fname, NFTP_TYPE_HELLO, 0, 0, &s, &slen);
	test_send(s, slen); // your send function. for example tcp_send()
	free(s);

	// waiting for ack
	test_recv(&r, &rlen); // your recv function. for example tcp_recv();
	// handle ack
	rv = nftp_proto_handler(r, rlen, &s, &slen);
	free(r);
	if (rv != 0) {
		goto done; // The ack is invalid or other errors happened
	}

	nftp_file_blocks(fname, &blocks);

	// Note. index start from 0, and end with blocks-1
	for (int i=0; i<blocks; ++i) {
		nftp_proto_maker(fname, NFTP_TYPE_FILE, 0, i, &s, &slen);
		test_send(s, slen);
		free(s);
	}

done:
	nftp_proto_fini();
}

Recver

#include "nftp.h"

int recver() {
	int rv, blocks;
	char * fname = "demo.txt";
	uint8_t * s; // something to send
	uint8_t * r; // something to recv
	size_t slen; // length of s
	size_t rlen; // length of r

	nftp_proto_init();

	while (1) {
		test_recv(&r, &rlen); // your recv function. for example tcp_recv();

		rv = nftp_proto_handler(r, rlen, &s, &slen);
		free(r);
		if (rv != 0) {
			goto done; // Errors happened when recving
		}

		if (s) {
			// recved an hello and send an ack as response(if u want)
			test_send(s, slen);
			free(s);
		} else {
			printf("File transferring...\n");
		}
	}

done:
	nftp_proto_fini();
}

How it works

Process

(sync)
sender  --HELLO-->  recver
sender  <---ACK---  recver

(async allowed)
sender  ---FILE-->  recver
sender  ---FILE-->  recver
sender  ---FILE-->  recver
sender  ---END--->  recver

How do discontinued transmission

A good question! TODO :)

Something u should know

Property iter vector iovs codec file hash proto
Thread-safe X O O O X O X

TODO List

  • More easy to use
  • Support discontinued transmission
  • And so on...

nftp-codec's People

Contributors

wanghaemq avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.