s2c

package
Version: v0.0.0-...-2770a49 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 1, 2015 License: MIT Imports: 24 Imported by: 0

README

s2c

放置服务端与客户端通信源程序

packet.go

定义packet io相关的工具,包括包加密,解密,解包等操作

typedef.go

定义packet, session,server 相关结构

session.go

管理会话:对上层输入/输出message,对下层输出tcpstream

server.go

服务容器,启动服务器,监听一个端口的tcp连接

Documentation

Overview

4byte(packagehead)+4byte(packagetype)+4byte(packagelength)+data(protobuf)+4byte(CRC32) 包格式

Index

Constants

View Source
const (
	Packethead = 0x56565656
	//PacketBufSize   = 1024
	MaxPacketLength = 0x00ffffff
)
View Source
const (
	MaxDelayTime  = 60                       //服务器发出公钥  30s内没收到正确的回馈包则会关闭conn
	APPKEY        = string("appkeyismartin") //和客户端统一好的appkey
	MaxOutLineMsg = 200
)
View Source
const (
	BusinessKey = string("IamBusinessLayer") //作为AES加密Key

)

Variables

View Source
var (
	ErrorMysql        = errors.New("mysql err!")
	ErrorDataTransfer = errors.New("packet data protobuf transfer error!") //protobuf解析出错
	ErrorDataLength   = errors.New("packet was too long!")                 //data长度过长
	ErrorPacketHead   = errors.New("packet error!")                        //头错
	ErrorDataCRC      = errors.New("CRC32 error!")
	ErrorGetOneError  = errors.New("GetOne from FeedBackInServerTable error!")
)
View Source
var (
	PublicMess = &protocol.Message{
		Data: proto.String(`-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCbmNv4mBsuPb4xzuUwT2TxEbjG
va76zLrF//NRiUKV/T8mKtfv/R+Q+7FGwYkZbDNl5bO0UF0MR69u8ZE1cBAzn74z
JGqQVs9QrRgp3VTYM/9s8nOvogcz6+l1amEY+djV3iztHOxyptl1Eq8d3r82kcwf
ybxRlNMfwNhT7VpvqQIDAQAB
-----END PUBLIC KEY-----`),
	}
	PublicCMD = &protocol.Command{
		Sender:   proto.Uint64(0),
		Receiver: proto.Uint64(0),
		Message:  PublicMess,
	}
)

********Type=0: IM->Client : 发送公钥********不加密

View Source
var MaxMemberInGroup int //这里设置一个群的成员最大数
View Source
var MaxRecendTimes int //最大重发次数
View Source
var MaxSession int
View Source
var PacketBufSize int

var Packethead int

View Source
var SessionTable map[uint64]*Session //存在线用户的session
View Source
var TokenTime int //令牌有效时间,单位秒
View Source
var UserTable map[uint64]Token // 存在线用户,存token,以及token有效状态

********Tables in RAM var FeedBackInServerTable map[uint32]PacketAndTime //等待回馈包的表

Functions

func Beat

func Beat(uid uint64)

收到心跳包则心跳+1

func CreateServer

func CreateServer(connString string)

********创建一个server 注意:这里设置了最多20000个客户

func DatabaseLog

func DatabaseLog(messages ...interface{})

func GenRandom

func GenRandom() uint32

random生成器

func GetCommand

func GetCommand(packet *Packet) (cmd *protocol.Command, err error)

********拆分包结构//客户端读取公钥和random********

func GetInterfaceCMD

func GetInterfaceCMD(data []byte) (cmd *business.Command, err error)

func ImBusinessLog

func ImBusinessLog(messages ...interface{})

func ImCountbeatLog

func ImCountbeatLog(messages ...interface{})

func ImMaxSession

func ImMaxSession(messages ...interface{})

func ImPacketInTime

func ImPacketInTime(messages ...interface{})

测试性能用

func ImPacketOutTime

func ImPacketOutTime(messages ...interface{})

func ImServerLog

func ImServerLog(messages ...interface{})

func ImSessionLog(messages ...interface{}) {

var year, month, day = time.Now().Date()
var LogPath = "./log/" + strconv.Itoa(year) + "/" + month.String() + "/" + strconv.Itoa(day) + "/"
err := os.MkdirAll(LogPath, 0777)
if err != nil {
	fmt.Println(err)
	return
}
imsessionlog, err := os.OpenFile(LogPath+"imSession.log", os.O_RDWR|os.O_APPEND|os.O_CREATE, 0777)

if err != nil {
	fmt.Println(err)
	return
}
	defer imsessionlog.Close()
sessionlog := log.New(imsessionlog, "\r\n", log.Ldate|log.Ltime)
sessionlog.Println(messages)

}

func ImSessionErrLog(messages ...interface{}) {

year, month, day := time.Now().Date()
var LogPath = "./log/" + strconv.Itoa(year) + "/" + month.String() + "/" + strconv.Itoa(day) + "/"
err := os.MkdirAll(LogPath, 0777)
if err != nil {
	fmt.Println(err)
	return
}
imsessionlog, err := os.OpenFile(LogPath+"imSessionErr.log", os.O_RDWR|os.O_APPEND|os.O_CREATE, 0777)

if err != nil {
	fmt.Println(err)
	return
}
	defer imsessionlog.Close()
sessionlog := log.New(imsessionlog, "\r\n", log.Ldate|log.Ltime)
sessionlog.Println(messages)

}

func ImUserLog

func ImUserLog(messages ...interface{})

10.4:日志保留(1.平常用户登录登出记录,表的变化;2.IM错误(server+session+packetio);3.数据库操作(主要记录错误))

func MakeToken

func MakeToken() string

产生随机的字符串,做令牌用

func MergeSort

func MergeSort(s1 []string, s2 []string, res1 *[]string, res2 *[]string, secondBegin int, end int)

func QuickSort

func QuickSort(s1 []string, s2 []string, low int, high int)

Types

type FeedBackInServer

type FeedBackInServer struct {
	FeedBackInServerTable map[uint32]PacketAndTime
	// contains filtered or unexported fields
}
var FeedBack FeedBackInServer

func (*FeedBackInServer) AddOne

func (f *FeedBackInServer) AddOne(random uint32, packetAndTime PacketAndTime)

func (*FeedBackInServer) DeleteOne

func (f *FeedBackInServer) DeleteOne(random uint32)

func (*FeedBackInServer) GetAll

func (f *FeedBackInServer) GetAll() (random []uint32, packetAndTime []PacketAndTime)

func (*FeedBackInServer) GetOne

func (f *FeedBackInServer) GetOne(random uint32) (PacketAndTime, error)

type InterfaceForProject

type InterfaceForProject struct {
	InterfaceDB imdatabase.ImDb
}

func (*InterfaceForProject) CloseDialog

func (self *InterfaceForProject) CloseDialog(interfaceCMD *business.Command) (resultPacket Packet, err error)

func (*InterfaceForProject) CreateGroup

func (self *InterfaceForProject) CreateGroup(interfaceCMD *business.Command) (resultPacket Packet, err error)

func (*InterfaceForProject) InqueryHistory

func (self *InterfaceForProject) InqueryHistory(interfaceCMD *business.Command) (resultPacket Packet, err error)

func (*InterfaceForProject) LeaveGroup

func (self *InterfaceForProject) LeaveGroup(interfaceCMD *business.Command) (resultPacket Packet, err error)

func (*InterfaceForProject) LogIn

func (self *InterfaceForProject) LogIn(interfaceCMD *business.Command) (resultPacket Packet, err error)

func (*InterfaceForProject) LogOut

func (self *InterfaceForProject) LogOut(interfaceCMD *business.Command) (resultPacket Packet, err error)

func (*InterfaceForProject) PushMessage

func (self *InterfaceForProject) PushMessage(interfaceCMD *business.Command) (resultPacket Packet, err error)

type Packet

type Packet struct {
	// contains filtered or unexported fields
}

********Packet 属性********

func MakeInterfacePacket

func MakeInterfacePacket(cmd *business.Command, Type uint32) *Packet

func MakePacket

func MakePacket(cmd *protocol.Command, Type uint32) *Packet

********构建包结构********

func NewPacket

func NewPacket() *Packet

********缓冲清除******** func (w *PacketWriter) Flush() error {

return w.bw.Flush()

}

********创建新Packet包********

func (*Packet) GetData

func (p *Packet) GetData() (data []byte)

func (*Packet) GetType

func (p *Packet) GetType() (t uint32)

func (*Packet) SetData

func (p *Packet) SetData(data []byte)

func (*Packet) SetType

func (p *Packet) SetType(t uint32)

********Packet 方法********

type PacketAndTime

type PacketAndTime struct {
	Packet      Packet
	Receiver    uint64
	StartTime   int64 //time.unix()
	ReSendTimes int   //用户重发次数
}

type PacketReader

type PacketReader struct {
	AESKEY []byte
	// contains filtered or unexported fields
}

------------------------------------------ ********PacketReader 属性********

func NewPacketReader

func NewPacketReader(readConn net.Conn) *PacketReader

********新建PacketReader********

func (*PacketReader) ReadPacket

func (p *PacketReader) ReadPacket() (Packet *Packet, err error)

********读取整个包********

type PacketWriter

type PacketWriter struct {
	AESKEY []byte
	// contains filtered or unexported fields
}

------------------------------------------ ********PacketWriter 属性********

func NewPacketWriter

func NewPacketWriter(writeConn net.Conn) *PacketWriter

********新建PacketWriter********

func (*PacketWriter) WritePacket

func (w *PacketWriter) WritePacket(packet *Packet, rsaPublicKey []byte) error

********写包********

type Server

type Server struct {
	// contains filtered or unexported fields
}

********Server 属性

func (*Server) DelOutLineMessage

func (self *Server) DelOutLineMessage()

func (*Server) ReSend

func (self *Server) ReSend()

func (*Server) Sort

func (self *Server) Sort()

*******************************************************************************

func (*Server) Stop

func (self *Server) Stop()

********Server 方法 ********关闭server

type Session

type Session struct {
	// contains filtered or unexported fields
}

********Session 属性

func CreateSession

func CreateSession(conns net.Conn, imDB imdatabase.ImDb) *Session

func (*Session) GetBeatCount

func (self *Session) GetBeatCount() int32

func (*Session) GetConn

func (self *Session) GetConn() net.Conn

func (*Session) GetUid

func (self *Session) GetUid() uint64

********Session 方法

func (*Session) Listen

func (self *Session) Listen()

func (*Session) Read

func (self *Session) Read()

读入tcp里面的包到Session.incoming

func (*Session) SetBeatCount

func (self *Session) SetBeatCount(beatCount int32)

func (*Session) SetConn

func (self *Session) SetConn(conn net.Conn)

func (*Session) SetUid

func (self *Session) SetUid(uid uint64)

func (*Session) Write

func (self *Session) Write()

把Session.outgoing里面的内容写出去

type Token

type Token struct {
	AToken    string
	Situation int // 1:= 有效,2:=180s有效
}

type TotalStruct

type TotalStruct struct {
	Total int
	// contains filtered or unexported fields
}
var TotalConn TotalStruct

func (*TotalStruct) Abstract

func (t *TotalStruct) Abstract()

func (*TotalStruct) Add

func (t *TotalStruct) Add()

func (*TotalStruct) GetTotal

func (t *TotalStruct) GetTotal() int

Directories

Path Synopsis
Package business is a generated protocol buffer package.
Package business is a generated protocol buffer package.
Package protocol is a generated protocol buffer package.
Package protocol is a generated protocol buffer package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL