Esempio n. 1
0
// newObject wraps a Python object.
func newObject(pyObject *C.PyObject) (o Object) {
	t := C.getType(pyObject)
	o, _ = newObjectType(t, pyObject)
	if t > 3 {
		C.INCREF(pyObject)
	}
	return
}
Esempio n. 2
0
func setupArg(arg *C.utp_callback_arguments) *UtpCallbackArguments {
	goarg := UtpCallbackArguments{}
	ctx, ok := contexts[arg.context]
	if !ok {
		logging.Println("not found")
		return nil
	}
	goarg.UtpContext = ctx
	if arg.socket == nil {
		goarg.UtpSocket = nil
	} else {
		sock, ok := ctx.sockets[arg.socket]
		if !ok {
			sock = &UtpSocket{s: arg.socket, ctx: ctx}
			ctx.sockets[arg.socket] = sock
		}
		goarg.UtpSocket = sock
	}
	goarg.Len = (int64)(arg.len)
	goarg.Flags = (uint32)(arg.flags)
	goarg.CallbackType = (int)(arg.callback_type)
	if arg.buf != nil {
		switch goarg.CallbackType {
		case UTP_LOG:
			str := C.GoString((*C.char)(unsafe.Pointer(arg.buf)))
			goarg.Buf = []byte(str)
		case UTP_ON_READ, UTP_SENDTO:
			goarg.Buf = C.GoBytes(unsafe.Pointer(arg.buf), (C.int)(arg.len))
		}
	}
	switch goarg.CallbackType {
	case UTP_ON_FIREWALL, UTP_ON_ACCEPT, UTP_GET_UDP_MTU, UTP_GET_UDP_OVERHEAD, UTP_SENDTO:
		ipAddr := make([]C.char, 46+5) //INET6_ADDRSTRLEN+port
		C.getIPPort(arg, (*C.char)(&ipAddr[0]))
		caddr := C.GoString(&ipAddr[0])
		addr, err := net.ResolveUDPAddr("udp", caddr)
		if err == nil {
			goarg.Address = addr
		} else {
			logging.Println(err)
		}
	default:
		goarg.Send = (int64)(C.getUnion1(arg))
		goarg.SampleMs = goarg.Send
		goarg.ErrorCode = goarg.Send
		goarg.State = goarg.Send
		goarg.Type = (int64)(C.getType(arg))
	}
	return &goarg
}
Esempio n. 3
0
// decode translates a Python object to a Go value.  It must be non-NULL.
func decode(pyValue *C.PyObject) (interface{}, error) {
	return decodeType(C.getType(pyValue), pyValue)
}