-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShortRequestJob.go
53 lines (49 loc) · 1.09 KB
/
ShortRequestJob.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
package gserver
import (
"context"
"fmt"
"github.com/gw123/gserver/contracts"
"github.com/pkg/errors"
"math/rand"
"net"
"time"
)
type ShortConnJob struct {
ctx context.Context
timeout time.Duration
timer *time.Timer
cancelFunc context.CancelFunc
stopFlag bool
conn net.Conn
}
func NewShortConnJob(conn net.Conn, timeout time.Duration, ctx context.Context) *ShortConnJob {
ctx, cancelFunc := context.WithTimeout(ctx, timeout)
requestJob := &ShortConnJob{
timeout: timeout,
ctx: ctx,
cancelFunc: cancelFunc,
conn: conn,
}
return requestJob
}
func (job *ShortConnJob) Run(ctx context.Context) error {
if job.conn == nil {
return errors.New("conn is nil")
}
defer job.conn.Close()
packer := NewDataPack()
msg, err := packer.UnPackFromConn(job.conn)
if err != nil {
return err
}
msg = contracts.NewMsg(1, []byte("server"))
buf, err := packer.Pack(msg)
if err != nil {
fmt.Println("net.Dial 连接错误: ", err)
return err
}
sleep := rand.Int31n(6)
time.Sleep(time.Duration(sleep) * time.Second)
job.conn.Write(buf)
return err
}