-
Notifications
You must be signed in to change notification settings - Fork 3
/
session.go
43 lines (37 loc) · 843 Bytes
/
session.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main
import (
"fmt"
"net"
"strings"
)
type Session struct {
conn net.Conn
id int64
uID byte
ipAddress string
}
func (s Session) WriteLong(i int64) {
s.WriteByte_(byte(i >> 56))
s.WriteByte_(byte(i >> 48))
s.WriteByte_(byte(i >> 40))
s.WriteByte_(byte(i >> 32))
s.WriteByte_(byte(i >> 24))
s.WriteByte_(byte(i >> 16))
s.WriteByte_(byte(i >> 8))
s.WriteByte_(byte(i & 0xFF))
}
func (s Session) WriteByte_(b byte) {
_, err := s.conn.Write([]byte{b})
if err != nil {
fmt.Println("Error writing byte(s) to client session.")
fmt.Println(err.Error())
}
}
func (s Session) String() string {
return s.ipAddress
}
func NewSession(conn net.Conn) Session {
s := Session{conn, -1, 0xFF, strings.Split(conn.RemoteAddr().String(), ":")[0]}
fmt.Printf("Accepting connection from: %s\n", s)
return s
}