예제 #1
0
파일: nflog.go 프로젝트: ncw/go-nflog-acctd
// Receive packets in a loop until quit
func (nflog *NfLog) Loop() {
	buflen := C.size_t(RecvBufferSize)
	pbuf := C.malloc(buflen)
	if pbuf == nil {
		log.Fatal("No memory for malloc")
	}
	defer C.free(pbuf)
	for {
		nr, err := C.recv(nflog.fd, pbuf, buflen, 0)
		select {
		case <-nflog.quit:
			return
		default:
		}
		if nr < 0 || err != nil {
			log.Printf("Recv failed: %s", err)
			nflog.errors++
		} else {
			// Handle messages in packet reusing memory
			ps := <-nflog.a.returnAddPackets
			nflog.packets.index = 0
			C.nflog_handle_packet(nflog.h, (*C.char)(pbuf), (C.int)(nr))
			nflog.a.processAddPackets <- nflog.processPackets(ps[:0])
		}
	}

}
예제 #2
0
// Receive packets in a loop until quit
func (nflog *NfLog) Loop() {
	buf := make([]byte, RecvBufferSize)
	pbuf := unsafe.Pointer(&buf[0])
	buflen := C.size_t(len(buf))
	for !nflog.quit {
		nr := C.recv(nflog.fd, pbuf, buflen, 0)
		if nr < 0 {
			log.Printf("Recvfrom failed: %s", strerror())
			nflog.errors++
		} else {
			// Handle messages in packet reusing memory
			ps := <-nflog.a.returnAddPackets
			nflog.addPackets = ps[:0]
			C.nflog_handle_packet(nflog.h, (*C.char)(pbuf), (C.int)(nr))
			nflog.a.processAddPackets <- nflog.addPackets
			nflog.addPackets = nil
		}
	}

}
예제 #3
0
// transact calls a method on a Java object instance.
// It blocks until the call is complete.
func transact(ref *seq.Ref, _ string, code int, inBuf *seq.Buffer) *seq.Buffer {
	var (
		out    *C.uint8_t = nil
		outLen C.size_t   = 0
		in     *C.uint8_t = nil
		inLen  C.size_t   = 0
	)

	if len(inBuf.Data) > 0 {
		in = (*C.uint8_t)(unsafe.Pointer(&inBuf.Data[0]))
		inLen = C.size_t(len(inBuf.Data))
	}

	C.recv(C.int32_t(ref.Num), C.int(code), in, inLen, &out, &outLen)
	if outLen > 0 {
		outBuf := &seq.Buffer{
			Data: make([]byte, outLen),
		}
		copy(outBuf.Data, (*[maxSliceLen]byte)(unsafe.Pointer(out))[:outLen])
		return outBuf
	}
	return nil
}