func requestsSender(w *bufio.Writer, requests <-chan tasker, responses chan<- tasker, c net.Conn, done *sync.WaitGroup) { defer done.Done() defer w.Flush() defer close(responses) scratchBuf := make([]byte, 0, 1024) for { var t tasker var ok bool // Flush w only if there are no pending requests. select { case t, ok = <-requests: default: w.Flush() t, ok = <-requests } if !ok { break } if !t.WriteRequest(w, &scratchBuf) { t.Done(false) break } responses <- t } }
/* Handhake described in (soon obsolete) draft-hixie-thewebsocket-protocol-75. */ func draft75handshake(resourceName, host, origin, location, protocol string, br *bufio.Reader, bw *bufio.Writer) (err os.Error) { bw.WriteString("GET " + resourceName + " HTTP/1.1\r\n") bw.WriteString("Upgrade: WebSocket\r\n") bw.WriteString("Connection: Upgrade\r\n") bw.WriteString("Host: " + host + "\r\n") bw.WriteString("Origin: " + origin + "\r\n") if protocol != "" { bw.WriteString("WebSocket-Protocol: " + protocol + "\r\n") } bw.WriteString("\r\n") bw.Flush() resp, err := http.ReadResponse(br, "GET") if err != nil { return } if resp.Status != "101 Web Socket Protocol Handshake" { return ErrBadStatus } if resp.Header["Upgrade"] != "WebSocket" || resp.Header["Connection"] != "Upgrade" { return ErrBadUpgrade } if resp.Header["Websocket-Origin"] != origin { return ErrBadWebSocketOrigin } if resp.Header["Websocket-Location"] != location { return ErrBadWebSocketLocation } if protocol != "" && resp.Header["Websocket-Protocol"] != protocol { return ErrBadWebSocketProtocol } return }
func (r Resolver) outputResults(w *bufio.Writer, results gresults) { var zip, city, state, stateName, county, country, countryName string var latitude, longitude float32 var comp addressComponent var err error for _, res := range results.Results { if comp, err = res.findAddressComponent(zipType); err == nil { zip = comp.LongName } if comp, err = res.findAddressComponent(cityType); err == nil { city = comp.LongName } if comp, err = res.findAddressComponent(countyType); err == nil { county = comp.LongName } if comp, err = res.findAddressComponent(stateType); err == nil { state = strings.ToUpper(comp.ShortName) stateName = comp.LongName } if comp, err = res.findAddressComponent(countryType); err == nil { country = strings.ToUpper(comp.ShortName) countryName = comp.LongName } latitude = res.Geometry.Location.Lat longitude = res.Geometry.Location.Lng } w.WriteString(fmt.Sprintf("%v,\"%v\",\"%v\",\"%v\",\"%v\",\"%v\",\"%v\",%v,%v\n", country, zip, city, state, stateName, county, countryName, latitude, longitude)) w.Flush() }
// NewStreamReader returns a reader, which replays all the data generated by sw. // // The returned reader may be passed to Response.SetBodyStream. // // Close must be called on the returned reader after all the required data // has been read. Otherwise goroutine leak may occur. // // See also Response.SetBodyStreamWriter. func NewStreamReader(sw StreamWriter) io.ReadCloser { pc := fasthttputil.NewPipeConns() pw := pc.Conn1() pr := pc.Conn2() var bw *bufio.Writer v := streamWriterBufPool.Get() if v == nil { bw = bufio.NewWriter(pw) } else { bw = v.(*bufio.Writer) bw.Reset(pw) } go func() { defer func() { if r := recover(); r != nil { defaultLogger.Printf("panic in StreamWriter: %s\nStack trace:\n%s", r, debug.Stack()) } }() sw(bw) bw.Flush() pw.Close() streamWriterBufPool.Put(bw) }() return pr }
func (conn *Connection) writer() { var w *bufio.Writer var err error for { var packet []byte select { case packet = <-conn.packets: default: if w = conn.w; w != nil { if err := w.Flush(); err != nil { conn.closeConnection(err) } } select { case packet = <-conn.packets: case <-conn.control: return } } if packet == nil { return } if w = conn.w; w == nil { if _, w, err = conn.createConnection(); err != nil { return } } if err := write(w, packet); err != nil { conn.closeConnection(err) continue } } }
func (t *TcpPlayer) sendUpdates(w *bufio.Writer, wg *sync.WaitGroup) { defer wg.Done() enc := json.NewEncoder(w) lastTurnSent := -1 for update := range t.update { t.stateL.Lock() t.state = update t.stateL.Unlock() if update.Turn == lastTurnSent { continue } if err := enc.Encode(update); err != nil { t.l.Errorf("[TCP] sending update to player, %v", err) return } if _, err := w.WriteRune('\n'); err != nil { t.l.Errorf("[TCP] sending EOL to player, %v", err) return } if err := w.Flush(); err != nil { t.l.Errorf("[TCP] flushing update to player, %v", err) return } lastTurnSent = update.Turn } }
func WriteProto(wr *bufio.Writer, proto *Proto) (err error) { // write if err = binary.Write(wr, binary.BigEndian, uint32(rawHeaderLen)+uint32(len(proto.Body))); err != nil { return } if err = binary.Write(wr, binary.BigEndian, rawHeaderLen); err != nil { return } if err = binary.Write(wr, binary.BigEndian, proto.Ver); err != nil { return } if err = binary.Write(wr, binary.BigEndian, proto.Operation); err != nil { return } if err = binary.Write(wr, binary.BigEndian, proto.SeqId); err != nil { return } if proto.Body != nil { log.Debug("cipher body: %v", proto.Body) if err = binary.Write(wr, binary.BigEndian, proto.Body); err != nil { return } } err = wr.Flush() return }
func writeErrorResponseHeader(w *bufio.Writer, opcode uint8, status uint16, opaque uint32) error { header := resHeadPool.Get().(ResponseHeader) header.Magic = MagicResponse header.Opcode = opcode header.KeyLength = uint16(0) header.ExtraLength = uint8(0) header.DataType = uint8(0) header.Status = status header.TotalBodyLength = uint32(0) header.OpaqueToken = opaque header.CASToken = uint64(0) if err := writeResponseHeader(w, header); err != nil { resHeadPool.Put(header) return err } if err := w.Flush(); err != nil { resHeadPool.Put(header) return err } metrics.IncCounterBy(common.MetricBytesWrittenRemote, resHeaderLen) resHeadPool.Put(header) return nil }
func deleteGroup(r *bufio.Reader, w *bufio.Writer, args []string, thisUser *utils.User) (err error) { if len(args) < 2 { fmt.Println("Not enough arguments for call to group-remove:") fmt.Println("Format should be: delete-group GID") return nil } err = w.WriteByte(24) w.Flush() if err != nil { return err } w.WriteString(args[1] + "\n") w.Flush() // get success (1) or fail (2) success, _ := r.ReadByte() if success != 1 { fmt.Println("You cannot remove this group. Does it exist/are you the owner?") return err } fmt.Println("Removed: " + args[1]) return err }
// auth for goim handshake with client, use rsa & aes. func (server *Server) authTCP(rr *bufio.Reader, wr *bufio.Writer, ch *Channel) (subKey string, heartbeat time.Duration, err error) { var p *Proto // WARN // don't adv the cli proto, after auth simply discard it. if p, err = ch.CliProto.Set(); err != nil { return } if err = server.readTCPRequest(rr, p); err != nil { return } if p.Operation != define.OP_AUTH { log.Warn("auth operation not valid: %d", p.Operation) err = ErrOperation return } if subKey, ch.RoomId, heartbeat, err = server.operator.Connect(p); err != nil { log.Error("operator.Connect error(%v)", err) return } p.Body = nil p.Operation = define.OP_AUTH_REPLY if err = server.writeTCPResponse(wr, p); err != nil { log.Error("[%s] server.sendTCPResponse() error(%v)", subKey, err) } err = wr.Flush() return }
func writeSuccessResponseHeader(w *bufio.Writer, opcode uint8, keyLength, extraLength, totalBodyLength int, opaque uint32, flush bool) error { header := resHeadPool.Get().(ResponseHeader) header.Magic = MagicResponse header.Opcode = opcode header.KeyLength = uint16(keyLength) header.ExtraLength = uint8(extraLength) header.DataType = uint8(0) header.Status = StatusSuccess header.TotalBodyLength = uint32(totalBodyLength) header.OpaqueToken = opaque header.CASToken = uint64(0) if err := writeResponseHeader(w, header); err != nil { resHeadPool.Put(header) return err } if flush { if err := w.Flush(); err != nil { resHeadPool.Put(header) return err } } metrics.IncCounterBy(common.MetricBytesWrittenRemote, resHeaderLen) resHeadPool.Put(header) return nil }
func testWriteMatch(t *testing.T, obj writeObject, expected string) { var buf *bytes.Buffer = bytes.NewBuffer(nil) var wr *bufio.Writer = bufio.NewWriter(buf) var err error err = obj.write(wr) if err != nil { t.Errorf("Error: %#v", err) return } err = wr.Flush() if err != nil { t.Errorf("Error: %#v", err) return } var bExpected []byte = []byte(expected) var bActual []byte = buf.Bytes() if !bytes.Equal(bExpected, bActual) { t.Errorf("Expected: %#v, got: %#v", string(bExpected), string(bActual)) return } }
func readFile(path string) { var w *bufio.Writer = bufio.NewWriter(os.Stdout) defer w.Flush() fmt.Fprintf(w, "read file start: %s\n", path) file, err := os.OpenFile(path, os.O_RDONLY, 0666) // 打开文件 if err != nil { fmt.Printf("open file err: %v\n", err) return } defer file.Close() // 延时关闭文件 var buff []byte = make([]byte, 1024) var r *bufio.Reader = bufio.NewReader(file) for { n, _ := r.Read(buff) if n <= 0 { fmt.Fprintf(w, "read file end: %s %d\n", path, n) return } w.Write(buff[0:n]) } }
func marshalText(w io.Writer, pb Message, compact bool) error { val := reflect.ValueOf(pb) if pb == nil || val.IsNil() { w.Write([]byte("<nil>")) return nil } var bw *bufio.Writer ww, ok := w.(writer) if !ok { bw = bufio.NewWriter(w) ww = bw } aw := &textWriter{ w: ww, complete: true, compact: compact, } // Dereference the received pointer so we don't have outer < and >. v := reflect.Indirect(val) if err := writeStruct(aw, v); err != nil { return err } if bw != nil { return bw.Flush() } return nil }
func HandshakeSample(c net.Conn, br *bufio.Reader, bw *bufio.Writer, timeout time.Duration) (err error) { defer func() { if r := recover(); r != nil { err = r.(error) } }() // Send C0+C1 err = bw.WriteByte(0x03) c1 := CreateRandomBlock(RTMP_SIG_SIZE) for i := 0; i < 8; i++ { c1[i] = 0 } bw.Write(c1) err = bw.Flush() CheckError(err, "Handshake() Flush C0+C1") // Read S0+S1+S2 s0, err := br.ReadByte() CheckError(err, "Handshake() Read S0") if s0 != 0x03 { return errors.New(fmt.Sprintf("Handshake() Got S0: %x", s0)) } s1 := make([]byte, RTMP_SIG_SIZE) _, err = io.ReadAtLeast(br, s1, RTMP_SIG_SIZE) CheckError(err, "Handshake() Read S1") bw.Write(s1) err = bw.Flush() CheckError(err, "Handshake() Flush C2") _, err = io.ReadAtLeast(br, s1, RTMP_SIG_SIZE) CheckError(err, "Handshake() Read S2") return }
func groupLs(r *bufio.Reader, w *bufio.Writer, args []string, thisUser *utils.User) (err error) { if len(args) != 2 { fmt.Println("Wrong number of arguments for call to group-ls:") fmt.Println("Format should be: group-ls GID") return nil } err = w.WriteByte(23) w.Flush() if err != nil { return err } // send the group id w.WriteString(args[1] + "\n") w.Flush() // get success (1) or fail (2) success, _ := r.ReadByte() if success != 1 { fmt.Println("The group does not exist") return err } // get uuids of the group result, _ := r.ReadString('\n') result = strings.TrimSuffix(strings.TrimSpace(result), ",") fmt.Println("Members of Group " + args[1] + ": " + result) return err }
func SendHandshakeResponse(writer *bufio.Writer, status byte) (err error) { _, err = writer.Write([]byte{0x05, status}) if err != nil { return } return writer.Flush() }
// EncodeData encodes the entire data received so far into a JavaScript array format. func (x *SeriesSweeper) EncodeData(w io.Writer) error { var bw *bufio.Writer = bufio.NewWriter(w) bw.WriteByte(byte('[')) for e := x.chrono.Front(); e != nil; e = e.Next() { s := e.Value.(*sample) bw.WriteByte(byte('[')) fmt.Fprintf(bw, "%0.3f,", s.Time) for i, series := range x.series { if series == s.Series { fmt.Fprintf(bw, "%0.3f", s.Value) } else { bw.WriteString("null") } if i < len(x.series)-1 { bw.WriteByte(byte(',')) } } bw.WriteByte(']') if e.Next() != nil { bw.WriteByte(byte(',')) } bw.WriteByte('\n') } bw.WriteByte(byte(']')) return bw.Flush() }
func (c *DefaultServerCodec) WriteResponse(wr *bufio.Writer, proto *Proto) (err error) { log.Debug("write proto: %v", proto) // packlen =header(16) + body if err = binary.Write(wr, binary.BigEndian, uint32(rawHeaderLen)+uint32(len(proto.Body))); err != nil { log.Error("packLen: binary.Write() error(%v)", err) return } if err = binary.Write(wr, binary.BigEndian, rawHeaderLen); err != nil { log.Error("headerLen: binary.Write() error(%v)", err) return } if err = binary.Write(wr, binary.BigEndian, proto.Ver); err != nil { log.Error("protoVer: binary.Write() error(%v)", err) return } if err = binary.Write(wr, binary.BigEndian, proto.Operation); err != nil { log.Error("operation: binary.Write() error(%v)", err) return } if err = binary.Write(wr, binary.BigEndian, proto.SeqId); err != nil { log.Error("seqId: binary.Write() error(%v)", err) return } if proto.Body != nil { if _, err = wr.Write(proto.Body); err != nil { log.Error("body: wr.Write() error(%v)", err) return } } return wr.Flush() }
// sendResponse send resp to client, sendResponse must be goroutine safe. func (server *Server) writeTCPResponse(wr *bufio.Writer, pb []byte, proto *Proto) (err error) { log.Debug("write proto: %v", proto) BigEndian.PutInt32(pb[:packLenSize], int32(rawHeaderLen)+int32(len(proto.Body))) if _, err = wr.Write(pb[:packLenSize]); err != nil { return } BigEndian.PutInt16(pb[:headerLenSize], rawHeaderLen) if _, err = wr.Write(pb[:headerLenSize]); err != nil { return } BigEndian.PutInt16(pb[:VerSize], proto.Ver) if _, err = wr.Write(pb[:VerSize]); err != nil { return } BigEndian.PutInt32(pb[:OperationSize], proto.Operation) if _, err = wr.Write(pb[:OperationSize]); err != nil { return } BigEndian.PutInt32(pb[:SeqIdSize], proto.SeqId) if _, err = wr.Write(pb[:SeqIdSize]); err != nil { return } if proto.Body != nil { if _, err = wr.Write(proto.Body); err != nil { return } } if err = wr.Flush(); err != nil { log.Error("tcp wr.Flush() error(%v)", err) } proto.Reset() return }
func writeToStderr(msg string, errWriter *bufio.Writer) { if !strings.HasSuffix(msg, "\n") { msg = msg + "\n" } errWriter.WriteString(msg) errWriter.Flush() }
//take input from srvChan and send to server func writeToServer(w *bufio.Writer, srvChan chan string, wg *sync.WaitGroup, quit chan bool, quitChans chan chan bool) { defer wg.Done() defer fmt.Println("WTS") //debug _, err := w.WriteString("PING" + config.Nick + "\r\n") //test message. primarily to get to select loop if err == nil { err = w.Flush() } //send all lines in srvChan to server for err == nil { select { case <-quit: //exit if indicated return case str := <-srvChan: _, err = w.WriteString(str + "\r\n") if err == nil { err = w.Flush() } } } //print error and exit if err != nil { errOut(err, quitChans) } }
// authenticate authenticates ourselves with the X server. // displayStr is the "12" out of ":12.0". func authenticate(w *bufio.Writer, displayStr string) os.Error { key, value, err := readAuth(displayStr) if err != nil { return err } // Assume that the authentication protocol is "MIT-MAGIC-COOKIE-1". if len(key) != 18 || len(value) != 16 { return os.NewError("unsupported Xauth") } // 0x006c means little-endian. 0x000b, 0x0000 means X major version 11, minor version 0. // 0x0012 and 0x0010 means the auth key and value have lengths 18 and 16. // The final 0x0000 is padding, so that the string length is a multiple of 4. _, err = io.WriteString(w, "\x6c\x00\x0b\x00\x00\x00\x12\x00\x10\x00\x00\x00") if err != nil { return err } _, err = io.WriteString(w, key) if err != nil { return err } // Again, the 0x0000 is padding. _, err = io.WriteString(w, "\x00\x00") if err != nil { return err } _, err = io.WriteString(w, value) if err != nil { return err } err = w.Flush() if err != nil { return err } return nil }
// Write Bind Response PDU func (pdu *PDUBindResp) write(w *bufio.Writer) (err os.Error) { // Write Header err = pdu.Header.write(w) if err != nil { err = os.NewError("Bind Response: Error writing Header") return } // Create byte array the size of the PDU p := make([]byte, pdu.Header.CmdLength-pdu.OptionalLen-16) pos := 0 // Copy system id if len(pdu.SystemId) > 0 { copy(p[pos:len(pdu.SystemId)], []byte(pdu.SystemId)) pos += len(pdu.SystemId) } pos++ // Null terminator // Write to buffer _, err = w.Write(p) if err != nil { err = os.NewError("Bind Response: Error writing to buffer") return } // Flush write buffer err = w.Flush() if err != nil { err = os.NewError("Bind Response: Error flushing write buffer") } // Optional params err = pdu.writeOptional(w) if err != nil { err = os.NewError("Bind Response: Error writing optional params") } return }
func writeBodyFixedSize(w *bufio.Writer, r io.Reader, size int64) error { if size > maxSmallFileSize { // w buffer must be empty for triggering // sendfile path in bufio.Writer.ReadFrom. if err := w.Flush(); err != nil { return err } } // Unwrap a single limited reader for triggering sendfile path // in net.TCPConn.ReadFrom. lr, ok := r.(*io.LimitedReader) if ok { r = lr.R } n, err := copyZeroAlloc(w, r) if ok { lr.N -= n } if n != size && err == nil { err = fmt.Errorf("copied %d bytes from body stream instead of %d bytes", n, size) } return err }
// Write Optional param func (op *pduOptParam) write(w *bufio.Writer) (err os.Error) { // Create byte array p := make([]byte, 4+op.length) copy(p[0:2], packUint(uint64(op.tag), 2)) copy(p[2:4], packUint(uint64(op.length), 2)) // Determine data type of value v := reflect.NewValue(op.value) switch t := v.(type) { case *reflect.StringValue: copy(p[4:op.length], []byte(op.value.(string))) case *reflect.Uint8Value: p[4] = byte(op.value.(uint8)) case *reflect.Uint16Value: copy(p[4:6], packUint(uint64(op.value.(uint16)), 2)) case *reflect.Uint32Value: copy(p[4:8], packUint(uint64(op.value.(uint32)), 4)) } // Write to buffer _, err = w.Write(p) if err != nil { return } // Flush write buffer err = w.Flush() return }
func writeHeader(output *bufio.Writer) { if *verbose && !*dryRun { fmt.Println("Writing source header") } licensePath := pathutils.Join(*projectPath, "GPL3-header.txt") license, err := os.Open(licensePath) if err != nil { panic(err) } defer func() { if err := license.Close(); err != nil { panic(err) } }() reader := bufio.NewReader(license) for { if *verbose { fmt.Printf("Got line: '%s'\n") } line, err := reader.ReadString('\n') if err != nil { if err != io.EOF { panic(err) } } fmt.Fprintf(output, "// %s", line) if err == io.EOF { break } } output.WriteString("\n//\n") output.Flush() }
func main() { flag.Parse() checkFlags() var csvWriter *bufio.Writer if *csvFile != "" { f, err := os.Create(*csvFile) if err != nil { log.Fatal(err) } defer f.Close() csvWriter = bufio.NewWriter(f) defer csvWriter.Flush() } isFirstFile := true if *multiple { // Process multiple history files filepath.Walk(*input, func(filePath string, f os.FileInfo, err error) error { if filePath == *input { return nil } fmt.Println("Processing ", filePath, "...") result := processFile(filePath, csvWriter, isFirstFile) fmt.Println(result) isFirstFile = false return nil }) } else { result := processFile(*input, csvWriter, isFirstFile) fmt.Println(result) } }
// sendResponse send resp to client, sendResponse must be goroutine safe. func (server *Server) writeTCPResponse(wr *bufio.Writer, proto *Proto) (err error) { if Conf.Debug { log.Debug("write proto: %v", proto) } if err = ioutil.WriteBigEndianInt32(wr, int32(rawHeaderLen)+int32(len(proto.Body))); err != nil { return } if err = ioutil.WriteBigEndianInt16(wr, rawHeaderLen); err != nil { return } if err = ioutil.WriteBigEndianInt16(wr, proto.Ver); err != nil { return } if err = ioutil.WriteBigEndianInt32(wr, proto.Operation); err != nil { return } if err = ioutil.WriteBigEndianInt32(wr, proto.SeqId); err != nil { return } if proto.Body != nil { if _, err = wr.Write(proto.Body); err != nil { return } } if err = wr.Flush(); err != nil { log.Error("tcp wr.Flush() error(%v)", err) } proto.Reset() return }
// WriteToJSON translates MessagePack from 'r' and writes it as // JSON to 'w' until the underlying reader returns io.EOF. It returns // the number of bytes written, and an error if it stopped before EOF. func (r *Reader) WriteToJSON(w io.Writer) (n int64, err error) { var j jsWriter var bf *bufio.Writer if jsw, ok := w.(jsWriter); ok { j = jsw } else { bf = bufio.NewWriter(w) j = bf } var nn int for err == nil { nn, err = rwNext(j, r) n += int64(nn) } if err != io.EOF { if bf != nil { bf.Flush() } return } err = nil if bf != nil { err = bf.Flush() } return }