Esempio n. 1
0
func main() {
	version()
	fmt.Printf("built on %s\n", BuildTime())
	flag.Parse()
	cfg := NewRouterConfig(*InputConfFile)
	err := cfg.LoadConfig()
	if err != nil {
		glog.Error(err.Error())
		return
	}
	p := link.PacketN(2, link.BigEndianBO, link.LittleEndianBF)

	server, err := link.Listen(cfg.TransportProtocols, cfg.Listen, p)
	if err != nil {
		glog.Error(err.Error())
		return
	}
	glog.Info("server start: ", server.Listener().Addr().String())

	r := NewRouter(cfg)
	go r.subscribeChannels()
	server.AcceptLoop(func(session *link.Session) {

	})
}
Esempio n. 2
0
func main() {
	version()
	fmt.Printf("built on %s\n", BuildTime())
	flag.Parse()
	cfg := NewMsgServerConfig(*InputConfFile)
	err := cfg.LoadConfig()
	if err != nil {
		glog.Error(err.Error())
		return
	}

	ms := NewMsgServer(cfg)

	p := link.PacketN(2, link.BigEndianBO, link.LittleEndianBF)

	ms.server, err = link.Listen(cfg.TransportProtocols, cfg.Listen, p)
	if err != nil {
		panic(err)
	}
	glog.Info("server start:", ms.server.Listener().Addr().String())

	ms.createChannels()
	go ms.scanDeadSession()

	ms.server.AcceptLoop(func(session *link.Session) {
		glog.Info("client ", session.Conn().RemoteAddr().String(), " | in")

		go handleSession(ms, session)
	})
}
Esempio n. 3
0
func main() {
	version()
	fmt.Printf("built on %s\n", BuildTime())
	flag.Parse()
	cfg := NewGatewayConfig(*InputConfFile)
	err := cfg.LoadConfig()
	if err != nil {
		glog.Error(err.Error())
		return
	}

	p := link.PacketN(2, link.BigEndianBO, link.LittleEndianBF)

	server, err := link.Listen(cfg.TransportProtocols, cfg.Listen, p)
	if err != nil {
		glog.Error(err.Error())
		return
	}
	glog.Info("server start: ", server.Listener().Addr().String())

	server.AcceptLoop(func(session *link.Session) {
		glog.Info("client ", session.Conn().RemoteAddr().String(), " | in")
		msgServer := common.SelectServer(cfg.MsgServerList, cfg.MsgServerNum)

		err = session.Send(link.Binary(msgServer))
		if err != nil {
			glog.Error(err.Error())
			return
		}
		session.Close(nil)
		glog.Info("client ", session.Conn().RemoteAddr().String(), " | close")
	})
}
Esempio n. 4
0
File: main.go Progetto: ilahsa/go
// This is an echo server demo work with the echo_client.
// usage:
//     go run echo_server/main.go
func main() {
	common.ULogger.Info("stat chat at ", 10010)

	//flag.Parse()

	link.DefaultConnBufferSize = *buffersize
	link.DefaultProtocol = http.HttpPacket

	server, err := link.Listen("tcp", "0.0.0.0:10010")
	if err != nil {
		panic(err)
	}
	//server.Protocol()
	common.ULogger.Info("server start:", server.Listener().Addr().String())

	server.Serve(func(session *link.Session) {

		common.ULogger.Info("client", session.Conn().RemoteAddr().String(), "in")
		SessionInfo := &proxy.Session_Info{}
		session.State = SessionInfo

		session.Process(func(msg *link.InBuffer) error {

			common.ULogger.Info("receive request ", session.Conn().RemoteAddr().String(), ":", string(msg.Data))

			req, _ := http.Parse(msg.Data)
			req_CallId, ok1 := req.HEADER["CallId"]
			resp, other := proxy.ProcessRequest(req,
				SessionInfo)

			_ = other
			_, ok2 := resp.HEADER["CallId"]
			// add callid
			if ok1 && !ok2 {
				resp.AddResponseHeader("CallId", req_CallId)
			}
			//str := "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"

			common.ULogger.Info("send response ", session.Conn().RemoteAddr().String(), ":", string(resp.ToByte()))

			if SessionInfo.Data_Ex != "" {
				session.Send(link.Bytes(resp.ToByte()))
				return session.Send(link.String(SessionInfo.Data_Ex))
			} else {
				return session.Send(link.Bytes(resp.ToByte()))
			}
		})

		common.ULogger.Info("client", session.Conn().RemoteAddr().String(), "close")
	})
}
Esempio n. 5
0
File: main.go Progetto: ilahsa/go
// This is an echo server demo work with the echo_client.
// usage:
//     go run echo_server/main.go
func main() {
	flag.Parse()

	link.DefaultConnBufferSize = *buffersize
	link.DefaultProtocol = TCProtocol

	server, err := link.Listen("tcp", "0.0.0.0:10010")
	if err != nil {
		panic(err)
	}

	ULogger.Info("server start:", server.Listener().Addr().String())

	server.Serve(func(session *link.Session) {
		ULogger.Info("client", session.Conn().RemoteAddr().String(), "in")

		session.Process(func(msg *link.InBuffer) error {
			ULogger.Info("client", session.Conn().RemoteAddr().String(), "say:", string(msg.Data))
			var dat map[string]string

			err := json.Unmarshal(msg.Data, &dat)
			if err != nil {
				ULogger.Errorf("bad request,req is %s", string(msg.Data))
				return errors.New("bad request")
			} else {
				er := Process(session, dat)
				//ULogger.Infof("tttt %v\n", er)
				if er != nil {
					panic(er)
				}
				return er
			}

		})

		ULogger.Info("client", session.Conn().RemoteAddr().String(), "close")
		if session.State != nil {
			u := session.State.(*User)
			userId := u.Id
			add := session.Conn().RemoteAddr().String()
			if u.UserType == "P" {
				Exec(`insert into user_activities(user_id,active_time,active_type,user_type,other_info) values(?,now(),'end','production',?)`, userId, add)
			} else if u.UserType == "C" {
				Exec(`insert into user_activities(user_id,active_time,active_type,user_type,other_info) values(?,now(),'end','customer',?)`, userId, add)
			}
		}
	})
}
Esempio n. 6
0
func StartTestGateway(t *testing.T, backendAddr string) *Frontend {
	listener, err := link.Listen("tcp", "0.0.0.0:0", protocol)
	unitest.NotError(t, err)

	var linkIds []uint64

	gateway := NewFrontend(listener.(*packet.Listener),
		func(_ *link.Session) (uint64, error) {
			return linkIds[rand.Intn(len(linkIds))], nil
		},
	)

	for i := 0; i < 10; i++ {
		id, err := gateway.AddBackend("tcp",
			backendAddr,
			stream.New(1024, 1024, 1024),
		)
		unitest.NotError(t, err)
		linkIds = append(linkIds, id)
	}

	return gateway
}