OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [net/] [textproto/] [textproto.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2010 The Go Authors.  All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4
 
5
// Package textproto implements generic support for text-based request/response
6
// protocols in the style of HTTP, NNTP, and SMTP.
7
//
8
// The package provides:
9
//
10
// Error, which represents a numeric error response from
11
// a server.
12
//
13
// Pipeline, to manage pipelined requests and responses
14
// in a client.
15
//
16
// Reader, to read numeric response code lines,
17
// key: value headers, lines wrapped with leading spaces
18
// on continuation lines, and whole text blocks ending
19
// with a dot on a line by itself.
20
//
21
// Writer, to write dot-encoded text blocks.
22
//
23
package textproto
24
 
25
import (
26
        "bufio"
27
        "fmt"
28
        "io"
29
        "net"
30
)
31
 
32
// An Error represents a numeric error response from a server.
33
type Error struct {
34
        Code int
35
        Msg  string
36
}
37
 
38
func (e *Error) Error() string {
39
        return fmt.Sprintf("%03d %s", e.Code, e.Msg)
40
}
41
 
42
// A ProtocolError describes a protocol violation such
43
// as an invalid response or a hung-up connection.
44
type ProtocolError string
45
 
46
func (p ProtocolError) Error() string {
47
        return string(p)
48
}
49
 
50
// A Conn represents a textual network protocol connection.
51
// It consists of a Reader and Writer to manage I/O
52
// and a Pipeline to sequence concurrent requests on the connection.
53
// These embedded types carry methods with them;
54
// see the documentation of those types for details.
55
type Conn struct {
56
        Reader
57
        Writer
58
        Pipeline
59
        conn io.ReadWriteCloser
60
}
61
 
62
// NewConn returns a new Conn using conn for I/O.
63
func NewConn(conn io.ReadWriteCloser) *Conn {
64
        return &Conn{
65
                Reader: Reader{R: bufio.NewReader(conn)},
66
                Writer: Writer{W: bufio.NewWriter(conn)},
67
                conn:   conn,
68
        }
69
}
70
 
71
// Close closes the connection.
72
func (c *Conn) Close() error {
73
        return c.conn.Close()
74
}
75
 
76
// Dial connects to the given address on the given network using net.Dial
77
// and then returns a new Conn for the connection.
78
func Dial(network, addr string) (*Conn, error) {
79
        c, err := net.Dial(network, addr)
80
        if err != nil {
81
                return nil, err
82
        }
83
        return NewConn(c), nil
84
}
85
 
86
// Cmd is a convenience method that sends a command after
87
// waiting its turn in the pipeline.  The command text is the
88
// result of formatting format with args and appending \r\n.
89
// Cmd returns the id of the command, for use with StartResponse and EndResponse.
90
//
91
// For example, a client might run a HELP command that returns a dot-body
92
// by using:
93
//
94
//      id, err := c.Cmd("HELP")
95
//      if err != nil {
96
//              return nil, err
97
//      }
98
//
99
//      c.StartResponse(id)
100
//      defer c.EndResponse(id)
101
//
102
//      if _, _, err = c.ReadCodeLine(110); err != nil {
103
//              return nil, err
104
//      }
105
//      text, err := c.ReadDotAll()
106
//      if err != nil {
107
//              return nil, err
108
//      }
109
//      return c.ReadCodeLine(250)
110
//
111
func (c *Conn) Cmd(format string, args ...interface{}) (id uint, err error) {
112
        id = c.Next()
113
        c.StartRequest(id)
114
        err = c.PrintfLine(format, args...)
115
        c.EndRequest(id)
116
        if err != nil {
117
                return 0, err
118
        }
119
        return id, nil
120
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.