Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
G
getty
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wei.xuan
getty
Commits
3d5349a9
Commit
3d5349a9
authored
Aug 23, 2016
by
alexstocks
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
init
parents
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
79 additions
and
0 deletions
+79
-0
utils.go
common/utils.go
+79
-0
No files found.
common/utils.go
0 → 100644
View file @
3d5349a9
/******************************************************
# DESC : getty utility
# AUTHOR : Alex Stocks
# LICENCE : Apache License 2.0
# EMAIL : alexstocks@foxmail.com
# MOD : 2016-08-22 17:44
# FILE : utils.go
******************************************************/
package
getty
import
(
"bytes"
"encoding/binary"
"net"
"strconv"
"time"
)
func
HostAddress
(
host
string
,
port
int
)
string
{
return
net
.
JoinHostPort
(
host
,
strconv
.
Itoa
(
port
))
}
func
dial
(
addr
string
)
(
net
.
Conn
,
error
)
{
return
net
.
Dial
(
"tcp"
,
addr
)
}
func
dialTimeout
(
addr
string
,
timeout
time
.
Duration
)
(
net
.
Conn
,
error
)
{
return
net
.
DialTimeout
(
"tcp"
,
addr
,
timeout
)
}
////////////////////////////////////////
// enc/dec
////////////////////////////////////////
func
Int2Bytes
(
x
int32
)
[]
byte
{
var
buf
=
bytes
.
NewBuffer
([]
byte
{})
binary
.
Write
(
buf
,
binary
.
BigEndian
,
x
)
return
buf
.
Bytes
()
}
func
Bytes2Int
(
b
[]
byte
)
int32
{
var
(
x
int32
buf
*
bytes
.
Buffer
)
buf
=
bytes
.
NewBuffer
(
b
)
binary
.
Read
(
buf
,
binary
.
BigEndian
,
&
x
)
return
x
}
type
GettyHeader
struct
{
Magic
int32
Len
int32
// body length
LogId
int32
// log id
ServiceId
int32
// service id
Code
int32
// error code
Extra
int32
// reserved
}
func
(
this
*
GettyHeader
)
Bytes
()
[]
byte
{
var
buf
=
bytes
.
NewBuffer
(
make
([]
byte
,
24
))
binary
.
Write
(
buf
,
binary
.
BigEndian
,
this
.
Magic
)
binary
.
Write
(
buf
,
binary
.
BigEndian
,
this
.
Len
)
binary
.
Write
(
buf
,
binary
.
BigEndian
,
this
.
LogId
)
binary
.
Write
(
buf
,
binary
.
BigEndian
,
this
.
ServiceId
)
binary
.
Write
(
buf
,
binary
.
BigEndian
,
this
.
Code
)
binary
.
Write
(
buf
,
binary
.
BigEndian
,
this
.
Extra
)
return
buf
.
Bytes
()
}
type
WrappedPackage
struct
{
H
GettyHeader
Payload
[]
byte
}
func
(
this
*
WrappedPackage
)
PackMessage
()
[]
byte
{
return
append
(
this
.
H
.
Bytes
(),
this
.
Payload
...
)
}
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