This repository has been archived by the owner on Aug 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
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
1 parent
7447db0
commit 0f187a0
Showing
8 changed files
with
98 additions
and
70 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
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 |
---|---|---|
@@ -1,13 +1,50 @@ | ||
package mux | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"io" | ||
) | ||
|
||
const ( | ||
MessageMethodData = iota | ||
MessageMethodDial | ||
) | ||
|
||
type MessageHead struct { | ||
//MessageHeadLength = 13 | ||
type Message struct { | ||
Method uint8 | ||
ConnID uint32 | ||
MessageID uint32 | ||
Length uint32 | ||
Data []byte | ||
|
||
r io.Reader | ||
buf []byte | ||
} | ||
|
||
func (m *Message) Read(p []byte) (n int, err error) { | ||
if m.r == nil { | ||
h := make([]byte, 13) | ||
h[0] = m.Method | ||
binary.BigEndian.PutUint32(h[1:5], m.ConnID) | ||
binary.BigEndian.PutUint32(h[5:9], m.MessageID) | ||
binary.BigEndian.PutUint32(h[9:13], m.Length) | ||
m.r = bytes.NewReader(append(h, m.Data...)) | ||
} | ||
|
||
n, err = m.Read(p) | ||
return len(p), nil | ||
} | ||
|
||
func (m *Message) Write(p []byte) (n int, err error) { | ||
m.buf = append(m.buf, p...) | ||
if len(m.buf) >= 13 { | ||
m.Method = m.buf[0] | ||
m.ConnID = binary.BigEndian.Uint32(m.buf[1:5]) | ||
m.MessageID = binary.BigEndian.Uint32(m.buf[5:9]) | ||
m.Length = binary.BigEndian.Uint32(m.buf[9:13]) | ||
m.Data = m.buf[13:] | ||
} | ||
return len(p), nil | ||
} |
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,38 @@ | ||
package mux | ||
|
||
import ( | ||
"log" | ||
"math/rand" | ||
"net" | ||
) | ||
|
||
//HandleMessage is a server group function | ||
func (group *Group) HandleMessage(m *Message) (err error) { | ||
//accept new conn | ||
if m.Method == MessageMethodDial { | ||
host := string(m.Data) | ||
conn := &Conn{ | ||
ID: rand.Uint32(), | ||
wait: make(chan int), | ||
sendMessageID: new(uint32), | ||
} | ||
|
||
tcpAddr, err := net.ResolveTCPAddr("tcp", host) | ||
if err != nil { | ||
log.Printf(err.Error()) | ||
return err | ||
} | ||
|
||
tcpConn, err := net.DialTCP("tcp", nil, tcpAddr) | ||
if err != nil { | ||
log.Printf(err.Error()) | ||
return err | ||
} | ||
|
||
log.Printf("Accepted mux conn %s", host) | ||
|
||
conn.Run(tcpConn) | ||
return err | ||
} | ||
return | ||
} |
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