aceld
6 years ago
3 changed files with 185 additions and 0 deletions
@ -0,0 +1,11 @@ |
|||
package ziface |
|||
|
|||
//定义服务器接口
|
|||
type IServer interface{ |
|||
//启动服务器方法
|
|||
Start() |
|||
//停止服务器方法
|
|||
Stop() |
|||
//开启业务服务方法
|
|||
Serve() |
|||
} |
@ -0,0 +1,113 @@ |
|||
package znet |
|||
|
|||
import ( |
|||
"fmt" |
|||
"net" |
|||
"time" |
|||
"zinx/ziface" |
|||
) |
|||
|
|||
//iServer 接口实现,定义一个Server服务类
|
|||
type Server struct { |
|||
//服务器的名称
|
|||
Name string |
|||
//tcp4 or other
|
|||
IPVersion string |
|||
//服务绑定的IP地址
|
|||
IP string |
|||
//服务绑定的端口
|
|||
Port int |
|||
} |
|||
|
|||
|
|||
//============== 实现 ziface.IServer 里的全部接口方法 ========
|
|||
|
|||
//开启网络服务
|
|||
func (s *Server) Start() { |
|||
fmt.Printf("[START] Server listenner at IP: %s, Port %d, is starting\n", s.IP, s.Port) |
|||
|
|||
//开启一个go去做服务端Linster业务
|
|||
go func() { |
|||
//1 获取一个TCP的Addr
|
|||
addr, err := net.ResolveTCPAddr(s.IPVersion, fmt.Sprintf("%s:%d", s.IP, s.Port)) |
|||
if err != nil { |
|||
fmt.Println("resolve tcp addr err: ", err) |
|||
return |
|||
} |
|||
|
|||
//2 监听服务器地址
|
|||
listenner, err:= net.ListenTCP(s.IPVersion, addr) |
|||
if err != nil { |
|||
fmt.Println("listen", s.IPVersion, "err", err) |
|||
return |
|||
} |
|||
|
|||
//已经监听成功
|
|||
fmt.Println("start Zinx server ", s.Name, " succ, now listenning...") |
|||
|
|||
//3 启动server网络连接业务
|
|||
for { |
|||
//3.1 阻塞等待客户端建立连接请求
|
|||
conn, err := listenner.AcceptTCP() |
|||
if err != nil { |
|||
fmt.Println("Accept err ", err) |
|||
continue |
|||
} |
|||
|
|||
//3.2 TODO Server.Start() 设置服务器最大连接控制,如果超过最大连接,那么则关闭此新的连接
|
|||
|
|||
//3.3 TODO Server.Start() 处理该新连接请求的 业务 方法, 此时应该有 handler 和 conn是绑定的
|
|||
|
|||
//我们这里暂时做一个最大512字节的回显服务
|
|||
go func () { |
|||
//不断的循环从客户端获取数据
|
|||
for { |
|||
buf := make([]byte, 512) |
|||
cnt, err := conn.Read(buf) |
|||
if err != nil { |
|||
fmt.Println("recv buf err ", err) |
|||
continue |
|||
} |
|||
//回显
|
|||
if _, err := conn.Write(buf[:cnt]); err !=nil { |
|||
fmt.Println("write back buf err ", err) |
|||
continue |
|||
} |
|||
} |
|||
}() |
|||
} |
|||
}() |
|||
} |
|||
|
|||
func (s *Server) Stop() { |
|||
fmt.Println("[STOP] Zinx server , name " , s.Name) |
|||
|
|||
//TODO Server.Stop() 将其他需要清理的连接信息或者其他信息 也要一并停止或者清理
|
|||
} |
|||
|
|||
func (s *Server) Serve() { |
|||
s.Start() |
|||
|
|||
//TODO Server.Serve() 是否在启动服务的时候 还要处理其他的事情呢 可以在这里添加
|
|||
|
|||
|
|||
//阻塞,否则主Go退出, listenner的go将会退出
|
|||
for { |
|||
time.Sleep(10*time.Second) |
|||
} |
|||
} |
|||
|
|||
|
|||
/* |
|||
创建一个服务器句柄 |
|||
*/ |
|||
func NewServer (name string) ziface.IServer { |
|||
s:= &Server { |
|||
Name :name, |
|||
IPVersion:"tcp4", |
|||
IP:"0.0.0.0", |
|||
Port:7777, |
|||
} |
|||
|
|||
return s |
|||
} |
@ -0,0 +1,61 @@ |
|||
package znet |
|||
|
|||
import ( |
|||
"fmt" |
|||
"net" |
|||
"testing" |
|||
"time" |
|||
) |
|||
|
|||
/* |
|||
模拟客户端 |
|||
*/ |
|||
func ClientTest() { |
|||
|
|||
fmt.Println("Client Test ... start") |
|||
//3秒之后发起测试请求,给服务端开启服务的机会
|
|||
time.Sleep(3 * time.Second) |
|||
|
|||
conn,err := net.Dial("tcp", "127.0.0.1:7777") |
|||
if err != nil { |
|||
fmt.Println("client start err, exit!") |
|||
return |
|||
} |
|||
|
|||
for { |
|||
_, err := conn.Write([]byte("hahaha")) |
|||
if err !=nil { |
|||
fmt.Println("write error err ", err) |
|||
return |
|||
} |
|||
|
|||
buf :=make([]byte, 512) |
|||
cnt, err := conn.Read(buf) |
|||
if err != nil { |
|||
fmt.Println("read buf error ") |
|||
return |
|||
} |
|||
|
|||
fmt.Printf(" server call back : %s, cnt = %d\n", buf, cnt) |
|||
|
|||
time.Sleep(1*time.Second) |
|||
} |
|||
} |
|||
|
|||
//Server 模块的测试函数
|
|||
func TestServer(t *testing.T) { |
|||
|
|||
/* |
|||
服务端测试 |
|||
*/ |
|||
//1 创建一个server 句柄 s
|
|||
s := NewServer("[zinx V0.1]") |
|||
|
|||
/* |
|||
客户端测试 |
|||
*/ |
|||
go ClientTest() |
|||
|
|||
//2 开启服务
|
|||
s.Serve() |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue