-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from lesismal/dev
Dev
- Loading branch information
Showing
10 changed files
with
1,249 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package main | ||
|
||
import ( | ||
"crypto/rand" | ||
"encoding/base64" | ||
"fmt" | ||
"io" | ||
"log" | ||
"net" | ||
"sync" | ||
"time" | ||
|
||
"github.com/lesismal/arpc" | ||
) | ||
|
||
var ( | ||
addr = "localhost:8888" | ||
|
||
method = "Hello" | ||
) | ||
|
||
// HelloReq . | ||
type HelloReq struct { | ||
Msg string | ||
} | ||
|
||
// HelloRsp . | ||
type HelloRsp struct { | ||
Msg string | ||
} | ||
|
||
func dialer() (net.Conn, error) { | ||
return net.DialTimeout("tcp", addr, time.Second*3) | ||
} | ||
|
||
func main() { | ||
arpc.EnablePool(true) | ||
|
||
client, err := arpc.NewClient(dialer) | ||
if err != nil { | ||
log.Println("NewClient failed:", err) | ||
return | ||
} | ||
defer client.Stop() | ||
|
||
wg := &sync.WaitGroup{} | ||
wg.Add(1) | ||
client.Handler.HandleStream("/stream_server_to_client", func(stream *arpc.Stream) { | ||
defer wg.Done() | ||
defer stream.CloseRecv() | ||
for { | ||
str := "" | ||
err := stream.Recv(&str) | ||
if err == io.EOF { | ||
stream.CloseSend() | ||
log.Printf("[client] [stream id: %v] stream_server_to_client closed", stream.Id()) | ||
break | ||
} | ||
if err != nil { | ||
panic(err) | ||
} | ||
log.Printf("[client] [stream id: %v] stream_server_to_client: %v", stream.Id(), str) | ||
err = stream.Send(&str) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
}) | ||
|
||
data := make([]byte, 10) | ||
rand.Read(data) | ||
req := &HelloReq{Msg: base64.RawStdEncoding.EncodeToString(data)} | ||
rsp := &HelloRsp{} | ||
err = client.Call(method, req, rsp, time.Second*5) | ||
if err != nil { | ||
log.Printf("Call failed: %v", err) | ||
} else if rsp.Msg != req.Msg { | ||
log.Fatal("Call failed: not equal") | ||
} | ||
|
||
wg.Wait() | ||
time.Sleep(time.Second) | ||
|
||
stream := client.NewStream("/stream_client_to_server") | ||
defer stream.CloseRecv() | ||
go func() { | ||
for i := 0; i < 3; i++ { | ||
err := stream.Send(fmt.Sprintf("stream data %v", i)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
err = stream.SendAndClose(fmt.Sprintf("stream data %v", 3)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
}() | ||
|
||
for { | ||
str := "" | ||
err = stream.Recv(&str) | ||
if err == io.EOF { | ||
log.Printf("[client] [stream id: %v] stream_client_to_server closed", stream.Id()) | ||
break | ||
} | ||
if err != nil { | ||
panic(err) | ||
} | ||
log.Printf("[client] [stream id: %v] stream_client_to_server: %v", stream.Id(), str) | ||
} | ||
} |
Oops, something went wrong.