Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Sources/UDPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import Foundation
@_silgen_name("yudpsocket_get_server_ip") func c_yudpsocket_get_server_ip(_ host:UnsafePointer<Int8>,ip:UnsafePointer<Int8>) -> Int32
@_silgen_name("yudpsocket_sentto") func c_yudpsocket_sentto(_ fd:Int32,buff:UnsafePointer<Byte>,len:Int32,ip:UnsafePointer<Int8>,port:Int32) -> Int32
@_silgen_name("enable_broadcast") func c_enable_broadcast(_ fd:Int32)
@_silgen_name("set_timeout") func c_set_timeout(_ fd:Int32, _ timeoutSec:Int32, _ timeoutUSec:Int32)


open class UDPClient: Socket {
public override init(address: String, port: Int32) {
Expand Down Expand Up @@ -94,6 +96,15 @@ open class UDPClient: Socket {
c_enable_broadcast(fd)
}

/*
* setTimeout
*/
open func setTimeout(_ timeoutSec:Int32, _ timeoutUSec:Int32) {
guard let fd: Int32 = self.fd else { return }

c_set_timeout(fd, timeoutSec, timeoutUSec)
}

/*
*
* send nsdata
Expand Down
9 changes: 9 additions & 0 deletions Sources/yudpsocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ void enable_broadcast(int socket_fd) {
setsockopt(socket_fd, SOL_SOCKET, SO_BROADCAST, &reuseon, sizeof(reuseon));
}

//set receiving timeout
void set_timeout(int socket_fd, int timeout_sec, int timeout_usec) {
struct timeval timeval;
timeval.tv_sec = timeout_sec;
timeval.tv_usec = timeout_usec;

setsockopt(socket_fd, SOL_SOCKET, SO_RCVTIMEO, &timeval, sizeof(timeval));
}

int yudpsocket_get_server_ip(char *host, char *ip) {
struct hostent *hp;
struct sockaddr_in address;
Expand Down