当前位置: 首页>>代码示例>>Golang>>正文


Golang syscall.ReadFile函数代码示例

本文整理汇总了Golang中syscall.ReadFile函数的典型用法代码示例。如果您正苦于以下问题:Golang ReadFile函数的具体用法?Golang ReadFile怎么用?Golang ReadFile使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了ReadFile函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: read

func (p *Port) read(b []byte) (n int, err error) {
	var done uint32
	err = syscall.ReadFile(p.handle, b, &done, nil)
	n = int(done)

	return
}
开发者ID:datawolf,项目名称:go-study,代码行数:7,代码来源:serial_windows.go

示例2: Read

func (p *serialPort) Read(buf []byte) (int, error) {
	//fmt.Printf("read(<%d bytes>)\n", len(buf))
	if p == nil || p.f == nil {
		return 0, fmt.Errorf("Invalid port on read %v %v", p, p.f)
	}

	p.rl.Lock()
	defer p.rl.Unlock()

	if err := resetEvent(p.ro.HEvent); err != nil {
		return 0, err
	}
	var done uint32
	//fmt.Printf("calling ReadFile... ")
	err := syscall.ReadFile(p.fd, buf, &done, p.ro)
	//fmt.Printf(" done. %d, %v\n", done, err)
	if err != nil && err != syscall.ERROR_IO_PENDING {
		return int(done), err
	}

	//fmt.Printf("getting OverlappedResult... ")
	n, err := getOverlappedResult(p.fd, p.ro)
	//fmt.Printf(" done. n %d err %v\n", n, err)
	if n == 0 && err == nil {
		return n, winSersTimeout{}
	}
	return n, err
}
开发者ID:distributed,项目名称:sers,代码行数:28,代码来源:sers_windows.go

示例3: Read

func (t *tun) Read(ch chan []byte) (err error) {
	overlappedRx := syscall.Overlapped{}
	var hevent windows.Handle
	hevent, err = windows.CreateEvent(nil, 0, 0, nil)
	if err != nil {
		return
	}
	overlappedRx.HEvent = syscall.Handle(hevent)
	buf := make([]byte, t.mtu)
	var l uint32
	for {
		if err := syscall.ReadFile(t.fd, buf, &l, &overlappedRx); err != nil {
		}
		if _, err := syscall.WaitForSingleObject(overlappedRx.HEvent, syscall.INFINITE); err != nil {
			fmt.Println(err)
		}
		overlappedRx.Offset += l
		totalLen := 0
		switch buf[0] & 0xf0 {
		case 0x40:
			totalLen = 256*int(buf[2]) + int(buf[3])
		case 0x60:
			continue
			totalLen = 256*int(buf[4]) + int(buf[5]) + IPv6_HEADER_LENGTH
		}
		fmt.Println("read data", buf[:totalLen])
		send := make([]byte, totalLen)
		copy(send, buf)
		ch <- send
	}
}
开发者ID:FlexibleBroadband,项目名称:tun-go,代码行数:31,代码来源:tun_windows.go

示例4: Read

func (p *Port) Read(buf []byte) (int, error) {
	if p == nil || p.f == nil {
		return 0, fmt.Errorf("Invalid port on read %v %v", p, p.f)
	}

	p.rl.Lock()
	defer p.rl.Unlock()

	fmt.Println("Reset Event")

	if err := resetEvent(p.ro.HEvent); err != nil {
		return 0, err
	}

	fmt.Println("Reset Event Done")

	var done uint32

	//Public Declare Auto Function ReadFile Lib "kernel32.dll" (ByVal hFile As IntPtr, _
	//   ByVal lpBuffer As Byte(), ByVal nNumberOfBytesToRead As Int32, _
	//      ByRef lpNumberOfBytesRead As Int32, ByVal lpOverlapped As IntPtr) As Boolean
	err := syscall.ReadFile(p.fd, buf, &done, p.ro)

	fmt.Println("WriteFile Done")

	if err != nil && err != syscall.ERROR_IO_PENDING {
		return int(done), err
	}
	return getOverlappedResult(p.fd, p.ro)
}
开发者ID:kity-xu,项目名称:xuxiaodong,代码行数:30,代码来源:parallel_windows.go

示例5: Read

// Read implements the net.Conn Read method.
func (c *PipeConn) Read(b []byte) (int, error) {
	// Use ReadFile() rather than Read() because the latter
	// contains a workaround that eats ERROR_BROKEN_PIPE.
	overlapped, err := newOverlapped()
	if err != nil {
		return 0, err
	}
	var n uint32
	err = syscall.ReadFile(c.handle, b, &n, overlapped)
	return c.completeRequest(iodata{n, err}, c.readDeadline, overlapped)
}
开发者ID:postfix,项目名称:npipe,代码行数:12,代码来源:npipe_windows.go

示例6: Read

// Read reads from serial port.
// It is blocked until data received or timeout after p.timeout.
func (p *port) Read(b []byte) (n int, err error) {
	var done uint32
	if err = syscall.ReadFile(p.handle, b, &done, nil); err != nil {
		return
	}
	if done == 0 {
		err = ErrTimeout
		return
	}
	n = int(done)
	return
}
开发者ID:akofoed,项目名称:serial,代码行数:14,代码来源:serial_windows.go

示例7: Read

func (p *pipe) Read(b []byte) (int, error) {
	// syscall.Read returns 0, nil on ERROR_BROKEN_PIPE, but for
	// our purposes this should indicate EOF. This may be a go bug.
	var read uint32
	err := syscall.ReadFile(p.handle, b, &read, nil)
	if err != nil {
		if err == syscall.ERROR_BROKEN_PIPE {
			return 0, io.EOF
		}
		return 0, err
	}
	return int(read), nil
}
开发者ID:DaveDaCoda,项目名称:docker,代码行数:13,代码来源:createprocess.go

示例8: readConsole

// readConsole reads utf16 characters from console File,
// encodes them into utf8 and stores them in buffer b.
// It returns the number of utf8 bytes read and an error, if any.
func (f *File) readConsole(b []byte) (n int, err error) {
	if len(b) == 0 {
		return 0, nil
	}
	if len(f.readbuf) == 0 {
		numBytes := len(b)
		// Windows  can't read bytes over max of int16.
		if numBytes > 32767 {
			numBytes = 32767
		}
		mbytes := make([]byte, numBytes)
		var nmb uint32
		err := syscall.ReadFile(f.fd, mbytes, &nmb, nil)
		if err != nil {
			return 0, err
		}
		if nmb > 0 {
			var pmb *byte
			if len(b) > 0 {
				pmb = &mbytes[0]
			}
			acp, err := windows.GetACP()
			if err != nil {
				return 0, err
			}
			nwc, err := windows.MultiByteToWideChar(acp, 2, pmb, int32(nmb), nil, 0)
			if err != nil {
				return 0, err
			}
			wchars := make([]uint16, nwc)
			pwc := &wchars[0]
			nwc, err = windows.MultiByteToWideChar(acp, 2, pmb, int32(nmb), pwc, int32(nwc))
			if err != nil {
				return 0, err
			}
			f.readbuf = utf16.Decode(wchars[:nwc])
		}
	}
	for i, r := range f.readbuf {
		if utf8.RuneLen(r) > len(b) {
			f.readbuf = f.readbuf[i:]
			return n, nil
		}
		nr := utf8.EncodeRune(b, r)
		b = b[nr:]
		n += nr
	}
	f.readbuf = nil
	return n, nil
}
开发者ID:danny8002,项目名称:go,代码行数:53,代码来源:file_windows.go

示例9: Read

func (p *virtioPort) Read(buf []byte) (int, error) {
	if p == nil || p.f == nil {
		return 0, fmt.Errorf("Invalid port on read %v %v", p, p.f)
	}
	p.rl.Lock()
	defer p.rl.Unlock()
	if err := resetEvent(p.ro.HEvent); err != nil {
		return 0, err
	}
	var done uint32
	err := syscall.ReadFile(p.fd, buf, &done, p.ro)
	if err != nil && err != syscall.ERROR_IO_PENDING {
		return int(done), err
	}
	return getOverlappedResult(p.fd, p.ro)
}
开发者ID:npe9,项目名称:minimega,代码行数:16,代码来源:virtio_windows.go

示例10: Read

// Read reads from a file handle.
func (f *win32File) Read(b []byte) (int, error) {
	c, err := f.prepareIo()
	if err != nil {
		return 0, err
	}
	var bytes uint32
	err = syscall.ReadFile(f.handle, b, &bytes, &c.o)
	n, err := f.asyncIo(c, f.readDeadline, bytes, err)

	// Handle EOF conditions.
	if err == nil && n == 0 && len(b) != 0 {
		return 0, io.EOF
	} else if err == syscall.ERROR_BROKEN_PIPE {
		return 0, io.EOF
	} else {
		return n, err
	}
}
开发者ID:ChristianKniep,项目名称:swarmkit,代码行数:19,代码来源:file.go

