add fn to pass ip:port as values
This commit is contained in:
		
							parent
							
								
									613f21310e
								
							
						
					
					
						commit
						2d3828fb2b
					
				
					 3 changed files with 46 additions and 14 deletions
				
			
		|  | @ -2,7 +2,7 @@ | ||||||
| #include "servicepoint.h" | #include "servicepoint.h" | ||||||
| 
 | 
 | ||||||
| int main(void) { | int main(void) { | ||||||
|     UdpConnection *connection = sp_connection_open("localhost:2342"); |     UdpConnection *connection = sp_connection_open_ipv4(127,0,0,1,2342); | ||||||
|     if (connection == NULL) |     if (connection == NULL) | ||||||
|         return 1; |         return 1; | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -979,13 +979,32 @@ void sp_connection_free(UdpConnection */*notnull*/ connection); | ||||||
|  * # Examples |  * # Examples | ||||||
|  * |  * | ||||||
|  * ```C |  * ```C | ||||||
|  * CConnection connection = sp_connection_open("172.23.42.29:2342"); |  * UdpConnection connection = sp_connection_open("172.23.42.29:2342"); | ||||||
|  * if (connection != NULL) |  * if (connection != NULL) | ||||||
|  *     sp_connection_send_command(connection, sp_command_clear()); |  *     sp_connection_send_command(connection, sp_command_clear()); | ||||||
|  * ``` |  * ``` | ||||||
|  */ |  */ | ||||||
| UdpConnection *sp_connection_open(char */*notnull*/ host); | UdpConnection *sp_connection_open(char */*notnull*/ host); | ||||||
| 
 | 
 | ||||||
|  | /**
 | ||||||
|  |  * Creates a new instance of [UdpConnection]. | ||||||
|  |  * | ||||||
|  |  * returns: NULL if connection fails, or connected instance | ||||||
|  |  * | ||||||
|  |  * # Examples | ||||||
|  |  * | ||||||
|  |  * ```C | ||||||
|  |  * UdpConnection connection = sp_connection_open_ipv4(172, 23, 42, 29, 2342); | ||||||
|  |  * if (connection != NULL) | ||||||
|  |  *     sp_connection_send_command(connection, sp_command_clear()); | ||||||
|  |  * ``` | ||||||
|  |  */ | ||||||
|  | UdpConnection *sp_connection_open_ipv4(uint8_t ip1, | ||||||
|  |                                        uint8_t ip2, | ||||||
|  |                                        uint8_t ip3, | ||||||
|  |                                        uint8_t ip4, | ||||||
|  |                                        uint16_t port); | ||||||
|  | 
 | ||||||
| /**
 | /**
 | ||||||
|  * Sends a [TypedCommand] to the display using the [UdpConnection]. |  * Sends a [TypedCommand] to the display using the [UdpConnection]. | ||||||
|  * |  * | ||||||
|  |  | ||||||
|  | @ -1,5 +1,6 @@ | ||||||
| use servicepoint::{Connection, Packet, TypedCommand, UdpConnection}; | use servicepoint::{Connection, Packet, TypedCommand, UdpConnection}; | ||||||
| use std::ffi::{c_char, CStr}; | use std::ffi::{c_char, CStr}; | ||||||
|  | use std::net::{Ipv4Addr, SocketAddrV4}; | ||||||
| use std::ptr::NonNull; | use std::ptr::NonNull; | ||||||
| 
 | 
 | ||||||
| /// Creates a new instance of [UdpConnection].
 | /// Creates a new instance of [UdpConnection].
 | ||||||
|  | @ -9,7 +10,7 @@ use std::ptr::NonNull; | ||||||
| /// # Examples
 | /// # Examples
 | ||||||
| ///
 | ///
 | ||||||
| /// ```C
 | /// ```C
 | ||||||
| /// CConnection connection = sp_connection_open("172.23.42.29:2342");
 | /// UdpConnection connection = sp_connection_open("172.23.42.29:2342");
 | ||||||
| /// if (connection != NULL)
 | /// if (connection != NULL)
 | ||||||
| ///     sp_connection_send_command(connection, sp_command_clear());
 | ///     sp_connection_send_command(connection, sp_command_clear());
 | ||||||
| /// ```
 | /// ```
 | ||||||
|  | @ -28,17 +29,29 @@ pub unsafe extern "C" fn sp_connection_open( | ||||||
|     Box::into_raw(Box::new(connection)) |     Box::into_raw(Box::new(connection)) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| //#[no_mangle]
 | /// Creates a new instance of [UdpConnection].
 | ||||||
| //pub unsafe extern "C" fn sp_connection_open_ipv4(
 | ///
 | ||||||
| //    host: SocketAddrV4,
 | /// returns: NULL if connection fails, or connected instance
 | ||||||
| //) -> *mut UdpConnection {
 | ///
 | ||||||
| //    let connection = match servicepoint::UdpConnection::open(host) {
 | /// # Examples
 | ||||||
| //        Err(_) => return std::ptr::null_mut(),
 | ///
 | ||||||
| //        Ok(value) => value,
 | /// ```C
 | ||||||
| //    };
 | /// UdpConnection connection = sp_connection_open_ipv4(172, 23, 42, 29, 2342);
 | ||||||
| //
 | /// if (connection != NULL)
 | ||||||
| //    Box::into_raw(Box::new(UdpConnection(connection)))
 | ///     sp_connection_send_command(connection, sp_command_clear());
 | ||||||
| //}
 | /// ```
 | ||||||
|  | #[no_mangle] | ||||||
|  | pub unsafe extern "C" fn sp_connection_open_ipv4( | ||||||
|  |     ip1: u8, ip2: u8, ip3: u8, ip4: u8, | ||||||
|  |     port: u16, | ||||||
|  | ) -> *mut UdpConnection { | ||||||
|  |     let addr = SocketAddrV4::new(Ipv4Addr::from( [ip1, ip2, ip3, ip4]), port); | ||||||
|  |     let connection = match UdpConnection::open(addr) { | ||||||
|  |         Err(_) => return std::ptr::null_mut(), | ||||||
|  |         Ok(value) => value, | ||||||
|  |     }; | ||||||
|  |     Box::into_raw(Box::new(connection)) | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| // /// Creates a new instance of [SPUdpConnection] for testing that does not actually send anything.
 | // /// Creates a new instance of [SPUdpConnection] for testing that does not actually send anything.
 | ||||||
| // ///
 | // ///
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Vinzenz Schroeter
						Vinzenz Schroeter