-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove the open method from transports (#31)
The original design with the Open method and the possibility to postpone opening a connection to a Flume endpoint looks not so useful now and just produces many additional and unnecessary code. Removing such feature simplifies the code and makes it more clear.
- Loading branch information
Showing
8 changed files
with
49 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,29 @@ | ||
package transports | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"time" | ||
) | ||
|
||
type socket struct { | ||
conn net.Conn | ||
addr net.Addr | ||
timeout time.Duration | ||
net.Conn | ||
} | ||
|
||
func NewSocket(hostPort string) (Transport, error) { | ||
return NewSocketTimeout(hostPort, 0) | ||
} | ||
|
||
func NewSocketTimeout(hostPort string, timeout time.Duration) (Transport, error) { | ||
func NewSocket(hostPort string, timeout time.Duration) (Transport, error) { | ||
addr, err := net.ResolveTCPAddr("tcp", hostPort) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &socket{addr: addr, timeout: timeout}, nil | ||
} | ||
|
||
// Connects the socket, creating a new socket object if necessary. | ||
func (s *socket) Open() error { | ||
if s.conn != nil { | ||
return fmt.Errorf("already open") | ||
} | ||
|
||
conn, err := net.DialTimeout(s.addr.Network(), s.addr.String(), s.timeout) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
s.conn = conn | ||
|
||
return nil | ||
} | ||
|
||
func (s *socket) Close() error { | ||
if s.conn == nil { | ||
return nil | ||
} | ||
|
||
err := s.conn.Close() | ||
s := &socket{} | ||
s.Conn, err = net.DialTimeout(addr.Network(), addr.String(), timeout) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
s.conn = nil | ||
|
||
return nil | ||
} | ||
|
||
func (s *socket) Read(buf []byte) (int, error) { | ||
if s.conn == nil { | ||
return 0, fmt.Errorf("not open") | ||
} | ||
|
||
n, err := s.conn.Read(buf) | ||
fmt.Printf("R[%04d]: %X\n", n, buf[:n]) | ||
return n, err | ||
} | ||
|
||
func (s *socket) Write(buf []byte) (int, error) { | ||
if s.conn == nil { | ||
return 0, fmt.Errorf("not open") | ||
return nil, err | ||
} | ||
|
||
n, err := s.conn.Write(buf) | ||
fmt.Printf("W[%04d]: %X\n", n, buf) | ||
return n, err | ||
return s, nil | ||
} | ||
|
||
func (s *socket) Flush() error { | ||
return nil | ||
} | ||
|
||
func (s *socket) SetDeadline(d time.Time) error { | ||
return s.conn.SetDeadline(d) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.