示例#1
0
int llopen(unsigned int port, unsigned int flag) {
	if (init_link(port))
		return -1;

	linklayer.flag = flag;
	int fd = open_port_file(port);

	if (flag == RECEIVER) {
		if (rec_set(fd))
			return -1;
		if (send_ua(fd, flag))
			return -1;
	} else if (flag == SENDER) {
		if (send_set(fd))
			return -1;
		if (rec_ua(fd, flag))
			return -1;
	} else {
		printf("ENTER A VALID STATUS!\n");
		close_port_file(fd);
		return -1;
	}

	initialized = -1;
	return fd;
}
示例#2
0
		// perform sql statement
		db_query_basic::list_rec_set_t db_query_basic::perform(std::string query)
		{
			if (query.empty())
				return list_rec_set_t();
			// replace "%table_name%" to table_name
			//query = std::regex_replace(query, std::regex("\\%table_name%"), _table);
			int ret = mysql_real_query(_db.conn(), query.c_str(), static_cast<unsigned long>(query.length()));
			assert_ret(ret);

			list_rec_set_t list_rec_set;
			int status = 0;
			do
			{
				size_t num_col = mysql_field_count(_db.conn());
				MYSQL_RES* res = mysql_store_result(_db.conn());
				size_t affected = static_cast<size_t>(mysql_affected_rows(_db.conn()));
				if (res == nullptr)
				{
					// raise exception if result is supposed to be returned
					if (num_col > 0)
						throw ex_db_mariadb(_db.conn());
					db_record_meta::list_column_t list_col_empty;
					db_record_meta meta_empty(list_col_empty);
					db_record_set rec_set_empty(meta_empty);
					rec_set_empty._affected = affected;
					list_rec_set.push_back(rec_set_empty);
				}
				else // when MYSQL_RES* available
				{
					MYSQL_FIELD* p_col = mysql_fetch_fields(res);
					db_record_set rec_set(db_record_meta(p_col, num_col));
					for (size_t idx_row = 0; idx_row < res->row_count; idx_row++)
					{
						assert_res(res);
						const MYSQL_ROW row = mysql_fetch_row(res);
						assert_row(row);
						record_t rec_d = rec_set.record_new();
						for (size_t idx_col = 0; idx_col < num_col; idx_col++)
							rec_d[idx_col] = (row[idx_col] == nullptr ? "" : row[idx_col]);
						rec_set.list_record().push_back(rec_d);
					}
					rec_set._affected = res->row_count;
					list_rec_set.push_back(rec_set);
					mysql_free_result(res);
				}
				// check whether there are more result sets:
				//   status > 0   error occurred;
				//   status = 0   more result sets returned;
				//   status < 0   no next result set;
				if ((status = mysql_next_result(_db.conn())) > 0)
					throw ex_db_mariadb(_db.conn());
			} while (status == 0);

			return list_rec_set;
		}