• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

server.go源码阅读

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

package pingo

import (
    "bufio"
    "bytes"
    "flag"
    "fmt"
    "io"
    "math/rand"
    "net"
    "net/rpc"
    "os"
    "path"
    "path/filepath"
    "reflect"
    "strings"
    "time"
)

// Register a new object this plugin exports. The object must be
// an exported symbol and obey all rules an object in the standard
// "rpc" module has to obey.
//注册的一个对象作为可以导出对象。这个对象必须符合RPC规则
//               - exported method of exported type
//    - two arguments, both of exported type
//    - the second argument is a pointer
//    - one return value, of type error
// Register will panic if called after Run.
//如果在运行中  注册对象 就会报错 
func Register(obj interface{}) {
    if defaultServer.running {
        panic("Do not call Register after Run")
    }
    defaultServer.register(obj)//注册可导出的对象
}

// Run will start all the necessary steps to make the plugin available.
//调用Run函数是必须的来保证插件的可用性
func Run() error {
    if !flag.Parsed() {//判断参数是否解析
        flag.Parse()//解析当前参数
    }
    return defaultServer.run()
}

// Internal object for plugin control
type PingoRpc struct{}

// Default constructor for interal object. Do not call manually.
func NewPingoRpc() *PingoRpc {
    return &PingoRpc{}
}

// Internal RPC call to shut down a plugin. Do not call manually.
func (s *PingoRpc) Exit(status int, unused *int) error {
    os.Exit(status)
    return nil
}

type config struct {
    proto   string
    addr    string
    prefix  string
    unixdir string
}

func makeConfig() *config {
    c := &config{}
    flag.StringVar(&c.proto, "pingo:proto", "unix", "Protocol to use: unix or tcp")
    flag.StringVar(&c.unixdir, "pingo:unixdir", "", "Alternative directory for unix socket")
    flag.StringVar(&c.prefix, "pingo:prefix", "pingo", "Prefix to output lines")
    return c
}

type rpcServer struct {
    *rpc.Server
    secret  string
    objs    []string
    conf    *config
    running bool
}

func newRpcServer() *rpcServer {
    rand.Seed(time.Now().UTC().UnixNano())
    r := &rpcServer{
        Server: rpc.NewServer(),
        secret: randstr(64),
        objs:   make([]string, 0),
        conf:   makeConfig(), // conf remains fixed after this point
    }
    r.register(&PingoRpc{})
    return r
}

var defaultServer = newRpcServer()

type bufReadWriteCloser struct {
    *bufio.Reader
    r io.ReadWriteCloser
}

func newBufReadWriteCloser(r io.ReadWriteCloser) *bufReadWriteCloser {
    return &bufReadWriteCloser{Reader: bufio.NewReader(r), r: r}
}

func (b *bufReadWriteCloser) Write(data []byte) (int, error) {
    return b.r.Write(data)
}

func (b *bufReadWriteCloser) Close() error {
    return b.r.Close()
}

func readHeaders(brwc *bufReadWriteCloser) ([]byte, error) {
    var buf bytes.Buffer
    var headerEnd bool

    for {
        b, err := brwc.ReadByte()
        if err != nil {
            return []byte(""), err
        }

        buf.WriteByte(b)

        if b == '\n' {
            if headerEnd {
                break
            }
            headerEnd = true
        } else {
            headerEnd = false
        }
    }

    return buf.Bytes(), nil
}

func parseHeaders(brwc *bufReadWriteCloser, m map[string]string) error {
    headers, err := readHeaders(brwc)
    if err != nil {
        return err
    }

    r := bytes.NewReader(headers)
    scanner := bufio.NewScanner(r)

    for scanner.Scan() {
        parts := strings.SplitN(scanner.Text(), ": ", 2)
        if parts[0] == "" {
            continue
        }
        m[parts[0]] = parts[1]
    }

    return nil
}

func (r *rpcServer) authConn(token string) bool {
    if token != "" && token == r.secret {
        return true
    }
    return false
}

func (r *rpcServer) serveConn(conn io.ReadWriteCloser, h meta) {
    bconn := newBufReadWriteCloser(conn)
    defer bconn.Close()

    headers := make(map[string]string)
    if err := parseHeaders(bconn, headers); err != nil {
        h.output("error", err.Error())
        return
    }

    if r.authConn(headers["Auth-Token"]) 
                       
                    
                    

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
go map fatal error: concurrent map iteration and map write 读写锁与深度拷贝的坑 ...发布时间:2022-07-10
下一篇:
goruntime发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap