Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
adam.huang
go-libp2p
Commits
713d9a87
Commit
713d9a87
authored
Sep 03, 2016
by
Jeromy
Browse files
examples: example using swarm and raw tcp (no muxing)
parent
978b186d
Changes
2
Hide whitespace changes
Inline
Side-by-side
examples/justtcp/README.md
0 → 100644
View file @
713d9a87
# libp2p 'just tcp' example
## What this does
This example starts up a libp2p swarm that listens for tcp connections behind a
multistream muxer protocol of
`/plaintext/1.0.0`
. All connections made to it
will be echoed back.
## Building
```
$ go build
```
## Usage
```
$ ./justtcp
```
examples/justtcp/main.go
0 → 100644
View file @
713d9a87
package
main
import
(
"context"
"fmt"
"net"
"os"
transport
"github.com/ipfs/go-libp2p-transport"
ma
"github.com/jbenet/go-multiaddr"
smux
"github.com/jbenet/go-stream-muxer"
"github.com/libp2p/go-libp2p/p2p/net/swarm"
)
func
fatal
(
i
interface
{})
{
fmt
.
Println
(
i
)
os
.
Exit
(
1
)
}
type
NullMux
struct
{}
type
NullMuxConn
struct
{
net
.
Conn
}
func
(
c
*
NullMuxConn
)
AcceptStream
()
(
smux
.
Stream
,
error
)
{
panic
(
"We don't do this"
)
}
func
(
c
*
NullMuxConn
)
IsClosed
()
bool
{
return
false
}
func
(
c
*
NullMuxConn
)
OpenStream
()
(
smux
.
Stream
,
error
)
{
panic
(
"if only you could see how disappointed i am in you right now"
)
}
func
(
c
*
NullMuxConn
)
Serve
(
_
smux
.
StreamHandler
)
{
}
func
(
nm
NullMux
)
NewConn
(
c
net
.
Conn
,
server
bool
)
(
smux
.
Conn
,
error
)
{
return
&
NullMuxConn
{
c
},
nil
}
var
_
smux
.
Transport
=
(
*
NullMux
)(
nil
)
func
main
()
{
laddr
,
err
:=
ma
.
NewMultiaddr
(
"/ip4/0.0.0.0/tcp/5555"
)
if
err
!=
nil
{
fatal
(
err
)
}
swarm
.
PSTransport
=
new
(
NullMux
)
s
:=
swarm
.
NewBlankSwarm
(
context
.
Background
(),
"bob"
,
nil
)
s
.
AddTransport
(
transport
.
NewTCPTransport
())
err
=
s
.
AddListenAddr
(
laddr
)
if
err
!=
nil
{
fatal
(
err
)
}
s
.
SetConnHandler
(
func
(
c
*
swarm
.
Conn
)
{
fmt
.
Println
(
"CALLED OUR CONN HANDLER!"
)
defer
c
.
Close
()
buf
:=
make
([]
byte
,
1024
)
for
{
n
,
err
:=
c
.
RawConn
()
.
Read
(
buf
)
if
err
!=
nil
{
fmt
.
Println
(
err
)
return
}
fmt
.
Printf
(
"read: %q
\n
"
,
string
(
buf
[
:
n
]))
_
,
err
=
c
.
RawConn
()
.
Write
(
buf
[
:
n
])
if
err
!=
nil
{
fmt
.
Println
(
err
)
return
}
}
})
<-
make
(
chan
bool
)
}
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