从gin的启动开始看socket是如何在服务器中创建的:
gin -> net/http -> net -> internal/syscall -> syscall -> runtime
通过上面的调用流程,可以看出,http是对net的封装,方便开发者调用,实际创建Socket的还是net库,底层已经是到了go的syscall和runtime库了。
最终调用的还是操作系统提供的socket接口。这就是socket创建的流程。
具体流程涉及的文件以及代码:
router.go
r := gin.New() r.Run(address) // host:port
github.com\gin-gonic\[email protected]\gin.go
err = http.ListenAndServe(address, engine)
Go\src\net\http\server.go
// Go net/http包 return server.ListenAndServe() ln, err := net.Listen("tcp", addr) // http也是对net的封装 if err != nil { return err }
Go\src\net\dial.go
return lc.Listen(context.Background(), network, address) l, err = sl.listenTCP(ctx, la)
Go\src\net\tcpsock_posix.go
fd, err := internetSocket(ctx, sl.network, laddr, nil, syscall.SOCK_STREAM, 0, "listen", sl.ListenConfig.Control)
Go\src\net\ipsock_posix.go
return socket(ctx, net, family, sotype, proto, ipv6only, laddr, raddr, ctrlFn) // 出现socket函数了
Go\src\net\sock_posix.go
// socket returns a network file descriptor that is ready for // asynchronous I/O using the network poller. s, err := sysSocket(family, sotype, proto)
Go\src\net\sock_windows.go
s, err := wsaSocketFunc(int32(family), int32(sotype), int32(proto),nil, 0, windows.WSA_FLAG_OVERLAPPED|windows.WSA_FLAG_NO_HANDLE_INHERIT)
Go\src\net\hook_windows.go
wsaSocketFunc func(int32, int32, int32, *syscall.WSAProtocolInfo, uint32, uint32) (syscall.Handle, error) = windows.WSASocket // windows.WSASocket就是返回的socket
Go\src\internal\syscall\windows\zsyscall_windows.go
// 可以看到文件已经跳转到internal文件夹下的系统相关调用目录 func WSASocket(af int32, typ int32, protocol int32, protoInfo *WSAProtocolInfo, group uint32, flags uint32) (handle Handle, err error) { r0, _, e1 := syscall.Syscall6(procWSASocketW.Addr(), 6, uintptr(af), uintptr(typ), uintptr(protocol), uintptr(unsafe.Pointer(protoInfo)), uintptr(group), uintptr(flags)) handle = Handle(r0) if handle == InvalidHandle { err = errnoErr(e1) } return }
=== r0 start ===
C:\Program Files\Go\src\syscall\dll_windows.go
// 可以看到文件已经跳转到syscall系统调用目录,dll_windows名称也可看出该文件底层调用的是windos系统提供的接口。 // Implemented in ../runtime/syscall_windows.go. // 注释中是说继承于../runtime/syscall_windows.go文件中的Syscall6,这个写法比较特殊,是go1.14刚出的,go:linkname,有兴趣的可以继续深入这个写法,这里不做赘述。 func Syscall6(trap, nargs, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
C:\Program Files\Go\src\runtime\syscall_windows.go
//go:linkname syscall_Syscall6 syscall.Syscall6 //go:nosplit //go:cgo_unsafe_args func syscall_Syscall6(fn, nargs, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, err uintptr) { lockOSThread() defer unlockOSThread() c := &getg().m.syscall c.fn = fn c.n = nargs c.args = uintptr(noescape(unsafe.Pointer(&a1))) cgocall(asmstdcallAddr, unsafe.Pointer(c)) return c.r1, c.r2, c.err }
=== r0 end ===
标签:socket,err,syscall,windows,Go,go,gin,net From: https://www.cnblogs.com/yourstars/p/16966275.html