-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
115 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Server 建立 TCB,開啟監聽連線,進入狀態 LISTENING | ||
Client 發出連線要求 SYN,進入狀態 SYN-SENT,等待回應 | ||
Server 收到 SYN 要求,回應連線傳送 SYN+ACK,並進入狀態 SYN-RCVD (SYN-RECEIVED) | ||
Client 收到 SYN+ACK 確認完成連線進入狀態 ESTABLISHED,並送出 ACK | ||
Server 收到 ACK 確認連線完成,也進入狀態 ESTABLISHED | ||
雙方開始傳送交換資料 | ||
|
||
Client 準備關閉連線,發出 FIN,進入狀態 FIN-WAIT-1 | ||
Server 收到 FIN,發回收到的 ACK,進入狀態 CLOSE-WAIT,並通知 App 準備斷線 | ||
Client 收到 ACK,進入狀態 FIN-WAIT-2,等待 server 發出 FIN | ||
Server 確認 App 處理完斷線請求,發出 FIN,並進入狀態 LAST-ACK | ||
Client 收到 FIN,並回傳確認的 ACK,進入狀態 TIME-WAIT,等待時間過後正式關閉連線 | ||
Server 收到 ACK,便直接關閉連線 |
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,82 @@ | ||
package etch | ||
|
||
import ( | ||
"io" | ||
"log" | ||
"net" | ||
|
||
"github.com/mohanson/daze/lib/doa" | ||
) | ||
|
||
// Conf is acting as package level configuration. | ||
var Conf = struct { | ||
PayloadSize int | ||
}{ | ||
PayloadSize: 508, | ||
} | ||
|
||
// Server implemented the etch protocol. | ||
type Server struct { | ||
Closer io.Closer | ||
Listen string | ||
} | ||
|
||
// Close listener. | ||
func (s *Server) Close() error { | ||
if s.Closer != nil { | ||
return s.Closer.Close() | ||
} | ||
return nil | ||
} | ||
|
||
// Run it. | ||
func (s *Server) Run() error { | ||
addr, err := net.ResolveUDPAddr("udp", s.Listen) | ||
if err != nil { | ||
return err | ||
} | ||
conn, err := net.ListenUDP("udp", addr) | ||
if err != nil { | ||
return err | ||
} | ||
s.Closer = conn | ||
log.Println("main: listen and serve on", s.Listen) | ||
|
||
go func() { | ||
buf := make([]byte, Conf.PayloadSize) | ||
for { | ||
n, addr, err := conn.ReadFromUDP(buf) | ||
if err != nil { | ||
log.Println("main:", err) | ||
break | ||
} | ||
log.Println(addr, string(buf[:n])) | ||
} | ||
}() | ||
|
||
return nil | ||
} | ||
|
||
func NewServer(listen string) *Server { | ||
return &Server{ | ||
Listen: listen, | ||
} | ||
} | ||
|
||
type Client struct { | ||
Server string | ||
} | ||
|
||
func (s *Client) Run() { | ||
addr := doa.Try(net.ResolveUDPAddr("udp", s.Server)) | ||
conn := doa.Try(net.DialUDP("udp", nil, addr)) | ||
for range 32 { | ||
doa.Try(conn.Write([]byte("Hello World!"))) | ||
} | ||
} | ||
|
||
func NewClient(server string) *Client { | ||
return &Client{ | ||
Server: server, | ||
} | ||
} |
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,20 @@ | ||
package etch | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
const ( | ||
DazeServerListenOn = "127.0.0.1:28081" | ||
) | ||
|
||
func TestProtocolEtch(t *testing.T) { | ||
server := NewServer(DazeServerListenOn) | ||
go server.Run() | ||
time.Sleep(time.Second) | ||
|
||
client := NewClient(DazeServerListenOn) | ||
client.Run() | ||
time.Sleep(time.Second) | ||
} |