Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
neo
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
2
Merge Requests
2
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Jobs
Commits
Open sidebar
Kirill Smelkov
neo
Commits
8e3625f5
Commit
8e3625f5
authored
May 15, 2018
by
Kirill Smelkov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
.
parent
db33a386
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
89 additions
and
6 deletions
+89
-6
go/xcommon/xnet/lonet/lonet.go
go/xcommon/xnet/lonet/lonet.go
+89
-6
No files found.
go/xcommon/xnet/lonet/lonet.go
View file @
8e3625f5
...
...
@@ -126,8 +126,8 @@ const NetPrefix = "lonet" // lonet package creates only "lonet*" networks
var
(
// errNetClosed = stderrors.New("network connection closed")
//
errAddrAlreadyUsed = stderrors.New("address already in use")
//
errAddrNoListen = stderrors.New("cannot listen on requested address")
errAddrAlreadyUsed
=
stderrors
.
New
(
"address already in use"
)
errAddrNoListen
=
stderrors
.
New
(
"cannot listen on requested address"
)
errConnRefused
=
stderrors
.
New
(
"connection refused"
)
)
...
...
@@ -306,9 +306,71 @@ func (n *SubNetwork) NewHost(ctx context.Context, name string) (*Host, error) {
// XXX Host.resolveAddr
// XXX
func
(
h
*
Host
)
Listen
(
laddr
string
)
(
net
.
Listener
,
error
)
{
panic
(
"TODO"
)
// Listen starts new listener on the host.
//
// It either allocates free port if laddr is "" or with 0 port, or binds to laddr.
// Once listener is started, Dials could connect to listening address.
// Connection requests created by Dials could be accepted via Accept.
func
(
h
*
Host
)
Listen
(
laddr
string
)
(
_
net
.
Listener
,
err
error
)
{
h
.
subnet
.
mu
.
Lock
()
defer
h
.
subnet
.
mu
.
Unlock
()
var
sk
*
socket
if
laddr
==
""
{
laddr
=
":0"
}
var
netladdr
net
.
Addr
defer
func
()
{
if
err
!=
nil
{
err
=
&
net
.
OpError
{
Op
:
"listen"
,
Net
:
h
.
Network
(),
Addr
:
netladdr
,
Err
:
err
}
}
}()
// XXX cannot use full resolvaAddr here - it talks to registry and is blocking
a
,
err
:=
h
.
subnet
.
parseAddr
(
laddr
)
return
nil
,
err
// local host if host name omitted
if
a
.
Host
==
""
{
a
.
Host
=
h
.
name
}
netladdr
=
a
if
a
.
Host
!=
h
.
name
{
return
nil
,
errAddrNoListen
}
// find first free port if autobind requested
if
a
.
Port
==
0
{
sk
=
h
.
allocFreeSocket
()
// else allocate socket in-place
}
else
{
// grow if needed
for
a
.
Port
>=
len
(
h
.
socketv
)
{
h
.
socketv
=
append
(
h
.
socketv
,
nil
)
}
if
h
.
socketv
[
a
.
Port
]
!=
nil
{
return
nil
,
errAddrAlreadyUsed
}
sk
=
&
socket
{
host
:
h
,
port
:
a
.
Port
}
h
.
socketv
[
a
.
Port
]
=
sk
}
// create listener under socket
l
:=
&
listener
{
socket
:
sk
,
dialq
:
make
(
chan
dialReq
),
down
:
make
(
chan
struct
{}),
}
sk
.
listener
=
l
return
l
,
nil
}
// serve serves incomming OS-level connection to this subnetwork.
...
...
@@ -502,7 +564,28 @@ func (c *conn) RemoteAddr() net.Addr {
// ----------------------------------------
// XXX Host.allocFreeSocket
// allocFreeSocket finds first free port and allocates socket entry for it.
//
// must be called with SubNetwork.mu held
func
(
h
*
Host
)
allocFreeSocket
()
*
socket
{
// find first free port
port
:=
1
// never allocate port 0 - it is used for autobind on listen only
for
;
port
<
len
(
h
.
socketv
);
port
++
{
if
h
.
socketv
[
port
]
==
nil
{
break
}
}
// if all busy it exits with port >= len(h.socketv)
// grow if needed
for
port
>=
len
(
h
.
socketv
)
{
h
.
socketv
=
append
(
h
.
socketv
,
nil
)
}
sk
:=
&
socket
{
host
:
h
,
port
:
port
}
h
.
socketv
[
port
]
=
sk
return
sk
}
// empty checks whether socket's both conn and listener are all nil.
// XXX recheck we really need this.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment