forked from Haivision/srtgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
write.go
55 lines (48 loc) · 1.08 KB
/
write.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package srtgo
/*
#cgo LDFLAGS: -lsrt
#include <srt/srt.h>
int srt_sendmsg2_wrapped(SRTSOCKET u, const char* buf, int len, SRT_MSGCTRL *mctrl, int *srterror, int *syserror)
{
int ret = srt_sendmsg2(u, buf, len, mctrl);
if (ret < 0) {
*srterror = srt_getlasterror(syserror);
}
return ret;
}
*/
import "C"
import (
"errors"
"syscall"
"unsafe"
)
func srtSendMsg2Impl(u C.SRTSOCKET, buf []byte, msgctrl *C.SRT_MSGCTRL) (n int, err error) {
srterr := C.int(0)
syserr := C.int(0)
n = int(C.srt_sendmsg2_wrapped(u, (*C.char)(unsafe.Pointer(&buf[0])), C.int(len(buf)), msgctrl, &srterr, &syserr))
if n < 0 {
srterror := SRTErrno(srterr)
if syserr < 0 {
srterror.wrapSysErr(syscall.Errno(syserr))
}
err = srterror
n = 0
}
return
}
// Write data to the SRT socket
func (s SrtSocket) Write(b []byte) (n int, err error) {
//Fastpath:
if !s.blocking {
s.pd.reset(ModeWrite)
}
n, err = srtSendMsg2Impl(s.socket, b, nil)
for {
if !errors.Is(err, error(EAsyncSND)) || s.blocking {
return
}
s.pd.wait(ModeWrite, false)
n, err = srtSendMsg2Impl(s.socket, b, nil)
}
}