forked from GoBelieveIO/im_service
/
connection.go
118 lines (102 loc) · 3.09 KB
/
connection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* Copyright (c) 2014-2015, GoBelieve
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package main
import "net"
import "time"
import "sync"
import log "github.com/golang/glog"
import "github.com/googollee/go-engine.io"
const CLIENT_TIMEOUT = (60 * 6)
type Connection struct {
conn interface{}
wt chan *Message
ewt chan *EMessage //在线消息
owt chan *EMessage //离线消息
//客户端协议版本号
version int
tm time.Time
appid int64
uid int64
device_id string
device_ID int64 //generated by device_id + platform_id
platform_id int8
unackMessages map[int]*EMessage
unacks map[int]int64
mutex sync.Mutex
}
func (client *Connection) SendMessage(uid int64, msg *Message) bool {
return Send0Message(client.appid, uid, msg)
}
// 根据连接类型获取消息
func (client *Connection) read() *Message {
if conn, ok := client.conn.(net.Conn); ok {
conn.SetDeadline(time.Now().Add(CLIENT_TIMEOUT * time.Second))
return ReceiveMessage(conn)
} else if conn, ok := client.conn.(engineio.Conn); ok {
return ReadEngineIOMessage(conn)
}
return nil
}
// 根据连接类型发送消息
func (client *Connection) send(msg *Message) {
if conn, ok := client.conn.(net.Conn); ok {
err := SendMessage(conn, msg)
if err != nil {
log.Info("send msg:", Command(msg.cmd), " tcp err:", err)
}
} else if conn, ok := client.conn.(engineio.Conn); ok {
SendEngineIOBinaryMessage(conn, msg)
}
}
// 根据连接类型关闭
func (client *Connection) close() {
if conn, ok := client.conn.(net.Conn); ok {
conn.Close()
} else if conn, ok := client.conn.(engineio.Conn); ok {
conn.Close()
}
}
func (client *Connection) RemoveUnAckMessage(ack *MessageACK) *EMessage {
client.mutex.Lock()
defer client.mutex.Unlock()
var msgid int64
var msg *Message
var ok bool
seq := int(ack.seq)
if msgid, ok = client.unacks[seq]; ok {
log.Infof("dequeue offline msgid:%d uid:%d\n", msgid, client.uid)
delete(client.unacks, seq)
} else {
log.Warning("can't find msgid with seq:", seq)
}
if emsg, ok := client.unackMessages[seq]; ok {
msg = emsg.msg
delete(client.unackMessages, seq)
}
return &EMessage{msgid:msgid, msg:msg}
}
func (client *Connection) AddUnAckMessage(emsg *EMessage) {
client.mutex.Lock()
defer client.mutex.Unlock()
seq := emsg.msg.seq
client.unacks[seq] = emsg.msgid
if emsg.msg.cmd == MSG_IM || emsg.msg.cmd == MSG_GROUP_IM {
client.unackMessages[seq] = emsg
}
}