@@ -51,6 +51,7 @@ type SrtSocket struct {
51
51
options map [string ]string
52
52
mode int
53
53
pktSize int
54
+ pollTimeout int64
54
55
}
55
56
56
57
var (
@@ -89,6 +90,7 @@ func NewSrtSocket(host string, port uint16, options map[string]string) *SrtSocke
89
90
s .host = host
90
91
s .port = port
91
92
s .options = options
93
+ s .pollTimeout = - 1
92
94
93
95
val , exists := options ["pktsize" ]
94
96
if exists {
@@ -194,7 +196,7 @@ func (s SrtSocket) Accept() (*SrtSocket, *net.UDPAddr, error) {
194
196
if ! s .blocking {
195
197
// Socket readiness for connection is checked by polling on WRITE allowed sockets.
196
198
len := C .int (2 )
197
- timeoutMs := C .int64_t (- 1 )
199
+ timeoutMs := C .int64_t (s . pollTimeout )
198
200
ready := [2 ]C.int {SRT_INVALID_SOCK , SRT_INVALID_SOCK }
199
201
if C .srt_epoll_wait (s .epollConnect , & ready [0 ], & len , nil , nil , timeoutMs , nil , nil , nil , nil ) == - 1 {
200
202
return nil , nil , fmt .Errorf ("srt accept, epoll error: %s" , C .GoString (C .srt_getlasterror_str ()))
@@ -255,7 +257,7 @@ func (s SrtSocket) Connect() error {
255
257
if ! s .blocking {
256
258
// Socket readiness for connection is checked by polling on WRITE allowed sockets.
257
259
len := C .int (2 )
258
- timeoutMs := C .int64_t (- 1 )
260
+ timeoutMs := C .int64_t (s . pollTimeout )
259
261
ready := [2 ]C.int {SRT_INVALID_SOCK , SRT_INVALID_SOCK }
260
262
if C .srt_epoll_wait (s .epollConnect , nil , nil , & ready [0 ], & len , timeoutMs , nil , nil , nil , nil ) != - 1 {
261
263
state := C .srt_getsockstate (s .socket )
@@ -276,17 +278,17 @@ func (s SrtSocket) Connect() error {
276
278
}
277
279
278
280
// Read data from the SRT socket
279
- func (s SrtSocket ) Read (b []byte , timeout int ) (n int , err error ) {
281
+ func (s SrtSocket ) Read (b []byte ) (n int , err error ) {
280
282
if ! s .blocking {
281
283
len := C .int (2 )
282
- timeoutMs := C .int64_t (timeout )
284
+ timeoutMs := C .int64_t (s . pollTimeout )
283
285
ready := [2 ]C.int {SRT_INVALID_SOCK , SRT_INVALID_SOCK }
284
286
285
287
if C .srt_epoll_wait (s .epollIo , & ready [0 ], & len , nil , nil , timeoutMs , nil , nil , nil , nil ) == SRT_ERROR {
286
288
if C .srt_getlasterror (nil ) == C .SRT_ETIMEOUT {
287
289
return 0 , nil
288
290
}
289
- return 0 , fmt .Errorf ("error in read:epoll" )
291
+ return 0 , fmt .Errorf ("error in read:epoll %s" , C . GoString ( C . srt_getlasterror_str ()) )
290
292
}
291
293
}
292
294
@@ -299,22 +301,22 @@ func (s SrtSocket) Read(b []byte, timeout int) (n int, err error) {
299
301
}
300
302
301
303
// Write data to the SRT socket
302
- func (s SrtSocket ) Write (b []byte , timeout int ) (n int , err error ) {
304
+ func (s SrtSocket ) Write (b []byte ) (n int , err error ) {
303
305
if ! s .blocking {
304
- timeoutMs := C .int64_t (timeout )
306
+ timeoutMs := C .int64_t (s . pollTimeout )
305
307
len := C .int (2 )
306
308
ready := [2 ]C.int {SRT_INVALID_SOCK , SRT_INVALID_SOCK }
307
309
rlen := C .int (2 )
308
310
rready := [2 ]C.int {SRT_INVALID_SOCK , SRT_INVALID_SOCK }
309
311
310
312
if C .srt_epoll_wait (s .epollIo , & rready [0 ], & rlen , & ready [0 ], & len , timeoutMs , nil , nil , nil , nil ) == SRT_ERROR {
311
- return 0 , fmt .Errorf ("error in read :epoll" )
313
+ return 0 , fmt .Errorf ("error in write :epoll %s" , C . GoString ( C . srt_getlasterror_str ()) )
312
314
}
313
315
}
314
316
315
317
res := C .srt_sendmsg2 (s .socket , (* C .char )(unsafe .Pointer (& b [0 ])), C .int (len (b )), nil )
316
318
if res == SRT_ERROR {
317
- return 0 , fmt .Errorf ("error in read :srt_sendmsg2" )
319
+ return 0 , fmt .Errorf ("error in write :srt_sendmsg2 %s" , C . GoString ( C . srt_getlasterror_str ()) )
318
320
}
319
321
320
322
return int (res ), nil
@@ -325,7 +327,7 @@ func (s SrtSocket) Stats() (*SrtStats, error) {
325
327
var stats C.SRT_TRACEBSTATS = C.SRT_TRACEBSTATS {}
326
328
var b C.int = 1
327
329
if C .srt_bstats (s .socket , & stats , b ) == SRT_ERROR {
328
- return nil , fmt .Errorf ("Error getting stats" )
330
+ return nil , fmt .Errorf ("Error getting stats, %s" , C . GoString ( C . srt_getlasterror_str ()) )
329
331
}
330
332
331
333
return newSrtStats (& stats ), nil
@@ -341,6 +343,18 @@ func (s SrtSocket) PacketSize() int {
341
343
return s .pktSize
342
344
}
343
345
346
+ // PollTimeout - Return polling max time, in milliseconds, for connect/read/write operations.
347
+ // Only applied when socket is in non-blocking mode.
348
+ func (s SrtSocket ) PollTimeout () int64 {
349
+ return s .pollTimeout
350
+ }
351
+
352
+ // SetPollTimeout - Sets polling max time, in milliseconds, for connect/read/write operations.
353
+ // Only applied when socket is in non-blocking mode.
354
+ func (s SrtSocket ) SetPollTimeout (pollTimeout int64 ) {
355
+ s .pollTimeout = pollTimeout
356
+ }
357
+
344
358
// Close the SRT socket
345
359
func (s SrtSocket ) Close () {
346
360
if ! s .blocking {
@@ -517,7 +531,7 @@ func (s SrtSocket) SetSockOptString(opt int, value string) error {
517
531
func (s SrtSocket ) setSockOpt (opt int , data unsafe.Pointer , size int ) error {
518
532
res := C .srt_setsockopt (s .socket , 0 , C .SRT_SOCKOPT (opt ), data , C .int (size ))
519
533
if res == - 1 {
520
- return fmt .Errorf ("Error calling srt_setsockopt" )
534
+ return fmt .Errorf ("Error calling srt_setsockopt %v" , C . GoString ( C . srt_getlasterror_str ()) )
521
535
}
522
536
523
537
return nil
@@ -526,7 +540,7 @@ func (s SrtSocket) setSockOpt(opt int, data unsafe.Pointer, size int) error {
526
540
func (s SrtSocket ) getSockOpt (opt int , data unsafe.Pointer , size * int ) error {
527
541
res := C .srt_getsockopt (s .socket , 0 , C .SRT_SOCKOPT (opt ), data , (* C .int )(unsafe .Pointer (size )))
528
542
if res == - 1 {
529
- return fmt .Errorf ("Error calling srt_getsockopt" )
543
+ return fmt .Errorf ("Error calling srt_getsockopt %v" , C . GoString ( C . srt_getlasterror_str ()) )
530
544
}
531
545
532
546
return nil
0 commit comments