2016-03-14 11:18:24 +08:00
|
|
|
// Copyright 2016 fatedier, fatedier@gmail.com
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-01-27 21:24:36 +08:00
|
|
|
package conn
|
|
|
|
|
|
|
|
import (
|
2016-02-03 18:46:24 +08:00
|
|
|
"bufio"
|
2016-01-27 21:24:36 +08:00
|
|
|
"fmt"
|
2016-02-03 18:46:24 +08:00
|
|
|
"io"
|
2016-01-27 21:24:36 +08:00
|
|
|
"net"
|
2016-06-06 11:43:41 +08:00
|
|
|
"strings"
|
2016-01-27 21:24:36 +08:00
|
|
|
"sync"
|
2016-04-18 15:16:40 +08:00
|
|
|
"time"
|
2016-01-27 21:24:36 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Listener struct {
|
2016-02-19 17:01:47 +08:00
|
|
|
addr net.Addr
|
|
|
|
l *net.TCPListener
|
2016-04-18 15:16:40 +08:00
|
|
|
accept chan *Conn
|
2016-02-19 17:01:47 +08:00
|
|
|
closeFlag bool
|
2016-01-27 21:24:36 +08:00
|
|
|
}
|
|
|
|
|
2016-02-19 17:01:47 +08:00
|
|
|
func Listen(bindAddr string, bindPort int64) (l *Listener, err error) {
|
|
|
|
tcpAddr, err := net.ResolveTCPAddr("tcp4", fmt.Sprintf("%s:%d", bindAddr, bindPort))
|
|
|
|
listener, err := net.ListenTCP("tcp", tcpAddr)
|
|
|
|
if err != nil {
|
|
|
|
return l, err
|
|
|
|
}
|
|
|
|
|
|
|
|
l = &Listener{
|
|
|
|
addr: listener.Addr(),
|
|
|
|
l: listener,
|
2016-04-18 15:16:40 +08:00
|
|
|
accept: make(chan *Conn),
|
2016-02-19 17:01:47 +08:00
|
|
|
closeFlag: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
conn, err := l.l.AcceptTCP()
|
|
|
|
if err != nil {
|
|
|
|
if l.closeFlag {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &Conn{
|
|
|
|
TcpConn: conn,
|
|
|
|
closeFlag: false,
|
|
|
|
}
|
|
|
|
c.Reader = bufio.NewReader(c.TcpConn)
|
2016-04-18 15:16:40 +08:00
|
|
|
l.accept <- c
|
2016-02-19 17:01:47 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
return l, err
|
|
|
|
}
|
|
|
|
|
2016-02-25 10:28:34 +08:00
|
|
|
// wait util get one new connection or listener is closed
|
|
|
|
// if listener is closed, err returned
|
2016-04-18 15:16:40 +08:00
|
|
|
func (l *Listener) Accept() (*Conn, error) {
|
|
|
|
conn, ok := <-l.accept
|
2016-02-19 17:01:47 +08:00
|
|
|
if !ok {
|
2016-02-25 10:28:34 +08:00
|
|
|
return conn, fmt.Errorf("channel close")
|
2016-02-19 17:01:47 +08:00
|
|
|
}
|
2016-02-25 10:28:34 +08:00
|
|
|
return conn, nil
|
2016-01-27 21:24:36 +08:00
|
|
|
}
|
|
|
|
|
2016-04-18 15:16:40 +08:00
|
|
|
func (l *Listener) Close() error {
|
2016-02-19 17:01:47 +08:00
|
|
|
if l.l != nil && l.closeFlag == false {
|
|
|
|
l.closeFlag = true
|
|
|
|
l.l.Close()
|
2016-04-18 15:16:40 +08:00
|
|
|
close(l.accept)
|
2016-02-19 17:01:47 +08:00
|
|
|
}
|
2016-04-18 15:16:40 +08:00
|
|
|
return nil
|
2016-02-19 17:01:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// wrap for TCPConn
|
2016-01-27 21:24:36 +08:00
|
|
|
type Conn struct {
|
2016-04-18 15:16:40 +08:00
|
|
|
TcpConn net.Conn
|
2016-02-19 17:01:47 +08:00
|
|
|
Reader *bufio.Reader
|
|
|
|
closeFlag bool
|
2016-06-03 17:51:45 +08:00
|
|
|
mutex sync.RWMutex
|
2016-01-27 21:24:36 +08:00
|
|
|
}
|
|
|
|
|
2016-04-18 15:16:40 +08:00
|
|
|
func NewConn(conn net.Conn) (c *Conn) {
|
|
|
|
c = &Conn{}
|
|
|
|
c.TcpConn = conn
|
|
|
|
c.Reader = bufio.NewReader(c.TcpConn)
|
|
|
|
c.closeFlag = false
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2016-02-19 17:01:47 +08:00
|
|
|
func ConnectServer(host string, port int64) (c *Conn, err error) {
|
|
|
|
c = &Conn{}
|
2016-01-27 21:24:36 +08:00
|
|
|
servertAddr, err := net.ResolveTCPAddr("tcp4", fmt.Sprintf("%s:%d", host, port))
|
|
|
|
if err != nil {
|
2016-02-19 17:01:47 +08:00
|
|
|
return
|
2016-01-27 21:24:36 +08:00
|
|
|
}
|
|
|
|
conn, err := net.DialTCP("tcp", nil, servertAddr)
|
|
|
|
if err != nil {
|
2016-02-19 17:01:47 +08:00
|
|
|
return
|
2016-01-27 21:24:36 +08:00
|
|
|
}
|
|
|
|
c.TcpConn = conn
|
|
|
|
c.Reader = bufio.NewReader(c.TcpConn)
|
2016-02-19 17:01:47 +08:00
|
|
|
c.closeFlag = false
|
|
|
|
return c, nil
|
2016-01-27 21:24:36 +08:00
|
|
|
}
|
|
|
|
|
2016-07-26 00:18:19 +08:00
|
|
|
// if the tcpConn is different with c.TcpConn
|
|
|
|
// you should call c.Close() first
|
2016-07-20 16:33:42 +08:00
|
|
|
func (c *Conn) SetTcpConn(tcpConn net.Conn) {
|
2016-07-26 00:18:19 +08:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
2016-07-20 16:33:42 +08:00
|
|
|
c.TcpConn = tcpConn
|
2016-07-26 00:18:19 +08:00
|
|
|
c.closeFlag = false
|
2016-07-20 16:33:42 +08:00
|
|
|
c.Reader = bufio.NewReader(c.TcpConn)
|
|
|
|
}
|
|
|
|
|
2016-01-27 21:24:36 +08:00
|
|
|
func (c *Conn) GetRemoteAddr() (addr string) {
|
|
|
|
return c.TcpConn.RemoteAddr().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conn) GetLocalAddr() (addr string) {
|
|
|
|
return c.TcpConn.LocalAddr().String()
|
|
|
|
}
|
|
|
|
|
2016-07-20 16:00:35 +08:00
|
|
|
func (c *Conn) Read(p []byte) (n int, err error) {
|
|
|
|
n, err = c.Reader.Read(p)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-01-27 21:24:36 +08:00
|
|
|
func (c *Conn) ReadLine() (buff string, err error) {
|
|
|
|
buff, err = c.Reader.ReadString('\n')
|
2016-06-06 11:43:41 +08:00
|
|
|
if err != nil {
|
2016-08-01 15:24:04 +08:00
|
|
|
// wsarecv error in windows means connection closed?
|
|
|
|
if err == io.EOF || strings.Contains(err.Error(), "wsarecv") {
|
2016-06-06 11:43:41 +08:00
|
|
|
c.mutex.Lock()
|
|
|
|
c.closeFlag = true
|
|
|
|
c.mutex.Unlock()
|
|
|
|
}
|
2016-02-19 17:01:47 +08:00
|
|
|
}
|
2016-01-27 21:24:36 +08:00
|
|
|
return buff, err
|
|
|
|
}
|
|
|
|
|
2016-07-20 16:00:35 +08:00
|
|
|
func (c *Conn) WriteBytes(content []byte) (n int, err error) {
|
|
|
|
n, err = c.TcpConn.Write(content)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-01-27 21:24:36 +08:00
|
|
|
func (c *Conn) Write(content string) (err error) {
|
|
|
|
_, err = c.TcpConn.Write([]byte(content))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-04-18 15:16:40 +08:00
|
|
|
func (c *Conn) SetDeadline(t time.Time) error {
|
2016-07-29 23:08:00 +08:00
|
|
|
return c.TcpConn.SetDeadline(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conn) SetReadDeadline(t time.Time) error {
|
|
|
|
return c.TcpConn.SetReadDeadline(t)
|
2016-04-18 15:16:40 +08:00
|
|
|
}
|
|
|
|
|
2016-01-27 21:24:36 +08:00
|
|
|
func (c *Conn) Close() {
|
2016-06-03 17:51:45 +08:00
|
|
|
c.mutex.Lock()
|
2016-02-25 10:28:34 +08:00
|
|
|
if c.TcpConn != nil && c.closeFlag == false {
|
2016-02-19 17:01:47 +08:00
|
|
|
c.closeFlag = true
|
2016-02-05 14:18:26 +08:00
|
|
|
c.TcpConn.Close()
|
|
|
|
}
|
2016-06-03 17:51:45 +08:00
|
|
|
c.mutex.Unlock()
|
2016-01-27 21:24:36 +08:00
|
|
|
}
|
|
|
|
|
2016-06-03 17:51:45 +08:00
|
|
|
func (c *Conn) IsClosed() (closeFlag bool) {
|
|
|
|
c.mutex.RLock()
|
2016-07-29 23:08:00 +08:00
|
|
|
defer c.mutex.RUnlock()
|
2016-06-03 17:51:45 +08:00
|
|
|
closeFlag = c.closeFlag
|
|
|
|
return
|
2016-01-27 21:24:36 +08:00
|
|
|
}
|
2016-07-29 23:08:00 +08:00
|
|
|
|
|
|
|
// when you call this function, you should make sure that
|
|
|
|
// remote client won't send any bytes to this socket
|
|
|
|
func (c *Conn) CheckClosed() bool {
|
|
|
|
c.mutex.RLock()
|
|
|
|
if c.closeFlag {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
c.mutex.RUnlock()
|
|
|
|
|
|
|
|
// err := c.TcpConn.SetReadDeadline(time.Now().Add(100 * time.Microsecond))
|
|
|
|
err := c.TcpConn.SetReadDeadline(time.Now().Add(time.Millisecond))
|
|
|
|
if err != nil {
|
|
|
|
c.Close()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
var tmp []byte = make([]byte, 1)
|
|
|
|
_, err = c.TcpConn.Read(tmp)
|
|
|
|
if err == io.EOF {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.TcpConn.SetReadDeadline(time.Time{})
|
|
|
|
if err != nil {
|
|
|
|
c.Close()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|