prot simp, iterate starte, more send code
This commit is contained in:
		| @@ -134,8 +134,8 @@ struct NGC_EXT_CTX { | |||||||
| 		_EXT_CASE(FT1_INIT_ACK) | 		_EXT_CASE(FT1_INIT_ACK) | ||||||
| 		_EXT_CASE(FT1_DATA) | 		_EXT_CASE(FT1_DATA) | ||||||
| 		_EXT_CASE(FT1_DATA_ACK) | 		_EXT_CASE(FT1_DATA_ACK) | ||||||
| 		_EXT_CASE(FT1_DATA_FIN) | 		//_EXT_CASE(FT1_DATA_FIN) | ||||||
| 		_EXT_CASE(FT1_DATA_FIN_ACK) | 		//_EXT_CASE(FT1_DATA_FIN_ACK) | ||||||
|  |  | ||||||
| 		default: return "<unk>"; | 		default: return "<unk>"; | ||||||
| 	} | 	} | ||||||
|   | |||||||
							
								
								
									
										120
									
								
								ngc_ft1.cpp
									
									
									
									
									
								
							
							
						
						
									
										120
									
								
								ngc_ft1.cpp
									
									
									
									
									
								
							| @@ -37,6 +37,7 @@ struct NGC_FT1 { | |||||||
| 			size_t next_recv_transfer_idx {0}; // next id will be 0 | 			size_t next_recv_transfer_idx {0}; // next id will be 0 | ||||||
|  |  | ||||||
| 			struct SendTransfer { | 			struct SendTransfer { | ||||||
|  | 				NGC_FT1_file_kind file_kind; | ||||||
| 				std::vector<uint8_t> file_id; | 				std::vector<uint8_t> file_id; | ||||||
|  |  | ||||||
| 				enum class State { | 				enum class State { | ||||||
| @@ -52,7 +53,9 @@ struct NGC_FT1 { | |||||||
| 					// delete | 					// delete | ||||||
| 				} state; | 				} state; | ||||||
|  |  | ||||||
| 				// float time_since_last_activity ? | 				size_t inits_sent {1}; // is sent when creating | ||||||
|  |  | ||||||
|  | 				float time_since_activity {0.f}; | ||||||
| 				size_t file_size {0}; | 				size_t file_size {0}; | ||||||
| 				size_t file_size_current {0}; | 				size_t file_size_current {0}; | ||||||
|  |  | ||||||
| @@ -73,8 +76,8 @@ static bool _send_pkg_FT1_INIT(const Tox* tox, uint32_t group_number, uint32_t p | |||||||
| static bool _send_pkg_FT1_INIT_ACK(const Tox* tox, uint32_t group_number, uint32_t peer_number, uint8_t transfer_id); | static bool _send_pkg_FT1_INIT_ACK(const Tox* tox, uint32_t group_number, uint32_t peer_number, uint8_t transfer_id); | ||||||
| static bool _send_pkg_FT1_DATA(const Tox* tox, uint32_t group_number, uint32_t peer_number, uint8_t transfer_id, uint16_t sequence_id, const uint8_t* data, size_t data_size); | static bool _send_pkg_FT1_DATA(const Tox* tox, uint32_t group_number, uint32_t peer_number, uint8_t transfer_id, uint16_t sequence_id, const uint8_t* data, size_t data_size); | ||||||
| static bool _send_pkg_FT1_DATA_ACK(const Tox* tox, uint32_t group_number, uint32_t peer_number, uint8_t transfer_id, const uint16_t* seq_ids, size_t seq_ids_size); | static bool _send_pkg_FT1_DATA_ACK(const Tox* tox, uint32_t group_number, uint32_t peer_number, uint8_t transfer_id, const uint16_t* seq_ids, size_t seq_ids_size); | ||||||
| static bool _send_pkg_FT1_DATA_FIN(const Tox* tox, uint32_t group_number, uint32_t peer_number, uint8_t transfer_id); | //static bool _send_pkg_FT1_DATA_FIN(const Tox* tox, uint32_t group_number, uint32_t peer_number, uint8_t transfer_id); | ||||||
| static bool _send_pkg_FT1_DATA_FIN_ACK(const Tox* tox, uint32_t group_number, uint32_t peer_number, uint8_t transfer_id); | //static bool _send_pkg_FT1_DATA_FIN_ACK(const Tox* tox, uint32_t group_number, uint32_t peer_number, uint8_t transfer_id); | ||||||
|  |  | ||||||
| // handle pkgs | // handle pkgs | ||||||
| static void _handle_FT1_REQUEST(Tox* tox, NGC_EXT_CTX* ngc_ext_ctx, uint32_t group_number, uint32_t peer_number, const uint8_t *data, size_t length); | static void _handle_FT1_REQUEST(Tox* tox, NGC_EXT_CTX* ngc_ext_ctx, uint32_t group_number, uint32_t peer_number, const uint8_t *data, size_t length); | ||||||
| @@ -94,8 +97,6 @@ bool NGC_FT1_init(NGC_EXT_CTX* ngc_ext_ctx, const struct NGC_FT1_options* option | |||||||
| 	ngc_ext_ctx->callbacks[FT1_INIT_ACK] = nullptr; | 	ngc_ext_ctx->callbacks[FT1_INIT_ACK] = nullptr; | ||||||
| 	ngc_ext_ctx->callbacks[FT1_DATA] = nullptr; | 	ngc_ext_ctx->callbacks[FT1_DATA] = nullptr; | ||||||
| 	ngc_ext_ctx->callbacks[FT1_DATA_ACK] = nullptr; | 	ngc_ext_ctx->callbacks[FT1_DATA_ACK] = nullptr; | ||||||
| 	ngc_ext_ctx->callbacks[FT1_DATA_FIN] = nullptr; |  | ||||||
| 	ngc_ext_ctx->callbacks[FT1_DATA_FIN_ACK] = nullptr; |  | ||||||
|  |  | ||||||
| 	return true; | 	return true; | ||||||
| } | } | ||||||
| @@ -105,6 +106,81 @@ void NGC_FT1_kill(NGC_EXT_CTX* ngc_ext_ctx) { | |||||||
| 	ngc_ext_ctx->ngc_ft1_ctx = nullptr; | 	ngc_ext_ctx->ngc_ft1_ctx = nullptr; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | void NGC_FT1_iterate(Tox *tox, NGC_EXT_CTX* ngc_ext_ctx/*, void *user_data*/) { | ||||||
|  | 	assert(ngc_ext_ctx); | ||||||
|  | 	assert(ngc_ext_ctx->ngc_ft1_ctx); | ||||||
|  |  | ||||||
|  | 	for (auto& [group_number, group] : ngc_ext_ctx->ngc_ft1_ctx->groups) { | ||||||
|  | 		for (auto& [peer_number, peer] : group.peers) { | ||||||
|  | 			//for (auto& tf_opt : peer.send_transfers) { | ||||||
|  | 			for (size_t idx = 0; idx < peer.send_transfers.size(); idx++) { | ||||||
|  | 				auto& tf_opt = peer.send_transfers[idx]; | ||||||
|  | 				if (tf_opt) { | ||||||
|  | 					auto& tf = tf_opt.value(); | ||||||
|  |  | ||||||
|  | 					tf.time_since_activity += 0.025f; // TODO: actual delta | ||||||
|  |  | ||||||
|  | 					switch (tf.state) { | ||||||
|  | 						using State = NGC_FT1::Group::Peer::SendTransfer::State; | ||||||
|  | 						case State::INIT_SENT: | ||||||
|  | 							if (tf.time_since_activity >= 20.f) { | ||||||
|  | 								if (tf.inits_sent >= 3) { | ||||||
|  | 									// delete, timed out 3 times | ||||||
|  | 									fprintf(stderr, "warning, ft init timed out, deleting\n"); | ||||||
|  | 									tf_opt.reset(); | ||||||
|  | 									continue; | ||||||
|  | 								} else { | ||||||
|  | 									// timed out, resend | ||||||
|  | 									fprintf(stderr, "warning, ft init timed out, resending\n"); | ||||||
|  | 									_send_pkg_FT1_INIT(tox, group_number, peer_number, tf.file_kind, tf.file_size, idx, tf.file_id.data(), tf.file_id.size()); | ||||||
|  | 									tf.inits_sent++; | ||||||
|  | 									tf.time_since_activity = 0.f; | ||||||
|  | 								} | ||||||
|  | 							} | ||||||
|  | 							break; | ||||||
|  | 						case State::SENDING: | ||||||
|  | 							break; | ||||||
|  | 						case State::FINISHING: | ||||||
|  | 							break; | ||||||
|  |  | ||||||
|  | 						// finfin o.o | ||||||
|  |  | ||||||
|  | 						default: // invalid state, delete | ||||||
|  | 							fprintf(stderr, "error, ft in invalid state, deleting\n"); | ||||||
|  | 							tf_opt.reset(); | ||||||
|  | 							continue; | ||||||
|  | 					} | ||||||
|  | 				} | ||||||
|  |  | ||||||
|  | 			//struct SendTransfer { | ||||||
|  | 				//std::vector<uint8_t> file_id; | ||||||
|  |  | ||||||
|  | 				//enum class State { | ||||||
|  | 					//INIT_SENT, // keep this state until ack or deny or giveup | ||||||
|  |  | ||||||
|  | 					//SENDING, // we got the ack and are now sending data | ||||||
|  |  | ||||||
|  | 					//// is this real? | ||||||
|  | 					//FINISHING, // we sent all data but acks still outstanding???? | ||||||
|  |  | ||||||
|  | 					//FINFIN, // we sent the data_fin and are waiting for the data_fin_ack | ||||||
|  |  | ||||||
|  | 					//// delete | ||||||
|  | 				//} state; | ||||||
|  |  | ||||||
|  | 				//// float time_since_last_activity ? | ||||||
|  | 				//size_t file_size {0}; | ||||||
|  | 				//size_t file_size_current {0}; | ||||||
|  |  | ||||||
|  | 				//// sequence array | ||||||
|  | 				//// list of sent but not acked seq_ids | ||||||
|  | 			//}; | ||||||
|  | 			//std::array<std::optional<SendTransfer>, 256> send_transfers; | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
| void NGC_FT1_register_callback_recv_request(NGC_EXT_CTX* ngc_ext_ctx, NGC_FT1_file_kind file_kind, NGC_FT1_recv_request_cb* callback) { | void NGC_FT1_register_callback_recv_request(NGC_EXT_CTX* ngc_ext_ctx, NGC_FT1_file_kind file_kind, NGC_FT1_recv_request_cb* callback) { | ||||||
| 	assert(ngc_ext_ctx); | 	assert(ngc_ext_ctx); | ||||||
| 	assert(ngc_ext_ctx->ngc_ft1_ctx); | 	assert(ngc_ext_ctx->ngc_ft1_ctx); | ||||||
| @@ -133,9 +209,6 @@ void NGC_FT1_register_callback_send_data(NGC_EXT_CTX* ngc_ext_ctx, NGC_FT1_file_ | |||||||
| 	ngc_ext_ctx->ngc_ft1_ctx->cb_send_data[file_kind] = callback; | 	ngc_ext_ctx->ngc_ft1_ctx->cb_send_data[file_kind] = callback; | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| // iterate |  | ||||||
|  |  | ||||||
| void NGC_FT1_send_request( | void NGC_FT1_send_request( | ||||||
| 	Tox *tox, NGC_EXT_CTX* ngc_ext_ctx, | 	Tox *tox, NGC_EXT_CTX* ngc_ext_ctx, | ||||||
|  |  | ||||||
| @@ -217,8 +290,11 @@ bool NGC_FT1_send_init_private( | |||||||
| 	_send_pkg_FT1_INIT(tox, group_number, peer_number, file_kind, file_size, idx, file_id, file_id_size); | 	_send_pkg_FT1_INIT(tox, group_number, peer_number, file_kind, file_size, idx, file_id, file_id_size); | ||||||
|  |  | ||||||
| 	peer.send_transfers[idx] = NGC_FT1::Group::Peer::SendTransfer{ | 	peer.send_transfers[idx] = NGC_FT1::Group::Peer::SendTransfer{ | ||||||
|  | 		file_kind, | ||||||
| 		std::vector(file_id, file_id+file_id_size), | 		std::vector(file_id, file_id+file_id_size), | ||||||
| 		NGC_FT1::Group::Peer::SendTransfer::State::INIT_SENT, | 		NGC_FT1::Group::Peer::SendTransfer::State::INIT_SENT, | ||||||
|  | 		1, | ||||||
|  | 		0.f, | ||||||
| 		file_size, | 		file_size, | ||||||
| 		0, | 		0, | ||||||
| 	}; | 	}; | ||||||
| @@ -310,23 +386,23 @@ static bool _send_pkg_FT1_DATA_ACK(const Tox* tox, uint32_t group_number, uint32 | |||||||
| 	return tox_group_send_custom_private_packet(tox, group_number, peer_number, true, pkg.data(), pkg.size(), nullptr); | 	return tox_group_send_custom_private_packet(tox, group_number, peer_number, true, pkg.data(), pkg.size(), nullptr); | ||||||
| } | } | ||||||
|  |  | ||||||
| static bool _send_pkg_FT1_DATA_FIN(const Tox* tox, uint32_t group_number, uint32_t peer_number, uint8_t transfer_id) { | //static bool _send_pkg_FT1_DATA_FIN(const Tox* tox, uint32_t group_number, uint32_t peer_number, uint8_t transfer_id) { | ||||||
| 	std::vector<uint8_t> pkg; | 	//std::vector<uint8_t> pkg; | ||||||
| 	pkg.push_back(FT1_DATA_FIN); | 	//pkg.push_back(FT1_DATA_FIN); | ||||||
| 	pkg.push_back(transfer_id); | 	//pkg.push_back(transfer_id); | ||||||
|  |  | ||||||
| 	// lossless? | 	//// lossless? | ||||||
| 	return tox_group_send_custom_private_packet(tox, group_number, peer_number, true, pkg.data(), pkg.size(), nullptr); | 	//return tox_group_send_custom_private_packet(tox, group_number, peer_number, true, pkg.data(), pkg.size(), nullptr); | ||||||
| } | //} | ||||||
|  |  | ||||||
| static bool _send_pkg_FT1_DATA_FIN_ACK(const Tox* tox, uint32_t group_number, uint32_t peer_number, uint8_t transfer_id) { | //static bool _send_pkg_FT1_DATA_FIN_ACK(const Tox* tox, uint32_t group_number, uint32_t peer_number, uint8_t transfer_id) { | ||||||
| 	std::vector<uint8_t> pkg; | 	//std::vector<uint8_t> pkg; | ||||||
| 	pkg.push_back(FT1_DATA_FIN_ACK); | 	//pkg.push_back(FT1_DATA_FIN_ACK); | ||||||
| 	pkg.push_back(transfer_id); | 	//pkg.push_back(transfer_id); | ||||||
|  |  | ||||||
| 	// lossless? | 	//// lossless? | ||||||
| 	return tox_group_send_custom_private_packet(tox, group_number, peer_number, true, pkg.data(), pkg.size(), nullptr); | 	//return tox_group_send_custom_private_packet(tox, group_number, peer_number, true, pkg.data(), pkg.size(), nullptr); | ||||||
| } | //} | ||||||
|  |  | ||||||
| #define _DATA_HAVE(x, error) if ((length - curser) < (x)) { error; } | #define _DATA_HAVE(x, error) if ((length - curser) < (x)) { error; } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -476,7 +476,7 @@ bool _handle_HS1_ft_recv_init( | |||||||
| 	// TODO: more? | 	// TODO: more? | ||||||
| 	pending.at(msg_id).time_since_ft_activity = 0.f; | 	pending.at(msg_id).time_since_ft_activity = 0.f; | ||||||
| 	//pending.at(msg_id).transfer_acked; | 	//pending.at(msg_id).transfer_acked; | ||||||
| 	group.transfers | 	//group.transfers | ||||||
|  |  | ||||||
| 	return true; // accept | 	return true; // accept | ||||||
| } | } | ||||||
|   | |||||||
| @@ -9,6 +9,7 @@ | |||||||
| #include <map> | #include <map> | ||||||
| #include <list> | #include <list> | ||||||
| #include <set> | #include <set> | ||||||
|  | #include <vector> | ||||||
| #include <optional> | #include <optional> | ||||||
|  |  | ||||||
| struct NGC_HS1 { | struct NGC_HS1 { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user