-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
去除了pipe包,移动到了dial包,整理了部分函数,端口转发测试通过,修复了pkg的bug(4090字节的问题,需要多次读取)
- Loading branch information
1 parent
9affced
commit ce1bdc8
Showing
18 changed files
with
176 additions
and
232 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package dial | ||
|
||
import ( | ||
"context" | ||
"github.com/injoyai/io" | ||
) | ||
|
||
/* | ||
Client | ||
抽象管道概念 | ||
例如 用485线通讯,正常的TCP连接 都属于管道 | ||
需要 客户端对客户端 客户端对服务端 2种方式 | ||
需要 一个管道通讯多个io数据,并且不能长期占用 写入前建议分包 | ||
只做数据加密(可选),不解析数据,不分包数据 | ||
提供io.Reader io.Writer接口 | ||
写入数据会封装一层(封装连接信息,动作,数据) | ||
*/ | ||
|
||
// RedialPipe 通道客户端 | ||
func RedialPipe(addr string, fn ...func(ctx context.Context, c *io.Client)) *io.Client { | ||
return RedialTCP(addr, func(ctx context.Context, c *io.Client) { | ||
c.SetReadWriteWithPkg() | ||
c.SetKeepAlive(io.DefaultTimeout) | ||
c.SetPrintFunc(func(msg io.Message, tag ...string) { | ||
io.PrintWithASCII(msg, append([]string{"PI|C"}, tag...)...) | ||
}) | ||
for _, v := range fn { | ||
v(ctx, c) | ||
} | ||
}) | ||
} | ||
|
||
// NewPipeServer 通道服务端 | ||
func NewPipeServer(port int, fn ...func(s *io.Server)) (*io.Server, error) { | ||
return NewTCPServer(port, func(s *io.Server) { | ||
s.SetReadWriteWithPkg() | ||
s.SetPrintFunc(func(msg io.Message, tag ...string) { | ||
io.PrintWithASCII(msg, append([]string{"PI|S"}, tag...)...) | ||
}) | ||
for _, v := range fn { | ||
v(s) | ||
} | ||
}) | ||
} | ||
|
||
// NewPipeTransmit 通过客户端数据转发,例如客户端1的数据会广播其他所有客户端 | ||
func NewPipeTransmit(port int, fn ...func(s *io.Server)) (*io.Server, error) { | ||
return NewPipeServer(port, func(s *io.Server) { | ||
s.SetPrintFunc(func(msg io.Message, tag ...string) { | ||
if len(tag) > 0 { | ||
switch tag[0] { | ||
case io.TagWrite, io.TagRead: | ||
default: | ||
io.PrintWithASCII(msg, append([]string{"PI|T"}, tag...)...) | ||
} | ||
} | ||
}) | ||
s.SetDealFunc(func(msg *io.IMessage) { | ||
//当另一端代理未开启时,无法转发数据 | ||
for _, v := range s.GetClientMap() { | ||
if v.GetKey() != msg.GetKey() { | ||
//队列执行,避免阻塞其他 | ||
v.WriteQueue(msg.Bytes()) | ||
} | ||
} | ||
}) | ||
for _, v := range fn { | ||
v(s) | ||
} | ||
}) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.