示例11: pread

// pread reads len(b) bytes from the File starting at byte offset off.
// It returns the number of bytes read and the error, if any.
// EOF is signaled by a zero count with err set to 0.
func (f *File) pread(b []byte, off int64) (n int, err error) {
	f.l.Lock()
	defer f.l.Unlock()
	curoffset, e := syscall.Seek(f.fd, 0, 1)
	if e != nil {
		return 0, e
	}
	defer syscall.Seek(f.fd, curoffset, 0)
	o := syscall.Overlapped{
		OffsetHigh: uint32(off >> 32),
		Offset:     uint32(off),
	}
	var done uint32
	e = syscall.ReadFile(syscall.Handle(f.fd), b, &done, &o)
	if e != nil {
		return 0, e
	}
	return int(done), nil
}
开发者ID:timnau,项目名称:golang,代码行数:22,代码来源:file_windows.go

示例12: Read

func (t *Interface) Read(ch chan []byte) (err error) {
	rx := syscall.Overlapped{}
	var hevent windows.Handle
	hevent, err = windows.CreateEvent(nil, 0, 0, nil)
	if err != nil {
		return
	}
	rx.HEvent = syscall.Handle(hevent)
	buf := make([]byte, 1500)
	var l uint32
	for {
		if err := syscall.ReadFile(t.file, buf, &l, &rx); err != nil {
		}
		if _, err := syscall.WaitForSingleObject(rx.HEvent, syscall.INFINITE); err != nil {
			Log(Error, "Failed to read from TUN/TAP: %v", err)
		}
		rx.Offset += l
		ch <- buf
	}
}
开发者ID:subutai-io,项目名称:p2p,代码行数:20,代码来源:tuntap_windows.go

示例13: Read

func (p *dev) Read(buf []byte) (int, error) {
	var done uint32

	for {
		var ov syscall.Overlapped

		ov.HEvent = p.ev.r
		if e := syscall.ReadFile(p.fd, buf, &done, &ov); e != nil {
			if e != syscall.ERROR_IO_PENDING {
				return 0, &os.PathError{"reading from", p.name, e}
			}
			if e = win.GetOverlappedResult(p.fd, &ov, &done, 1); e != nil {
				return 0, &os.PathError{"reading from", p.name, e}
			}
		}
		if done > 0 {
			break
		}
	}
	return int(done), nil
}
开发者ID:jeffallen,项目名称:g,代码行数:21,代码来源:windows.go

示例14: Read

func (port *windowsPort) Read(p []byte) (int, error) {
	var readed uint32
	params := &dcb{}
	for {
		if err := syscall.ReadFile(port.handle, p, &readed, nil); err != nil {
			return int(readed), err
		}
		if readed > 0 {
			return int(readed), nil
		}

		// At the moment it seems that the only reliable way to check if
		// a serial port is alive in Windows is to check if the SetCommState
		// function fails.

		getCommState(port.handle, params)
		if err := setCommState(port.handle, params); err != nil {
			port.Close()
			return 0, err
		}
	}
}
开发者ID:cswank,项目名称:quimby,代码行数:22,代码来源:serial_windows.go

示例15: Read

func (dev *SerialDevice) Read(buf []byte) (int, error) {
	if dev == nil || dev.f == nil {
		return 0, fmt.Errorf("[ERROR] invalid port on read: %v %v", dev, dev.f)
	}

	dev.rl.Lock()
	defer dev.rl.Unlock()

	if err := syscallEvent(dev.ro.HEvent, "reset"); err != nil {
		fmt.Printf("[errr] %v\n", err)
		return 0, err
	}

	var done uint32
	err := syscall.ReadFile(dev.fd, buf, &done, dev.ro)
	if err != nil && err != syscall.ERROR_IO_PENDING {
		fmt.Printf("[errr] %v\n", err)
		return int(done), err
	}

	return getOverlapResult(dev.fd, dev.ro)
}
开发者ID:shotantan,项目名称:5DPrint,代码行数:22,代码来源:serial_windows.go


注:本文中的syscall.ReadFile函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。