github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/gp/mux-app.go (about) 1 /* For license and copyright information please see LEGAL file in repository */ 2 3 package gp 4 5 import ( 6 "../protocol" 7 ) 8 9 type AppMultiplexer struct{} 10 11 // MakeGPNetwork register app to OS GP router and start handle income GP packets. 12 func (appMux *AppMultiplexer) Init() (err protocol.Error) { 13 protocol.App.LogInfo("GP network begin listening...") 14 return 15 } 16 17 func (appMux *AppMultiplexer) HeaderID() protocol.NetworkLinkNextHeaderID { 18 return protocol.NetworkLinkNextHeaderGP 19 } 20 21 // Receive handle GP packet with any application protocol and response just some basic data! 22 // Protocol Standard : https://github.com/GeniusesGroup/RFCs/blob/master/Giti-Network.md 23 func (appMux *AppMultiplexer) Receive(linkConn protocol.NetworkLinkConnection, packet []byte) { 24 var err protocol.Error 25 var gpAddr [16]byte = GetSourceAddr(packet) 26 // Find Connection from ConnectionPoolByPeerAdd by requester GP 27 var conn protocol.Connection = protocol.App.GetConnectionByPeerAddr(gpAddr) 28 // If it is first time that user want to connect or longer than server GC old unused connections! 29 if conn == nil { 30 // conn, err = MakeNewConnectionByPeerAdd(gpAddr, linkConn) 31 if err != nil { 32 // Send response or just ignore packet 33 // TODO::: DDOS!!?? 34 return 35 } 36 protocol.App.RegisterConnection(conn) 37 } 38 conn.(*Connection).Receive(packet) 39 return 40 } 41 42 // Shutdown the listener when the application closes or force to closes by not recovered panic! 43 func (appMux *AppMultiplexer) Shutdown() { 44 // first closing open listener for income packet and refuse all new packet, 45 // then closing all idle connections, 46 // and then waiting indefinitely for connections to return to idle 47 // and then shut down 48 }