This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
conn.go
113 lines (90 loc) · 2.04 KB
/
conn.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package dummyGateway
import (
"github.com/ronaksoft/rony/errors"
"github.com/ronaksoft/rony/tools"
"mime/multipart"
"sync"
)
/*
Creation Time: 2020 - Oct - 02
Created by: (ehsan)
Maintainers:
1. Ehsan N. Moosa (E2)
Auditor: Ehsan N. Moosa (E2)
Copyright Ronak Software Group 2020
*/
type Conn struct {
id uint64
clientIP string
persistent bool
mtx sync.Mutex
kv map[string]interface{}
onMessage func(connID uint64, streamID int64, data []byte, hdr map[string]string)
// extra data for REST
status int
httpHdr map[string]string
method string
path string
body []byte
}
func (c *Conn) ReadHeader(key string) string {
return c.httpHdr[key]
}
func (c *Conn) Redirect(statusCode int, newHostPort string) {
// TODO:: implement it
}
func NewConn(onMessage func(connID uint64, streamID int64, data []byte, hdr map[string]string)) *Conn {
return &Conn{
id: tools.RandomUint64(0),
kv: map[string]interface{}{},
httpHdr: map[string]string{},
persistent: false,
onMessage: onMessage,
}
}
func (c *Conn) WriteStatus(status int) {
c.status = status
}
func (c *Conn) WriteHeader(key, value string) {
c.mtx.Lock()
defer c.mtx.Unlock()
c.httpHdr[key] = value
}
func (c *Conn) MultiPart() (*multipart.Form, error) {
return nil, errors.New(errors.NotImplemented, "Multipart")
}
func (c *Conn) Method() string {
return c.method
}
func (c *Conn) Path() string {
return c.path
}
func (c *Conn) Body() []byte {
return c.body
}
func (c *Conn) Get(key string) interface{} {
c.mtx.Lock()
defer c.mtx.Unlock()
return c.kv[key]
}
func (c *Conn) Set(key string, val interface{}) {
c.mtx.Lock()
defer c.mtx.Unlock()
c.kv[key] = val
}
func (c *Conn) ConnID() uint64 {
return c.id
}
func (c *Conn) ClientIP() string {
return c.clientIP
}
func (c *Conn) WriteBinary(streamID int64, data []byte) error {
c.onMessage(c.id, streamID, data, c.httpHdr)
return nil
}
func (c *Conn) Persistent() bool {
return c.persistent
}
func (c *Conn) SetPersistent(b bool) {
c.persistent = b
}