mirror of
https://github.com/amigan/aim-oscar-server.git
synced 2024-11-21 20:19:47 -05:00
58 lines
1.1 KiB
Go
58 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding"
|
|
"fmt"
|
|
"net"
|
|
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type sessionKey string
|
|
|
|
func (s sessionKey) String() string {
|
|
return "oscar-" + string(s)
|
|
}
|
|
|
|
var (
|
|
currentSession = sessionKey("session")
|
|
)
|
|
|
|
type Session struct {
|
|
Conn net.Conn
|
|
SequenceNumber uint16
|
|
GreetedClient bool
|
|
}
|
|
|
|
func NewSession(conn net.Conn) *Session {
|
|
return &Session{
|
|
Conn: conn,
|
|
SequenceNumber: 0,
|
|
GreetedClient: false,
|
|
}
|
|
}
|
|
|
|
func NewContextWithSession(ctx context.Context, conn net.Conn) context.Context {
|
|
session := NewSession(conn)
|
|
return context.WithValue(ctx, currentSession, session)
|
|
}
|
|
|
|
func CurrentSession(ctx context.Context) (session *Session, err error) {
|
|
s := ctx.Value(currentSession)
|
|
if s == nil {
|
|
return nil, errors.New("no session in context")
|
|
}
|
|
return s.(*Session), nil
|
|
}
|
|
|
|
func (s *Session) Send(m encoding.BinaryMarshaler) error {
|
|
bytes, err := m.MarshalBinary()
|
|
if err != nil {
|
|
return errors.Wrap(err, "could not marshal message")
|
|
}
|
|
|
|
fmt.Printf("-> %v\n%s\n\n", s.Conn.RemoteAddr(), prettyBytes(bytes))
|
|
_, err = s.Conn.Write(bytes)
|
|
return errors.Wrap(err, "could not write to client connection")
|
|
}
|