X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/95159e5585762c06c654945070ba54262b7dcec9..09261d2b52fa739bb6321c866566223f11061201:/hotline/client.go diff --git a/hotline/client.go b/hotline/client.go index 33dbd0d..e300bb8 100644 --- a/hotline/client.go +++ b/hotline/client.go @@ -29,23 +29,24 @@ type Client struct { Connection net.Conn Logger *slog.Logger Pref *ClientPrefs - Handlers map[uint16]ClientHandler - activeTasks map[uint32]*Transaction + Handlers map[[2]byte]ClientHandler + activeTasks map[[4]byte]*Transaction + UserList []User } type ClientHandler func(context.Context, *Client, *Transaction) ([]Transaction, error) -func (c *Client) HandleFunc(transactionID uint16, handler ClientHandler) { - c.Handlers[transactionID] = handler +func (c *Client) HandleFunc(tranType [2]byte, handler ClientHandler) { + c.Handlers[tranType] = handler } func NewClient(username string, logger *slog.Logger) *Client { c := &Client{ Logger: logger, - activeTasks: make(map[uint32]*Transaction), - Handlers: make(map[uint16]ClientHandler), + activeTasks: make(map[[4]byte]*Transaction), + Handlers: make(map[[2]byte]ClientHandler), + Pref: &ClientPrefs{Username: username}, } - c.Pref = &ClientPrefs{Username: username} return c } @@ -79,12 +80,12 @@ func (c *Client) Connect(address, login, passwd string) (err error) { // Authenticate (send TranLogin 107) err = c.Send( - *NewTransaction( - TranLogin, nil, + NewTransaction( + TranLogin, [2]byte{0, 0}, NewField(FieldUserName, []byte(c.Pref.Username)), NewField(FieldUserIconID, c.Pref.IconBytes()), - NewField(FieldUserLogin, encodeString([]byte(login))), - NewField(FieldUserPassword, encodeString([]byte(passwd))), + NewField(FieldUserLogin, EncodeString([]byte(login))), + NewField(FieldUserPassword, EncodeString([]byte(passwd))), ), ) if err != nil { @@ -102,7 +103,7 @@ const keepaliveInterval = 300 * time.Second func (c *Client) keepalive() error { for { time.Sleep(keepaliveInterval) - _ = c.Send(*NewTransaction(TranKeepAlive, nil)) + _ = c.Send(NewTransaction(TranKeepAlive, [2]byte{})) } } @@ -119,8 +120,8 @@ var ServerHandshake = []byte{ } func (c *Client) Handshake() error { - // Protocol ID 4 ‘TRTP’ 0x54 52 54 50 - // Sub-protocol ID 4 User defined + // Protocol Type 4 ‘TRTP’ 0x54 52 54 50 + // Sub-protocol Type 4 User defined // Version 2 1 Currently 1 // Sub-version 2 User defined if _, err := c.Connection.Write(ClientHandshake); err != nil { @@ -142,11 +143,11 @@ func (c *Client) Handshake() error { } func (c *Client) Send(t Transaction) error { - requestNum := binary.BigEndian.Uint16(t.Type) + requestNum := binary.BigEndian.Uint16(t.Type[:]) // if transaction is NOT reply, add it to the list to transactions we're expecting a response for if t.IsReply == 0 { - c.activeTasks[binary.BigEndian.Uint32(t.ID)] = &t + c.activeTasks[t.ID] = &t } n, err := io.Copy(c.Connection, &t) @@ -165,16 +166,15 @@ func (c *Client) Send(t Transaction) error { func (c *Client) HandleTransaction(ctx context.Context, t *Transaction) error { var origT Transaction if t.IsReply == 1 { - requestID := binary.BigEndian.Uint32(t.ID) - origT = *c.activeTasks[requestID] + origT = *c.activeTasks[t.ID] t.Type = origT.Type } - if handler, ok := c.Handlers[binary.BigEndian.Uint16(t.Type)]; ok { + if handler, ok := c.Handlers[t.Type]; ok { c.Logger.Debug( "Received transaction", "IsReply", t.IsReply, - "type", binary.BigEndian.Uint16(t.Type), + "type", t.Type[:], ) outT, err := handler(ctx, c, t) if err != nil { @@ -185,12 +185,6 @@ func (c *Client) HandleTransaction(ctx context.Context, t *Transaction) error { return err } } - } else { - c.Logger.Debug( - "Unimplemented transaction type", - "IsReply", t.IsReply, - "type", binary.BigEndian.Uint16(t.Type), - ) } return nil