加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
stream_service.go 3.13 KB
一键复制 编辑 原始数据 按行查看 历史
Hprose 提交于 2015-07-03 15:36 . Refactoring StreamService.
/**********************************************************\
| |
| hprose |
| |
| Official WebSite: http://www.hprose.com/ |
| http://www.hprose.org/ |
| |
\**********************************************************/
/**********************************************************\
* *
* hprose/stream_service.go *
* *
* hprose stream service for Go. *
* *
* LastModified: May 25, 2015 *
* Authors: Ma Bingyao <andot@hprose.com> *
* Ore_Ash <nanohugh@gmail.com> *
* *
\**********************************************************/
package hprose
import (
"net"
"time"
)
// StreamService is the base service for TcpService and UnixService
type StreamService struct {
*BaseService
timeout interface{}
readTimeout interface{}
readBuffer interface{}
writeTimeout interface{}
writeBuffer interface{}
}
// StreamContext is the hprose stream context for service
type StreamContext struct {
*BaseContext
net.Conn
}
func newStreamService() (service *StreamService) {
service = new(StreamService)
service.BaseService = NewBaseService()
return
}
// SetTimeout for stream service
func (service *StreamService) SetTimeout(d time.Duration) {
service.timeout = d
}
// SetReadTimeout for stream service
func (service *StreamService) SetReadTimeout(d time.Duration) {
service.readTimeout = d
}
// SetReadBuffer for stream service
func (service *StreamService) SetReadBuffer(bytes int) {
service.readBuffer = bytes
}
// SetWriteTimeout for stream service
func (service *StreamService) SetWriteTimeout(d time.Duration) {
service.writeTimeout = d
}
// SetWriteBuffer for stream service
func (service *StreamService) SetWriteBuffer(bytes int) {
service.writeBuffer = bytes
}
func (service *StreamService) serve(conn net.Conn) {
var data []byte
var err error
for {
if service.readTimeout != nil {
err = conn.SetReadDeadline(time.Now().Add(service.readTimeout.(time.Duration)))
}
if err == nil {
data, err = receiveDataOverStream(conn)
}
if err == nil {
data = service.Handle(data, &StreamContext{BaseContext: NewBaseContext(), Conn: conn})
if service.writeTimeout != nil {
err = conn.SetWriteDeadline(time.Now().Add(service.writeTimeout.(time.Duration)))
}
if err == nil {
err = sendDataOverStream(conn, data)
}
}
if err != nil {
conn.Close()
break
}
}
}
// Serve ...
func (service *StreamService) Serve(conn net.Conn) (err error) {
if service.timeout != nil {
if err = conn.SetDeadline(time.Now().Add(service.timeout.(time.Duration))); err != nil {
return err
}
}
go service.serve(conn)
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化