Commit f52053c4 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent b370f152
...@@ -34,12 +34,14 @@ import ( ...@@ -34,12 +34,14 @@ import (
const NetPrefix = "pipe" // pipenet package works only with "pipe*" networks const NetPrefix = "pipe" // pipenet package works only with "pipe*" networks
var errBadNetwork = errors.New("pipenet: invalid network") var (
var errBadAddress = errors.New("pipenet: invalid address") errBadNetwork = errors.New("pipenet: invalid network")
var errNetNotFound = errors.New("no such network") errBadAddress = errors.New("pipenet: invalid address")
var errNetClosed = errors.New("network connection closed") errNetNotFound = errors.New("no such network")
var errAddrAlreadyUsed = errors.New("address already in use") errNetClosed = errors.New("network connection closed")
var errConnRefused = errors.New("connection refused") errAddrAlreadyUsed = errors.New("address already in use")
errConnRefused = errors.New("connection refused")
)
// Network represents network of in-memory pipes // Network represents network of in-memory pipes
...@@ -47,7 +49,7 @@ var errConnRefused = errors.New("connection refused") ...@@ -47,7 +49,7 @@ var errConnRefused = errors.New("connection refused")
// //
// Network must be created with New // Network must be created with New
type Network struct { type Network struct {
// name (suffix) of this network, e.g. "" // name of this network under "pipe" namespace -> e.g. ""
// full network name will be reported as "pipe"+Name // full network name will be reported as "pipe"+Name
Name string Name string
...@@ -62,7 +64,7 @@ type pipe struct { ...@@ -62,7 +64,7 @@ type pipe struct {
listener *listener // listener is waiting here if != nil listener *listener // listener is waiting here if != nil
} }
// Addr represents address of a pipe endpoint // Addr represents address of a pipenet endpoint
type Addr struct { type Addr struct {
network string // full network name, e.g. "pipe" network string // full network name, e.g. "pipe"
addr string // XXX -> port ? + including c/s ? addr string // XXX -> port ? + including c/s ?
...@@ -117,11 +119,12 @@ func (n *Network) Listen(laddr string) (net.Listener, error) { ...@@ -117,11 +119,12 @@ func (n *Network) Listen(laddr string) (net.Listener, error) {
l := &listener{ l := &listener{
network: n, network: n,
port: port, port: port,
dialq: make(chan chan connected), dialq: make(chan chan net.Conn),
acceptq: make(chan chan connected), // acceptq: make(chan chan connected),
down: make(chan struct{}), down: make(chan struct{}),
} }
n.pipev[port] = &pipe{listener: l} n.pipev[port] = &pipe{listener: l}
// go l.listen()
return l, nil return l, nil
} }
...@@ -131,8 +134,8 @@ type listener struct { ...@@ -131,8 +134,8 @@ type listener struct {
network *Network // XXX needed ? network *Network // XXX needed ?
port int // port we are listening on XXX needed ? port int // port we are listening on XXX needed ?
dialq chan chan connected // Dial requests to our port go here dialq chan chan net.Conn // Dial requests to our port go here
acceptq chan chan connected // Accept requests go here // acceptq chan chan connected // Accept requests go here
down chan struct{} // Close -> down=ready down chan struct{} // Close -> down=ready
downOnce sync.Once // so Close several times is ok downOnce sync.Once // so Close several times is ok
} }
...@@ -150,21 +153,47 @@ func (l *listener) Close() error { ...@@ -150,21 +153,47 @@ func (l *listener) Close() error {
return nil return nil
} }
/*
// listen implements listener service process - connecting Accept and Dial calls with each other
func (l *listener) listen() {
for {
select {
case <-l.down:
return
case resp := <-l.dialq:
// TODO
case resp := <-l.acceptq:
// TODO
}
}
}
// connected is response from listener to Dial and Accept // connected is response from listener to Dial and Accept
type connected struct { type connected struct {
conn net.Conn conn net.Conn
err error err error
} }
*/
func (l *listener) Accept() (net.Conn, error) { func (l *listener) Accept() (net.Conn, error) {
ch := make(chan connected) // ch := make(chan connected)
select { select {
case <-l.down: case <-l.down:
return nil, &net.OpError{Op: "accept", Net: l.network.netname(), Addr: l.Addr(), Err: errNetClosed} return nil, &net.OpError{Op: "accept", Net: l.network.netname(), Addr: l.Addr(), Err: errNetClosed}
/*
case l.acceptq <- ch: case l.acceptq <- ch:
resp := <-ch resp := <-ch
return resp.conn, resp.err return resp.conn, resp.err
*/
case resp := <-l.dialq:
// someone dialed us - let's connect
pc, ps := net.Pipe()
resp <- pc
return ps, nil
} }
} }
...@@ -192,14 +221,13 @@ func (n *Network) Dial(addr string) (net.Conn, error) { ...@@ -192,14 +221,13 @@ func (n *Network) Dial(addr string) (net.Conn, error) {
l := p.listener l := p.listener
// NOTE listener is not locking n.mu -> it is ok to send/receive under mu // NOTE listener is not locking n.mu -> it is ok to send/receive under mu
ch := make(chan connected) resp := make(chan net.Conn)
select { select {
case <-l.down: case <-l.down:
return nil, derr(errConnRefused) return nil, derr(errConnRefused)
case l.dialq <- ch: case l.dialq <- resp:
resp := <-ch return <-resp, nil
return resp.conn, resp.err
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment