Skip to content

Commit

Permalink
Add zproto protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
wubenqi committed Feb 1, 2018
1 parent 5b65a6d commit 8794929
Show file tree
Hide file tree
Showing 5 changed files with 561 additions and 185 deletions.
61 changes: 61 additions & 0 deletions access/auth_key2/auth_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2018, https://github.com/nebulaim
* All rights reserved.
*
* 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.
*/

package main

import (
"github.com/nebulaim/telegramd/baselib/app"
"github.com/nebulaim/telegramd/access/frontend2/server"
"github.com/golang/glog"
"net"
)

type ServerConfig struct {
Network string
Addr string
ProtoName string
ServiceName string
}

type AuthKeyInsance struct {
server *server.FrontendServer
}

func (this *AuthKeyInsance) Initialize() error {
listener, err := net.Listen("tcp", "0.0.0.0:22345")
if err != nil {
glog.Errorf("listen error: %v", err)
return err
}

this.server = server.NewFrontendServer(listener, "ztproto")
return nil
}

func (this *AuthKeyInsance) RunLoop() {
this.server.Serve()
}

func (this *AuthKeyInsance) Destroy() {
this.server.Stop()
}


func main() {
instance := &AuthKeyInsance{}
app.DoMainAppInsance(instance)
}
77 changes: 77 additions & 0 deletions access/auth_key2/server/auth_key_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2018, https://github.com/nebulaim
* All rights reserved.
*
* 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.
*/

package server

import (
"github.com/golang/glog"
"github.com/nebulaim/telegramd/baselib/net2"
"net"
"github.com/nebulaim/telegramd/mtproto"
"fmt"
)

type AuthKeyServer struct {
server* net2.TcpServer
}

func NewFrontendServer(listener net.Listener, protoName string) *AuthKeyServer {
listener, err := net.Listen("tcp", "0.0.0.0:12345")
if err != nil {
glog.Fatalf("listen error: %v", err)
// return
}
s := &AuthKeyServer{}
s.server = net2.NewTcpServer(listener, "frontend", protoName, 1024, s)
return s
}

func (s* AuthKeyServer) Serve() {
s.server.Serve()
}

func (s* AuthKeyServer) Stop() {
s.server.Stop()
}

func (s *AuthKeyServer) OnNewConnection(conn *net2.TcpConnection) {
// conn.Context = &ConnContext{}
glog.Infof("OnNewConnection %v", conn.RemoteAddr())
}

func (s *AuthKeyServer) OnDataArrived(conn *net2.TcpConnection, msg interface{}) error {
glog.Infof("echo_server recv peer(%v) data: %v", conn.RemoteAddr(), msg)
// connContext, _ := conn.Context.(*ConnContext)
// _ = connContext

//switch msg.(type) {
//case *mtproto.UnencryptedRawMessage:
//case *mtproto.EncryptedRawMessage:
//default:
// // 不可能发生,直接coredump吧
// err := fmt.Errorf("")
// glog.Error(err)
// return err
//}
//return conn.Send(msg)

return nil
}

func (s *AuthKeyServer) OnConnectionClosed(conn *net2.TcpConnection) {
glog.Infof("OnConnectionClosed - %v", conn.RemoteAddr())
}
48 changes: 24 additions & 24 deletions baselib/net2/tcp_client_group_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ func NewTcpClientGroupManager(protoName string, clients map[string][]string, cb
return group
}

func (group *TcpClientGroupManager) Serve() bool {
group.clientMapLock.Lock()
defer group.clientMapLock.Unlock()
func (this *TcpClientGroupManager) Serve() bool {
this.clientMapLock.Lock()
defer this.clientMapLock.Unlock()

for _, v := range group.clientMap {
for _, v := range this.clientMap {
for _, c := range v {
c.Serve()
}
Expand All @@ -70,11 +70,11 @@ func (group *TcpClientGroupManager) Serve() bool {
return true
}

func (group *TcpClientGroupManager) Stop() bool {
group.clientMapLock.Lock()
defer group.clientMapLock.Unlock()
func (this *TcpClientGroupManager) Stop() bool {
this.clientMapLock.Lock()
defer this.clientMapLock.Unlock()

for _, v := range group.clientMap {
for _, v := range this.clientMap {
for _, c := range v {
c.Stop()
}
Expand All @@ -83,49 +83,49 @@ func (group *TcpClientGroupManager) Stop() bool {
return true
}

func (group *TcpClientGroupManager) GetConfig() interface{} {
func (this *TcpClientGroupManager) GetConfig() interface{} {
return nil
}

func (group *TcpClientGroupManager) AddClient(name string, address string) {
func (this *TcpClientGroupManager) AddClient(name string, address string) {
glog.Info("TcpClientGroup AddClient name ", name, " address ", address)
group.clientMapLock.Lock()
defer group.clientMapLock.Unlock()
this.clientMapLock.Lock()
defer this.clientMapLock.Unlock()

m, ok := group.clientMap[name]
m, ok := this.clientMap[name]

if !ok {
group.clientMap[name] = make(map[string]*TcpClient)
this.clientMap[name] = make(map[string]*TcpClient)
}

m, _ = group.clientMap[name]
m, _ = this.clientMap[name]

_, ok = m[address]

if ok {
return
}

client := NewTcpClient(name, 10 * 1024, group.protoName, address, group.callback)
client := NewTcpClient(name, 10 * 1024, this.protoName, address, this.callback)

m[address] = client

client.Serve()
}

func (group *TcpClientGroupManager) RemoveClient(name string, address string) {
func (this *TcpClientGroupManager) RemoveClient(name string, address string) {
glog.Info("TcpClientGroup RemoveClient name ", name, " address ", address)

group.clientMapLock.Lock()
defer group.clientMapLock.Unlock()
this.clientMapLock.Lock()
defer this.clientMapLock.Unlock()

m, ok := group.clientMap[name]
m, ok := this.clientMap[name]

if !ok {
return
}

m, _ = group.clientMap[name]
m, _ = this.clientMap[name]

c, ok := m[address]

Expand All @@ -135,11 +135,11 @@ func (group *TcpClientGroupManager) RemoveClient(name string, address string) {

c.Stop()

delete(group.clientMap[name], address)
delete(this.clientMap[name], address)
}

func (group *TcpClientGroupManager) SendData(name string, msg interface{}) error {
tcpConn := group.getRotationSession(name)
func (this *TcpClientGroupManager) SendData(name string, msg interface{}) error {
tcpConn := this.getRotationSession(name)
if tcpConn == nil {
return errors.New("Can not get connection!!")
}
Expand Down
Loading

0 comments on commit 8794929

Please sign in to comment.