Skip to content

Commit

Permalink
simplify the code
Browse files Browse the repository at this point in the history
  • Loading branch information
soyum2222 committed Apr 23, 2021
1 parent 51338b1 commit 0a7aea9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
20 changes: 19 additions & 1 deletion handshack.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import (

func firstHandShack(h *headquarters, remote net.Addr) {

_, ok := h.Snipers.Load(remote.String())
if ok {
return
}

sn := NewSniper(h.conn, remote.(*net.UDPAddr))

h.Snipers.Store(remote.String(), sn)
Expand All @@ -30,6 +35,7 @@ func firstHandShack(h *headquarters, remote net.Addr) {
return
}

sn.status = STATUS_SECONDHANDSHACK
_, _ = sn.conn.WriteToUDP(protocol.Marshal(ammo), sn.aim)

select {
Expand All @@ -51,6 +57,13 @@ func secondHandShack(h *headquarters, remote net.Addr) {

sn := i.(*Sniper)

// verification status
//if sn.status != STATUS_FIRSTHANDSHACK {
// return
//}

sn.status = STATUS_THIRDHANDSHACK

ammo := protocol.Ammo{
Id: 0,
Kind: protocol.THIRDHANDSHACK,
Expand All @@ -68,12 +81,17 @@ func thirdHandShack(h *headquarters, remote net.Addr) {

sn := i.(*Sniper)

//if sn.status != STATUS_SECONDHANDSHACK {
// return
//}

sn.status = STATUS_NORMAL

SystemTimedSched.Put(sn.healthMonitor, time.Now().Add(time.Second*3))

select {
case sn.handShakeSign <- struct{}{}:
default:

}

h.accept <- sn
Expand Down
28 changes: 6 additions & 22 deletions headquarters.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package sharpshooter

import (
"context"
"encoding/binary"
"errors"
"github.com/soyum2222/sharpshooter/protocol"
Expand Down Expand Up @@ -44,32 +43,16 @@ func Dial(addr string) (net.Conn, error) {
sn := NewSniper(conn, udpaddr)
sn.noLeader = true

c := make(chan error, 2)
c := make(chan error, 1)

go func() {

secondhand := make([]byte, 14)

ctx, _ := context.WithTimeout(context.Background(), time.Second*6)

select {

case <-func() chan struct{} {
ch := make(chan struct{})

go func() {
_, err = sn.conn.Read(secondhand)
if err != nil {
c <- err
}
close(ch)
}()

return ch
}():

case <-ctx.Done():
_ = conn.Close()
_, err = sn.conn.Read(secondhand)
if err != nil {
c <- err
return
}

ammo, err := protocol.Unmarshal(secondhand)
Expand All @@ -90,6 +73,7 @@ func Dial(addr string) (net.Conn, error) {
loop:

if i > 6 {
_ = conn.Close()
_ = sn.Close()
return nil, errors.New("dial timeout")
}
Expand Down
10 changes: 10 additions & 0 deletions sniper.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ const (
DEFAULT_INIT_INTERVAL = 500
)

const (
STATUS_SECONDHANDSHACK = iota
STATUS_THIRDHANDSHACK
STATUS_NORMAL
STATUS_CLOSEING1
STATUS_CLOSEING2
STATUS_CLOSEING3
)

var (
CLOSEERROR = errors.New("the connection is closed")
HEALTHTIMEOUTERROR = errors.New("health monitor timeout ")
Expand All @@ -52,6 +61,7 @@ type Sniper struct {
minSize int32
healthTryCount int32
shootStatus int32
status int32
sendId uint32
rcvId uint32
sendWinId uint32
Expand Down

0 comments on commit 0a7aea9

Please sign in to comment.