github.com/artyom/thrift@v0.0.0-20130902103359-388840a05deb/server_socket.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one
     3   * or more contributor license agreements. See the NOTICE file
     4   * distributed with this work for additional information
     5   * regarding copyright ownership. The ASF licenses this file
     6   * to you under the Apache License, Version 2.0 (the
     7   * "License"); you may not use this file except in compliance
     8   * with the License. You may obtain a copy of the License at
     9   *
    10   *   http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing,
    13   * software distributed under the License is distributed on an
    14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   * KIND, either express or implied. See the License for the
    16   * specific language governing permissions and limitations
    17   * under the License.
    18   */
    19  
    20  package thrift
    21  
    22  import (
    23  	"net"
    24  	"time"
    25  )
    26  
    27  type TServerSocket struct {
    28  	listener      net.Listener
    29  	addr          net.Addr
    30  	clientTimeout time.Duration
    31  	interrupted   bool
    32  }
    33  
    34  func NewTServerSocket(listenAddr string) (*TServerSocket, error) {
    35  	return NewTServerSocketTimeout(listenAddr, 0)
    36  }
    37  
    38  func NewTServerSocketTimeout(listenAddr string, clientTimeout time.Duration) (*TServerSocket, error) {
    39  	addr, err := net.ResolveTCPAddr("tcp", listenAddr)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  	return &TServerSocket{addr: addr, clientTimeout: clientTimeout}, nil
    44  }
    45  
    46  func (p *TServerSocket) Listen() error {
    47  	if p.IsListening() {
    48  		return nil
    49  	}
    50  	l, err := net.Listen(p.addr.Network(), p.addr.String())
    51  	if err != nil {
    52  		return err
    53  	}
    54  	p.listener = l
    55  	return nil
    56  }
    57  
    58  func (p *TServerSocket) Accept() (TTransport, error) {
    59  	if p.interrupted {
    60  		return nil, errTransportInterrupted
    61  	}
    62  	if p.listener == nil {
    63  		return nil, NewTTransportException(NOT_OPEN, "No underlying server socket")
    64  	}
    65  	conn, err := p.listener.Accept()
    66  	if err != nil {
    67  		return nil, NewTTransportExceptionFromError(err)
    68  	}
    69  	return NewTSocketFromConnTimeout(conn, p.clientTimeout), nil
    70  }
    71  
    72  // Checks whether the socket is listening.
    73  func (p *TServerSocket) IsListening() bool {
    74  	return p.listener != nil
    75  }
    76  
    77  // Connects the socket, creating a new socket object if necessary.
    78  func (p *TServerSocket) Open() error {
    79  	if p.IsListening() {
    80  		return NewTTransportException(ALREADY_OPEN, "Server socket already open")
    81  	}
    82  	if l, err := net.Listen(p.addr.Network(), p.addr.String()); err != nil {
    83  		return err
    84  	} else {
    85  		p.listener = l
    86  	}
    87  	return nil
    88  }
    89  
    90  func (p *TServerSocket) Addr() net.Addr {
    91  	return p.addr
    92  }
    93  
    94  func (p *TServerSocket) Close() error {
    95  	defer func() {
    96  		p.listener = nil
    97  	}()
    98  	if p.IsListening() {
    99  		return p.listener.Close()
   100  	}
   101  	return nil
   102  }
   103  
   104  func (p *TServerSocket) Interrupt() error {
   105  	p.interrupted = true
   106  	return nil
   107  }