Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic logic for g_socket #8

Merged
merged 6 commits into from
May 7, 2024
Merged
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
2 changes: 2 additions & 0 deletions glib/glib.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ const (
TYPE_VARIANT Type = C.G_TYPE_VARIANT
)

var TYPE_SOCKET Type = Type(C.G_TYPE_SOCKET) // is function g_socket_get_type() inside macro, can't be const in Go

// IsValue checks whether the passed in type can be used for g_value_init().
func (t Type) IsValue() bool {
return gobool(C._g_type_is_value(C.GType(t)))
Expand Down
45 changes: 45 additions & 0 deletions glib/gsocket.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package glib

// #include <gio/gio.h>
import "C"
import (
"errors"
"syscall"
"unsafe"

"golang.org/x/exp/constraints"
)

type Socket struct {
*Object
}

func SocketNew(domain, typ, proto int) (*Socket, error) {
fd, err := syscall.Socket(domain, typ, proto)
if err != nil {
return nil, err
}
return SocketNewFromFd(fd)
}

// generic because fd is 'syscall.Handle' (uintptr) for Windows and 'int' for others
// just to have same code for all platforms.
func SocketNewFromFd[T constraints.Integer](fd T) (*Socket, error) {
RSWilli marked this conversation as resolved.
Show resolved Hide resolved
var gerr *C.GError
socket := C.g_socket_new_from_fd((C.gint)(fd), (**C.GError)(unsafe.Pointer(&gerr)))
if gerr != nil {
defer C.g_error_free(gerr)
return nil, errors.New(C.GoString(gerr.message))
}

return &Socket{Take(unsafe.Pointer(socket))}, nil
}

func (s *Socket) ToGValue() (*Value, error) {
val, err := ValueInit(TYPE_SOCKET)
if err != nil {
return nil, err
}
val.SetInstance(unsafe.Pointer(s.GObject))
return val, nil
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ module github.com/go-gst/go-glib
go 1.22

require github.com/mattn/go-pointer v0.0.1

require golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/mattn/go-pointer v0.0.1 h1:n+XhsuGeVO6MEAp7xyEukFINEa+Quek5psIR/ylA6o0=
github.com/mattn/go-pointer v0.0.1/go.mod h1:2zXcozF6qYGgmsG+SeTZz3oAbFLdD3OWqnUbNvJZAlc=
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f h1:99ci1mjWVBWwJiEKYY6jWa4d2nTQVIEhZIptnrVb1XY=
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f/go.mod h1:/lliqkxwWAhPjf5oSOIJup2XcqJaw8RGS6k3TGEc7GI=
Loading