Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
neo
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
2
Merge Requests
2
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Jobs
Commits
Open sidebar
Kirill Smelkov
neo
Commits
346454a3
Commit
346454a3
authored
Dec 27, 2016
by
Kirill Smelkov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
.
parent
261f9303
Changes
3
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
159 additions
and
121 deletions
+159
-121
t/neo/proto.go
t/neo/proto.go
+17
-85
t/neo/protogen.go
t/neo/protogen.go
+141
-35
t/neo/storage.go
t/neo/storage.go
+1
-1
No files found.
t/neo/proto.go
View file @
346454a3
This diff is collapsed.
Click to expand it.
t/neo/protogen.go
View file @
346454a3
...
...
@@ -22,19 +22,46 @@ import (
"go/ast"
"go/parser"
"go/token"
"go/types"
"log"
)
var
_
=
ast
.
Print
// information about one packet type
type
PacketType
struct
{
name
string
msgCode
uint16
// message code for this packet type - derived from type order number in source
}
var
fset
=
token
.
NewFileSet
()
var
info
=
&
types
.
Info
{
Types
:
make
(
map
[
ast
.
Expr
]
types
.
TypeAndValue
),
Uses
:
make
(
map
[
*
ast
.
Ident
]
types
.
Object
),
Defs
:
make
(
map
[
*
ast
.
Ident
]
types
.
Object
),
}
// complete position of a node
func
pos
(
n
ast
.
Node
)
{
return
fset
.
Position
(
n
.
Pos
())
}
func
main
()
{
fset
:=
token
.
NewFileSet
()
typeMap
:=
map
[
string
]
*
PacketType
{}
// XXX needed ?
// go through proto.go and collect packets type definitions
var
mode
parser
.
Mode
=
0
// parser.Trace
f
,
err
:=
parser
.
ParseFile
(
fset
,
"proto.go"
,
nil
,
mode
)
if
err
!=
nil
{
panic
(
err
)
// XXX log
log
.
Fatal
(
err
)
// parse error
}
conf
:=
types
.
Config
{}
pkg
,
err
:=
conf
.
Check
(
"proto"
,
fset
,
[]
*
ast
.
File
{
f
},
info
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
// typecheck error
}
ncode
:=
0
//ast.Print(fset, f)
...
...
@@ -42,30 +69,32 @@ func main() {
for
_
,
decl
:=
range
f
.
Decls
{
// we look for types (which can be only under GenDecl)
gdecl
,
ok
:=
decl
.
(
*
ast
.
GenDecl
)
if
!
ok
||
gdecl
.
Tok
!=
token
.
TYPE
{
g
en
decl
,
ok
:=
decl
.
(
*
ast
.
GenDecl
)
if
!
ok
||
g
en
decl
.
Tok
!=
token
.
TYPE
{
continue
}
for
_
,
spec
:=
range
gdecl
.
Specs
{
t
spec
:=
spec
.
(
*
ast
.
TypeSpec
)
// must be because tok = TYPE
t
name
:=
t
spec
.
Name
.
Name
for
_
,
spec
:=
range
g
en
decl
.
Specs
{
t
ypespec
:=
spec
.
(
*
ast
.
TypeSpec
)
// must be because tok = TYPE
t
ypename
:=
type
spec
.
Name
.
Name
switch
t
:=
typespec
.
Type
.
(
type
)
{
default
:
// we are only interested in struct types
tstruct
,
ok
:=
tspec
.
Type
.
(
*
ast
.
StructType
)
if
!
ok
{
continue
}
//fmt.Printf("\n%s:\n", tname)
//fmt.Println(tstruct)
//ast.Print(fset, tstruct)
case
*
ast
.
StructType
:
//fmt.Printf("\n%s:\n", typename)
//fmt.Println(t)
//ast.Print(fset, t)
if
ncode
!=
0
{
fmt
.
Println
()
}
PacketType
{
name
:
typename
,
msgCode
:
ncode
}
// if ncode != 0 {
// fmt.Println()
// }
for
_
,
fieldv
:=
range
tstruc
t
.
Fields
.
List
{
for
_
,
fieldv
:=
range
t
.
Fields
.
List
{
// we only support simple types like uint16
ftype
,
ok
:=
fieldv
.
Type
.
(
*
ast
.
Ident
)
if
!
ok
{
...
...
@@ -76,19 +105,96 @@ func main() {
if
len
(
fieldv
.
Names
)
!=
0
{
for
_
,
field
:=
range
fieldv
.
Names
{
fmt
.
Printf
(
"%s(%d).%s
\t
%s
\n
"
,
t
name
,
ncode
,
field
.
Name
,
ftype
)
fmt
.
Printf
(
"%s(%d).%s
\t
%s
\n
"
,
type
name
,
ncode
,
field
.
Name
,
ftype
)
}
}
else
{
// no names means embedding
fmt
.
Printf
(
"%s(%d).<%s>
\n
"
,
t
name
,
ncode
,
ftype
)
fmt
.
Printf
(
"%s(%d).<%s>
\n
"
,
type
name
,
ncode
,
ftype
)
}
}
ncode
++
}
}
//fmt.Println(gdecl)
//ast.Print(fset, gdecl)
}
}
// wiresize returns wire size of a type
// type must be of fixed size (e.g. not a slice or map)
// XXX ast.Expr -> ?
func
wiresize
(
*
ast
.
Expr
)
int
{
// TODO
}
func
gendecode
(
typespec
*
ast
.
TypeSpec
)
string
{
buf
:=
butes
.
Buffer
{}
typename
:=
typespec
.
Name
.
Name
t
:=
typespec
.
Type
.
(
*
ast
.
StructType
)
// must be
fmt
.
Fprintf
(
&
buf
,
"func (p *%s) NEODecode(data []byte) int {
\n
"
,
typename
)
n
:=
0
// current decode pos in data
for
_
,
fieldv
:=
t
.
Fields
.
List
{
// type B struct { ... }
//
// type A struct {
// x, y int <- fieldv
// B <- fieldv
// embedding: change `B` -> `B B` (field type must be Ident)
fieldnamev
:=
fieldv
.
Names
if
fieldnamev
==
nil
{
fieldnamev
=
[]
*
ast
.
Ident
{
fieldv
.
Type
.
(
*
ast
.
Ident
)}
}
for
fieldname
:=
range
fieldnamev
{
switch
fieldtype
:=
fieldv
.
Type
.
(
type
)
{
// we are processing: <fieldname> <fieldtype>
// simple types like uint16
case
*
ast
.
Ident
:
// TODO
// array or slice
case
*
ast
.
ArrayType
:
if
fieldtype
.
Len
!=
nil
{
log
.
Fatalf
(
"%s: TODO arrays not suported"
,
pos
(
fieldtype
))
}
eltsize
:=
wiresize
(
fieldtype
.
Elt
)
// TODO
// len u32
// [len] items
emit
(
"length = Uint32(data[%s:])"
,
n
)
n
+=
4
emit
(
"for ; length != 0; length-- {"
)
emit
(
"}"
)
// map
case
*
ast
.
MapType
:
// len u32
// [len] key, value
emit
(
"length = Uint32(data[%s:])"
,
n
)
n
+=
4
keysize
:=
wiresize
(
fieldtype
.
Key
)
valsize
:=
wiresize
(
fieldtype
.
Value
)
// XXX *ast.StructType ?
default
:
panic
()
// TODO
}
}
}
fmt
.
Fprintf
(
&
buf
,
"}
\n
"
)
// TODO format.Source(buf.Bytes()) (XXX -> better at top-level for whole file)
return
buf
.
String
()
}
t/neo/storage.go
View file @
346454a3
...
...
@@ -63,7 +63,7 @@ func (stor *StorageApplication) ServeLink(ctx context.Context, link *NodeLink) {
switch pkt.MsgCode {
case GetObject:
req := GetObject{}
err = req.Decode(pkt.Payload())
err = req.
NEO
Decode(pkt.Payload())
if err != nil {
sendErr("malformed GetObject packet:", err)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment