コード例 #1
0
ファイル: handler.go プロジェクト: neutony/chatserver
	"github.com/gansidui/chatserver/config"
	"github.com/gansidui/chatserver/dao/c2c"
	"github.com/gansidui/chatserver/dao/cid"
	"github.com/gansidui/chatserver/packet"
	"github.com/gansidui/chatserver/pb"
	"github.com/gansidui/chatserver/report"
	"github.com/gansidui/chatserver/utils/convert"
	"github.com/gansidui/chatserver/utils/safemap"
	"log"
	"net"
	"time"
)

var (
	// 映射conn到uuid, uuid --> conn
	UuidMapConn *safemap.SafeMap = safemap.NewSafeMap()
	// 映射uuid到conn, conn --> uuid
	ConnMapUuid *safemap.SafeMap = safemap.NewSafeMap()
	// ConnMapLoginStatus 映射conn的登陆状态,conn->loginstatus
	// loginstatus为nil表示conn已经登陆, 否则 loginstatus表示conn连接服务器时的时间
	// 用于判断登陆是否超时,控制恶意连接
	ConnMapLoginStatus *safemap.SafeMap = safemap.NewSafeMap()
)

// 关闭conn
func CloseConn(conn *net.TCPConn) {
	conn.Close()
	ConnMapLoginStatus.Delete(conn)
	uuid := ConnMapUuid.Get(conn)
	UuidMapConn.Delete(uuid)
	ConnMapUuid.Delete(conn)
コード例 #2
0
ファイル: uuid.go プロジェクト: jackwanger/chatserver
package dao

import (
	"database/sql"
	"github.com/gansidui/chatserver/utils/convert"
	"github.com/gansidui/chatserver/utils/safemap"
	_ "github.com/mattn/go-sqlite3"
	"log"
	"sync"
	"time"
)

var (
	uuid_db             *sql.DB
	uuid_lock           *sync.RWMutex    = new(sync.RWMutex)
	uuid_allUsers       *safemap.SafeMap = safemap.NewSafeMap() // 所有用户的uuid信息(uuid, registertime)
	uuid_allOnlineUsers *safemap.SafeMap = safemap.NewSafeMap() // 在线用户, (uuid, true)
)

// 初始化
func UuidInit(dbPath string) {
	var err error
	uuid_db, err = sql.Open("sqlite3", dbPath)
	if err != nil {
		log.Fatal(err, "\r\n")
	}

	uuid_db.Exec("create table if not exists data(uuid varchar(32) primary key, registertime timestamp)")
	rows, err := uuid_db.Query("select * from data")
	if err != nil {
		log.Fatal(err, "\r\n")