Commit ace8c9c1 authored by Chunchi Che's avatar Chunchi Che

add impl of StocHsPlayerEnter and update some log

parent cb02e0b4
...@@ -16,7 +16,7 @@ import ( ...@@ -16,7 +16,7 @@ import (
const FILLING_TOKEN uint16 = 0xcccc const FILLING_TOKEN uint16 = 0xcccc
const UTF16_BUFFER_MAX_LEN int = 20 const UTF16_BUFFER_MAX_LEN int = 20
const PACKET_MIN_LEN int = 3 const PACKET_MIN_LEN int = 3
const COMPONENT = "[transform]"
const ( const (
ProtobufToRawBuf = 1 ProtobufToRawBuf = 1
RawBufToProtobuf = 2 RawBufToProtobuf = 2
...@@ -28,6 +28,7 @@ const ( ...@@ -28,6 +28,7 @@ const (
StocJoinGame = 18 StocJoinGame = 18
StocChat = 25 StocChat = 25
StocHsPlayerEnter = 32
) )
type YgoPacket struct { type YgoPacket struct {
...@@ -72,6 +73,17 @@ func bufferToPacket(p []byte) (YgoPacket, error) { ...@@ -72,6 +73,17 @@ func bufferToPacket(p []byte) (YgoPacket, error) {
// todo: impl Reader/Writer for buffer // todo: impl Reader/Writer for buffer
packet_len := binary.LittleEndian.Uint16(p) packet_len := binary.LittleEndian.Uint16(p)
proto := p[2] proto := p[2]
if len(p) < int(packet_len+2) {
log.Printf(COMPONENT+
`Unmatched packet size, proto=%d, buffer length=%d, packet_len=%d,
Use the buffer length.\n`,
proto, len(p), packet_len,
)
packet_len = uint16(len(p) - 2)
}
exdata := p[3 : packet_len+2] exdata := p[3 : packet_len+2]
return YgoPacket{ return YgoPacket{
...@@ -96,7 +108,7 @@ func Transform(src []byte, tranformType int) ([]byte, error) { ...@@ -96,7 +108,7 @@ func Transform(src []byte, tranformType int) ([]byte, error) {
case *(ygopropb.YgoCtosMsg_CtosJoinGame): case *(ygopropb.YgoCtosMsg_CtosJoinGame):
packet = (*pCtosJoinGame)(message.GetCtosJoinGame()).Pb2Packet() packet = (*pCtosJoinGame)(message.GetCtosJoinGame()).Pb2Packet()
default: default:
return nil, errors.New("Unhandled YgoCtosMsg type") return nil, errors.New(COMPONENT + "Unhandled YgoCtosMsg type")
} }
return packetToBuffer(packet), nil return packetToBuffer(packet), nil
...@@ -113,13 +125,15 @@ func Transform(src []byte, tranformType int) ([]byte, error) { ...@@ -113,13 +125,15 @@ func Transform(src []byte, tranformType int) ([]byte, error) {
pb = pStocChat{}.Packet2Pb(packet) pb = pStocChat{}.Packet2Pb(packet)
case StocJoinGame: case StocJoinGame:
pb = pStocJoinGame{}.Packet2Pb(packet) pb = pStocJoinGame{}.Packet2Pb(packet)
case StocHsPlayerEnter:
pb = pStocHsPlayerEnter{}.Packet2Pb(packet)
default: default:
return nil, errors.New(fmt.Sprintf("Unhandled YgoStocMsg type, proto=%d", packet.Proto)) return nil, errors.New(fmt.Sprintf(COMPONENT+"Unhandled YgoStocMsg type, proto=%d", packet.Proto))
} }
return proto.Marshal(&pb) return proto.Marshal(&pb)
} else { } else {
return nil, errors.New("Unknown tranformType") return nil, errors.New(COMPONENT + "Unknown tranformType")
} }
} }
...@@ -233,6 +247,25 @@ func (_ pStocJoinGame) Packet2Pb(pkt YgoPacket) ygopropb.YgoStocMsg { ...@@ -233,6 +247,25 @@ func (_ pStocJoinGame) Packet2Pb(pkt YgoPacket) ygopropb.YgoStocMsg {
} }
} }
type pStocHsPlayerEnter struct{}
func (_ pStocHsPlayerEnter) Packet2Pb(pkt YgoPacket) ygopropb.YgoStocMsg {
name_max := UTF16_BUFFER_MAX_LEN * 2
name := utf16BufferToStr(pkt.Exdata[:name_max])
pos := pkt.Exdata[name_max]
msg := ygopropb.YgoStocMsg_StocHsPlayerEnter{
StocHsPlayerEnter: &ygopropb.StocHsPlayerEnter{
Name: name,
Pos: int32(pos),
},
}
return ygopropb.YgoStocMsg{
Msg: &msg,
}
}
// +++++ Util Functions +++++ // +++++ Util Functions +++++
func strToUtf16Buffer(s string) []uint16 { func strToUtf16Buffer(s string) []uint16 {
......
...@@ -17,6 +17,7 @@ const PROXY_PORT = ":3344" ...@@ -17,6 +17,7 @@ const PROXY_PORT = ":3344"
const CHANNEL_SIZE = 0x1000 const CHANNEL_SIZE = 0x1000
const BUFFER_SIZE = 0x1000 const BUFFER_SIZE = 0x1000
const TIME_OUT = 5 const TIME_OUT = 5
const COMPONENT = "[proxy]"
var upgrader = websocket.Upgrader{ var upgrader = websocket.Upgrader{
ReadBufferSize: 0x1000, ReadBufferSize: 0x1000,
...@@ -24,7 +25,7 @@ var upgrader = websocket.Upgrader{ ...@@ -24,7 +25,7 @@ var upgrader = websocket.Upgrader{
} }
func ygoEndpoint(w http.ResponseWriter, r *http.Request) { func ygoEndpoint(w http.ResponseWriter, r *http.Request) {
defer log.Println("ygoEndpoint finished") defer log.Println(COMPONENT + "ygoEndpoint finished")
upgrader.CheckOrigin = wsChecker upgrader.CheckOrigin = wsChecker
...@@ -33,14 +34,14 @@ func ygoEndpoint(w http.ResponseWriter, r *http.Request) { ...@@ -33,14 +34,14 @@ func ygoEndpoint(w http.ResponseWriter, r *http.Request) {
log.Fatal(err) log.Fatal(err)
} }
log.Println("Connection to ws://localhost" + TARGET_PORT + " [websocket] succeeded!") log.Println(COMPONENT + "Connection to ws://localhost" + TARGET_PORT + " [websocket] succeeded!")
tcp, err := net.Dial("tcp", "127.0.0.1"+PROXY_PORT) tcp, err := net.Dial("tcp", "127.0.0.1"+PROXY_PORT)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
log.Println("Connection to " + "12.0.0.1" + PROXY_PORT + " [tcp] succeeded!") log.Println(COMPONENT + "Connection to " + "12.0.0.1" + PROXY_PORT + " [tcp] succeeded!")
wsCh := make(chan []byte, CHANNEL_SIZE) wsCh := make(chan []byte, CHANNEL_SIZE)
tcpCh := make(chan []byte, CHANNEL_SIZE) tcpCh := make(chan []byte, CHANNEL_SIZE)
...@@ -90,7 +91,7 @@ func wsProxy(ws *websocket.Conn, Ch chan<- []byte, stopCh <-chan bool) { ...@@ -90,7 +91,7 @@ func wsProxy(ws *websocket.Conn, Ch chan<- []byte, stopCh <-chan bool) {
for { for {
select { select {
case _, ok := <-stopCh: case _, ok := <-stopCh:
log.Println("wsProxy recv stop singal, exit. channel closed: ", ok) log.Println(COMPONENT+"wsProxy recv stop singal, exit. channel closed: ", ok)
return return
default: default:
// if err := ws.SetReadDeadline(time.Now().Add(time.Second * TIME_OUT)); err != nil { // if err := ws.SetReadDeadline(time.Now().Add(time.Second * TIME_OUT)); err != nil {
...@@ -109,7 +110,7 @@ func wsProxy(ws *websocket.Conn, Ch chan<- []byte, stopCh <-chan bool) { ...@@ -109,7 +110,7 @@ func wsProxy(ws *websocket.Conn, Ch chan<- []byte, stopCh <-chan bool) {
} }
if messageType == websocket.CloseMessage { if messageType == websocket.CloseMessage {
log.Println("Websocket closed") log.Println(COMPONENT + "Websocket closed")
return return
} }
...@@ -134,7 +135,7 @@ func tcpProxy(tcp net.Conn, Ch chan<- []byte, stopCh <-chan bool) { ...@@ -134,7 +135,7 @@ func tcpProxy(tcp net.Conn, Ch chan<- []byte, stopCh <-chan bool) {
for { for {
select { select {
case _, ok := <-stopCh: case _, ok := <-stopCh:
log.Println("tcpProxy recv stop singal, exit. channel closed: ", ok) log.Println(COMPONENT+"tcpProxy recv stop singal, exit. channel closed: ", ok)
return return
default: default:
if err := tcp.SetReadDeadline(time.Now().Add(time.Second * TIME_OUT)); err != nil { if err := tcp.SetReadDeadline(time.Now().Add(time.Second * TIME_OUT)); err != nil {
...@@ -178,5 +179,7 @@ func main() { ...@@ -178,5 +179,7 @@ func main() {
setupRoutes() setupRoutes()
log.Println(COMPONENT + "start listening on ws://localhost:" + TARGET_PORT)
log.Fatal(http.ListenAndServe(TARGET_PORT, nil)) log.Fatal(http.ListenAndServe(TARGET_PORT, nil))
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment