Exemplo n.º 1
0
// ServeUnix ...
func (service *UnixService) ServeUnix(conn *net.UnixConn) (err error) {
	if service.readBuffer != nil {
		if err = conn.SetReadBuffer(service.readBuffer.(int)); err != nil {
			return err
		}
	}
	if service.writeBuffer != nil {
		if err = conn.SetWriteBuffer(service.writeBuffer.(int)); err != nil {
			return err
		}
	}
	return ((*StreamService)(service)).Serve(conn)
}
Exemplo n.º 2
0
func (s *Server) serveConn(in, out *net.UnixConn) {
	defer in.Close()
	defer out.Close()

	go func() {
		for {
			vals := []int{}
			incomingMu.Lock()
			for k := range incoming {
				vals = append(vals, k)
			}
			incomingMu.Unlock()
			sort.Ints(vals)
			log.Infof("the server: values still being waited on: %#v", vals)
			time.Sleep(5 * time.Second)
		}
	}()

	inBuff := bufio.NewReader(in)
	for {
		msg := &ClientMsg{}

		select {
		case err := <-s.decoder(msg, inBuff):
			if err != nil {
				log.Errorf("problem with client message: %s", err)
				continue
			}
		case <-time.After(30 * time.Second):
			return
		}

		incomingMu.Lock()
		incoming[int(msg.ID)] = true
		incomingMu.Unlock()

		if msg.Type == ClientKeepAlive {
			log.Infof("%#v", msg)
			continue
		}

		log.Infof("server received msg.ID = %d", msg.ID)
		reg, ok := s.registry[msg.Handler]
		if !ok {
			log.Infof("can not locate Handler %q", msg.Handler)
			continue
		}

		go func(msg *ClientMsg, reg *register) {
			srvMsg, err := reg.handler(msg)
			if err != nil {
				srvMsg.ID = msg.ID
				srvMsg.Data = []byte(fmt.Sprintf("handler(%s) error: %s", msg.Handler, err))
				reg.Lock()
				defer reg.Unlock()
				log.Infof("server: writing error response to client for ID: %d", msg.ID)
				if err := srvMsg.Encode(out); err != nil {
					log.Infof("cannot write to the client: %s", err)
				}
				return
			}
			reg.Lock()
			defer reg.Unlock()
			log.Infof("server: writing response to client for ID: %d", msg.ID)
			for {
				if err := srvMsg.Encode(out); err != nil {
					log.Infof("cannot write to the client: %s", err)
					s.bufferSize += serverMsgHeader + len(srvMsg.Data)
					if err := out.SetWriteBuffer(s.bufferSize); err != nil {
						log.Infof("cannot extend the write buffer, call will fail: %s", err)
						srvMsg = &ServerMsg{ID: msg.ID, Data: []byte("buffer cannot be made large enough to hold output message")}
						if err := srvMsg.Encode(out); err != nil {
							// TODO(johnsiilver): make this an exponential backoff with a time limit on iterations.
							log.Infof("can't send error message")
							continue
						}
						break
					}
					continue
				}
				break
			}
			incomingMu.Lock()
			delete(incoming, int(msg.ID))
			incomingMu.Unlock()
		}(msg, reg)
	}
}