diff --git a/connect.go b/connect.go index 405fcf5e..4c1e730c 100644 --- a/connect.go +++ b/connect.go @@ -166,6 +166,7 @@ func (c *Connection) runClient() error { if !c.Debug { c.bars = make([]*uiprogress.Bar, c.NumberOfConnections) } + gotTimeout := false gotOK := false gotResponse := false gotConnectionInUse := false @@ -201,8 +202,13 @@ func (c *Connection) runClient() error { if c.IsSender { // this is a sender logger.Debug("waiting for ok from relay") message = receiveMessage(connection) + if message == "timeout" { + gotTimeout = true + fmt.Println("You've just exceeded limit waiting time.") + return + } if message == "no" { - if id == 0 { + if id == 0 { fmt.Println("The specifed code is already in use by a sender.") } gotConnectionInUse = true @@ -223,7 +229,7 @@ func (c *Connection) runClient() error { logger.Debug("waiting for meta data from sender") message = receiveMessage(connection) if message == "no" { - if id == 0 { + if id == 0 { fmt.Println("The specifed code is already in use by a sender.") } gotConnectionInUse = true @@ -291,7 +297,10 @@ func (c *Connection) runClient() error { } if c.IsSender { - // TODO: Add confirmation + if gotTimeout { + fmt.Println("Timeout waiting for receiver") + return nil + } fmt.Println("\nFile sent.") } else { // Is a Receiver if notPresent { diff --git a/relay.go b/relay.go index f28c35fd..12921e3b 100644 --- a/relay.go +++ b/relay.go @@ -12,6 +12,7 @@ import ( ) const MAX_NUMBER_THREADS = 8 +const CONNECTION_TIMEOUT = time.Hour type connectionMap struct { receiver map[string]net.Conn @@ -135,7 +136,12 @@ func (r *Relay) clientCommuncation(id int, connection net.Conn) { r.connections.Unlock() // wait for receiver receiversAddress := "" + isTimeout := time.Duration(0) for { + if CONNECTION_TIMEOUT <= isTimeout { + sendMessage("timeout", connection) + break + } r.connections.RLock() if _, ok := r.connections.receiver[key]; ok { receiversAddress = r.connections.receiver[key].RemoteAddr().String() @@ -145,6 +151,7 @@ func (r *Relay) clientCommuncation(id int, connection net.Conn) { } r.connections.RUnlock() time.Sleep(100 * time.Millisecond) + isTimeout += 100 * time.Millisecond } logger.Debug("telling sender ok") sendMessage(receiversAddress, connection)