本文整理汇总了Golang中net.TCPConn.Read方法的典型用法代码示例。如果您正苦于以下问题:Golang TCPConn.Read方法的具体用法?Golang TCPConn.Read怎么用?Golang TCPConn.Read使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.TCPConn
的用法示例。
在下文中一共展示了TCPConn.Read方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Pipe
// Pipe starts bridging with two tcp connection
func Pipe(dst *net.TCPConn, src *net.TCPConn, f *func([]byte) []byte) error {
defer src.CloseRead()
defer dst.CloseWrite()
rb := make([]byte, 4096)
for {
rsize, err := src.Read(rb)
if err != nil {
if isRecoverable(err) {
continue
}
return err
}
var wb []byte
if f != nil {
wb = (*f)(rb[:rsize])
} else {
wb = rb[:rsize]
}
wWrote := 0
wTotal := len(wb)
for wWrote != wTotal {
wSize, err := dst.Write(wb[wWrote:])
wWrote += wSize
if err != nil {
if isRecoverable(err) {
continue
}
return err
}
}
}
}
示例2: read_tcp_conn
func read_tcp_conn(tcpConn *net.TCPConn, connChan chan []byte) {
buffer := make([]byte, 2048)
tcpConn.SetReadBuffer(2048)
for {
n, err := tcpConn.Read(buffer[0:])
if err != nil {
log.Println("one tcp connection read function failed!")
log.Println("one tcp connection close now!")
tcpConn.Close()
runtime.Goexit()
} else {
connChan <- buffer[0 : n-1]
}
}
}
示例3: Service
func Service(conn *net.TCPConn) {
b := make([]byte, 1024)
for {
lenth, _ := conn.Read(b)
DisCmd(string(b[:lenth]))
}
}
示例4: readServer
func readServer(pTCPConn *net.TCPConn) {
//buffer := make([]byte, 1024)
tempBuf := make([]byte, 4)
for {
bufferSize, err := pTCPConn.Read(tempBuf)
if err != nil {
fmt.Println("read error: ", err)
break
}
if bufferSize >= 4 {
bodyLen := BytesToInt(tempBuf)
fmt.Println("bodyLen: ", bodyLen)
bodyBuffer := make([]byte, bodyLen)
_, err := pTCPConn.Read(bodyBuffer)
if err != nil {
fmt.Println("err : ", err)
break
}
msg := &protocol.WMessage{}
err = proto.Unmarshal(bodyBuffer, msg)
if err != nil {
fmt.Println("unmarshal:", err)
return
}
fmt.Println("解码:", msg.String())
}
}
}
示例5: readRemoteLoop
func readRemoteLoop(client, remote *net.TCPConn, stopChan chan<- bool) {
defer func() {
stopChan <- true
}()
addr := client.RemoteAddr()
for {
var buf [4096]byte
nr, err := remote.Read(buf[:])
if err != nil && err != os.EOF {
log.Printf("%v: Failed to read from the remote: %v", addr, err)
return
}
start := 0
for start < nr {
nw, err := client.Write(buf[start:nr])
if err != nil && err != os.EOF {
log.Printf("%v: Failed to write to the client: %v", addr, err)
return
}
start += nw
}
}
}
示例6: handleHost
func handleHost(conn *net.TCPConn) {
defer conn.Close()
defer fmt.Printf("Closed host conn.\n")
w := bufio.NewWriter(conn)
name_line := make([]byte, 32)
n, err := conn.Read(name_line)
if err != nil {
// TODO: Log this error
fmt.Printf("Failed to read game from host: %v\n", err)
return
}
name := strings.TrimSpace(string(name_line[0:n]))
fmt.Printf("Game name: '%s': %d\n", name, len(name))
if !registerGame(name, conn) {
w.Write([]byte(fmt.Sprintf("Unable to make game '%s', that name is already in use.\n", name)))
return
}
defer unregisterGame(name)
w.Flush()
game := getGame(name)
game.Run()
}
示例7: handleServiceConnection
// Handles connections to a service and calls the handler specified in RunService().
func handleServiceConnection(connection *net.TCPConn, handler ServiceHandler) error {
servicecall := ServiceCall{}
buffer := make([]byte, PACKET_SIZE)
defer connection.Close()
length, err := connection.Read(buffer)
if err != nil {
return err
}
err = json.Unmarshal(buffer[:length], &servicecall)
if err != nil {
return err
}
ret := handler(&servicecall)
bytes, err := json.Marshal(ServiceResult{ret})
if err != nil {
return err
}
_, err = connection.Write(bytes)
if err != nil {
return err
}
return nil
}
示例8: pipe
func (p *proxy) pipe(src, dst *net.TCPConn, powerCallback common.Callback) {
//data direction
islocal := src == p.lconn
//directional copy (64k buffer)
buff := make([]byte, 0xffff)
for {
n, err := src.Read(buff)
if err != nil {
p.err("Read failed '%s'\n", err)
return
}
b := buff[:n]
//show output
if islocal {
b = getModifiedBuffer(b, powerCallback)
n, err = dst.Write(b)
} else {
//write out result
n, err = dst.Write(b)
}
if err != nil {
p.err("Write failed '%s'\n", err)
return
}
}
}
示例9: readloop
func readloop(conn *net.TCPConn, clist *[]ChanPair, controlc chan chan string) {
output := make(chan string, 2048)
input := make(chan string, 2048)
controlc <- output
controlc <- input
address := conn.RemoteAddr()
player := parsing.NewPlayer(address.String())
for {
b := make([]byte, 4096)
n, err := conn.Read(b[:])
data := b[:n]
if err != nil {
fmt.Println(err)
}
select {
case str := <-input:
conn.Write([]uint8(str))
default:
}
if len(string(data)) == 0 {
fmt.Println("PARTING:", address.String())
conn.Close()
return
}
conn.Write(parsing.Parse(player, string(data), output))
}
}
示例10: readServer
func readServer(pTCPConn *net.TCPConn) {
buffer := make([]byte, 1024)
tempBuf := make([]byte, 4)
for {
bufferSize, err := pTCPConn.Read(buffer)
if err != nil {
return
}
if bufferSize >= 4 {
copy(tempBuf, buffer)
length := common.BytesToInt(tempBuf)
//fmt.Println("length:", length)
newbuffer := make([]byte, length)
copy(newbuffer, buffer[4:])
msg := &protocol.WMessage{}
err = proto.Unmarshal(newbuffer, msg)
if err != nil {
fmt.Println("unmarshal:", err)
return
}
fmt.Println("解码:", msg.String())
}
}
}
示例11: handleTCPConnection
// Handling a particular TCP connection.
func handleTCPConnection(conn *net.TCPConn, noteChannel chan synth.DelayedNoteData) {
defer conn.Close()
buffer := make([]byte, 256)
for {
rlen, err := conn.Read(buffer)
if err == io.EOF {
return
} else if err != nil {
fmt.Println("Failed to read from TCP socket: " + err.Error())
return
}
if rlen > 0 {
strs := strings.Split(strings.TrimSpace(string(buffer[:rlen])), "\n")
for _, v := range strs {
err = HandleMessage(v, noteChannel)
if err != nil {
fmt.Println("Failed to handle message \"" + v + "\": " + err.Error())
}
}
}
buffer = make([]byte, 256)
time.Sleep(1 * time.Millisecond)
}
}
示例12: ReadMessage
// Reading network message as a byte array,
// where first 4 bytes is a uint length for all message
func ReadMessage(conn *net.TCPConn) (msg []byte, err TreeError) {
var (
msg_len_byte = make([]byte, 4)
msg_len uint32
rlen int
)
err.From = FROM_READ_MESSAGE
rlen, err.Err = conn.Read(msg_len_byte)
if !err.IsNull() {
return
}
if rlen != 4 {
err.Err = errors.New("Data lenght reading error. Check API details")
return
}
msg_len = binary.LittleEndian.Uint32(msg_len_byte)
msg = make([]byte, int(msg_len))
rlen, err.Err = conn.Read(msg)
if !err.IsNull() {
return
}
if rlen != int(msg_len) {
err.Err = errors.New(fmt.Sprintf("Message length not equal to given length. Check API details. Given %d message length, but recieved %d", int(msg_len), rlen))
return
}
return
}
示例13: recv
func (b *Smarthomebox) recv(conn *net.TCPConn) {
b.Wg.Add(1)
defer func() {
b.Wg.Done()
}()
for {
select {
case <-b.ExitChan:
return
default:
}
buffer := make([]byte, 1024)
//length, _ := conn.Read(buffer)
conn.Read(buffer)
if buffer[3] == 0x80 && buffer[4] == 0x08 {
b.setnamefeedback(conn, buffer)
} else if buffer[3] == 0x80 && buffer[4] == 0x04 {
b.opfeedback(conn, buffer)
} else if buffer[3] == 0x80 && buffer[4] == 0x0A {
b.delfeedback(conn, buffer)
log.Println("feedback del")
}
// log.Printf("recv %X\n", buffer[0:length])
}
}
示例14: benchcore
func benchcore(host, req *string, port int, callback bench_callback) {
var fail, bs, speed int64
buf := make([]byte, 1500)
reqLen := len(*req)
var conn *net.TCPConn
ds := fmt.Sprintf("%ds", *sec)
dur, _ := time.ParseDuration(ds)
timer := time.NewTimer(dur)
for {
nextconn:
select {
case <-timer.C:
callback(fail, bs, speed)
fmt.Println("Some of our childrens died.")
return
default:
conn = socket.Socket(host, port)
if conn == nil {
fail++
continue
}
wlen, err := conn.Write([]byte(*req))
if wlen != reqLen || err != nil {
fail++
conn.Close()
continue
}
if httpVersion == 0 {
err := conn.CloseWrite()
if err != nil {
fail++
conn.Close()
continue
}
}
if *force == false {
for {
b, err := conn.Read(buf)
if err == io.EOF {
break
} else if err != nil {
fail++
conn.Close()
break nextconn
}
bs += int64(b)
}
}
if conn.Close() != nil {
fail++
continue
}
speed++
}
}
}
示例15: SockRead
func SockRead(con *net.TCPConn, buf []byte) (n int, e error) {
n, e = con.Read(buf)
if e == nil {
count_rcvd(uint64(n))
}
return
}