diff --git a/client.go b/client.go index cd1def6..3a1b94b 100644 --- a/client.go +++ b/client.go @@ -197,14 +197,13 @@ func (r* ClientRoute) ConnectToPeer(pts_id uint32) { if err != nil { // TODO: make send peer started failure mesage? fmt.Printf ("failed to connect to %s - %s\n", r.peer_addr.String(), err.Error()) - return + goto peer_aborted } real_conn, ok = conn.(*net.TCPConn) if !ok { fmt.Printf("not tcp connection - %s\n", err.Error()) - conn.Close() - return + goto peer_aborted } ptc, err = r.AddNewClientPeerConn(real_conn, pts_id) @@ -212,19 +211,27 @@ func (r* ClientRoute) ConnectToPeer(pts_id uint32) { // TODO: logging // TODO: make send peer started failure mesage? fmt.Printf("YYYYYYYY - %s\n", err.Error()) - conn.Close() - return + goto peer_aborted } fmt.Printf("STARTED NEW SERVER PEER STAK\n") err = r.cts.psc.Send(MakePeerStartedPacket(r.id, ptc.conn_id)) if err != nil { fmt.Printf("CLOSING NEW SERVER PEER STAK - %s\n", err.Error()) - conn.Close() - return + goto peer_aborted } r.ptc_wg.Add(1) go ptc.RunTask(&r.ptc_wg) + return + +peer_aborted: + if conn != nil { + conn.Close() + err = r.cts.psc.Send(MakePeerAbortedPacket(r.id, ptc.conn_id)) + if err != nil { + // TODO: logging + } + } } func (r* ClientRoute) DisconnectFromPeer(pts_id uint32) error { @@ -268,6 +275,8 @@ func (r* ClientRoute) ReportEvent (pts_id uint32, event_type PACKET_KIND, event_ fmt.Printf ("GOT PEER STARTD . CONENCT TO CLIENT_SIDE PEER\n") r.ConnectToPeer(pts_id) + case PACKET_KIND_PEER_ABORTED: + fallthrough case PACKET_KIND_PEER_STOPPED: fmt.Printf ("GOT PEER STOPPED . DISCONNECTION FROM CLIENT_SIDE PEER\n") err = r.DisconnectFromPeer(pts_id) diff --git a/hodu.proto b/hodu.proto index c02d368..a415f3f 100644 --- a/hodu.proto +++ b/hodu.proto @@ -46,8 +46,9 @@ enum PACKET_KIND { ROUTE_STOPPED = 5; PEER_STARTED = 6; PEER_STOPPED = 7; - PEER_EOF = 8; - PEER_DATA = 9; + PEER_ABORTED = 8; + PEER_EOF = 9; + PEER_DATA = 10; }; message Packet { diff --git a/packet.go b/packet.go index 2dec5b1..4af9e07 100644 --- a/packet.go +++ b/packet.go @@ -20,27 +20,33 @@ func MakeRouteStoppedPacket(route_id uint32, proto ROUTE_PROTO) *Packet { } -func MakePeerStartedPacket(route_id uint32, pts_id uint32) *Packet { +func MakePeerStartedPacket(route_id uint32, peer_id uint32) *Packet { // the connection from a peer to the server has been established return &Packet{Kind: PACKET_KIND_PEER_STARTED, - U: &Packet_Peer{Peer: &PeerDesc{RouteId: route_id, PeerId: pts_id}}, + U: &Packet_Peer{Peer: &PeerDesc{RouteId: route_id, PeerId: peer_id}}, } } -func MakePeerStoppedPacket(route_id uint32, pts_id uint32) *Packet { +func MakePeerStoppedPacket(route_id uint32, peer_id uint32) *Packet { return &Packet{Kind: PACKET_KIND_PEER_STOPPED, - U: &Packet_Peer{Peer: &PeerDesc{RouteId: route_id, PeerId: pts_id}, + U: &Packet_Peer{Peer: &PeerDesc{RouteId: route_id, PeerId: peer_id}, }} } -func MakePeerEofPacket(route_id uint32, pts_id uint32) *Packet { +func MakePeerAbortedPacket(route_id uint32, peer_id uint32) *Packet { + return &Packet{Kind: PACKET_KIND_PEER_ABORTED, + U: &Packet_Peer{Peer: &PeerDesc{RouteId: route_id, PeerId: peer_id}, + }} +} + +func MakePeerEofPacket(route_id uint32, peer_id uint32) *Packet { return &Packet{Kind: PACKET_KIND_PEER_EOF, - U: &Packet_Peer{Peer: &PeerDesc{RouteId: route_id, PeerId: pts_id}, + U: &Packet_Peer{Peer: &PeerDesc{RouteId: route_id, PeerId: peer_id}, }} } -func MakePeerDataPacket(route_id uint32, pts_id uint32, data []byte) *Packet { +func MakePeerDataPacket(route_id uint32, peer_id uint32, data []byte) *Packet { return &Packet{Kind: PACKET_KIND_PEER_DATA, - U: &Packet_Data{Data: &PeerData{RouteId: route_id, PeerId: pts_id, Data: data}, + U: &Packet_Data{Data: &PeerData{RouteId: route_id, PeerId: peer_id, Data: data}, }} } diff --git a/server.go b/server.go index a38b64b..f7bca85 100644 --- a/server.go +++ b/server.go @@ -407,6 +407,8 @@ fmt.Printf ("grpd stream ended\n") // TODO } + case PACKET_KIND_PEER_ABORTED: + fallthrough case PACKET_KIND_PEER_STOPPED: // the connection from the client to a peer has been established var x *Packet_Peer