-
Notifications
You must be signed in to change notification settings - Fork 53
/
accept.go
69 lines (61 loc) · 1.52 KB
/
accept.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package srtgo
/*
#cgo LDFLAGS: -lsrt
#include <srt/srt.h>
SRTSOCKET srt_accept_wrapped(SRTSOCKET lsn, struct sockaddr* addr, int* addrlen, int *srterror, int *syserror)
{
int ret = srt_accept(lsn, addr, addrlen);
if (ret < 0) {
*srterror = srt_getlasterror(syserror);
}
return ret;
}
*/
import "C"
import (
"fmt"
"net"
"syscall"
"unsafe"
)
func srtAcceptImpl(lsn C.SRTSOCKET, addr *C.struct_sockaddr, addrlen *C.int) (C.SRTSOCKET, error) {
srterr := C.int(0)
syserr := C.int(0)
socket := C.srt_accept_wrapped(lsn, addr, addrlen, &srterr, &syserr)
if srterr != 0 {
srterror := SRTErrno(srterr)
if syserr < 0 {
srterror.wrapSysErr(syscall.Errno(syserr))
}
return socket, srterror
}
return socket, nil
}
// Accept an incoming connection
func (s SrtSocket) Accept() (*SrtSocket, *net.UDPAddr, error) {
var err error
if !s.blocking {
err = s.pd.wait(ModeRead)
if err != nil {
return nil, nil, err
}
}
var addr syscall.RawSockaddrAny
sclen := C.int(syscall.SizeofSockaddrAny)
socket, err := srtAcceptImpl(s.socket, (*C.struct_sockaddr)(unsafe.Pointer(&addr)), &sclen)
if err != nil {
return nil, nil, err
}
if socket == SRT_INVALID_SOCK {
return nil, nil, fmt.Errorf("srt accept, error accepting the connection: %w", srtGetAndClearError())
}
newSocket, err := newFromSocket(&s, socket)
if err != nil {
return nil, nil, fmt.Errorf("new socket could not be created: %w", err)
}
udpAddr, err := udpAddrFromSockaddr(&addr)
if err != nil {
return nil, nil, err
}
return newSocket, udpAddr, nil
}