Ejemplo n.º 1
0
// handle each connection as a thread, say Hello, then receive data:
func connHandler(conn net.Conn) {
	connFrom := conn.RemoteAddr().String()
	println("Connection from: ", connFrom)
	sayHello(conn)
	for {
		// create an input buffer to store received data:
		var ibuf []byte = make([]byte, maxRead + 1)
		// store 25-bytes of received data in the buffer:
		length, err := conn.Read(ibuf[0:maxRead])
		// set last byte to prevent aginst buffer overflow:
		ibuf[maxRead] = 0

		// error handeling when reading data from client:
		switch err {
		case nil:
			handleMsg(length, err, ibuf)
		case os.EAGAIN:	// try again
			continue
		case default:
			goto DISCONNECT
		}	
	}

	DISCONNECT:
		err := conn.Close()
		println("Closed connection: ", connFrom)
		checkError(err, "Close: ")
}
Ejemplo n.º 2
0
//walk through child directories and files
func DoAnalysis(path, ignoreList, includeList, ServPath string, conn net.Conn) {
	Init(path, ignoreList, includeList)
	fmt.Println(path, ignoreList, includeList, g_RootName, g_IgnoreList, g_IncludeList)
	go func() {
		defer close(g_Ch)
		filepath.Walk(path, walkFunc)
	}()

	list := make([]*protocol.ClientFInfo, 0, 8)
	//pack informations
	for v := range g_Ch {
		//gloger.GetLoger().Printf("%s %d\n", v.SrcPath, v.ModTime)

		v.TarPath = filepath.Join(ServPath, v.TarPath)

		if runtime.GOOS == "windows" {
			v.TarPath = strings.Replace(v.TarPath, "\\", "/", -1)
		}

		list = append(list, v)
	}

	size := len(list)
	if size == 0 {
		fmt.Printf("Path: %s no files\n", path)
		conn.Close()
		os.Exit(0)
	} else {
		for i := 0; i < size; i++ {
			fi := list[i]
			fi.TotalFileNum = uint32(size)
			buff := msghandler.Marshal(uint16(msghandler.C2S_FINFO), fi)
			_, err := conn.Write(buff)
			if err != nil {
				panic(fmt.Sprintf("connection write error: %s", err))
			}
		}
	}
}
Ejemplo n.º 3
0
//----------------------------------------------- receive message from hub
func HubReceiver(conn net.Conn) {
	defer conn.Close()

	header := make([]byte, 2)
	seq_id := make([]byte, 8)

	for {
		// header
		n, err := io.ReadFull(conn, header)
		if n == 0 && err == io.EOF {
			break
		} else if err != nil {
			log.Println("error receving header:", err)
			break
		}

		// packet seq_id uint32
		n, err = io.ReadFull(conn, seq_id)
		if n == 0 && err == io.EOF {
			break
		} else if err != nil {
			log.Println("error receving seq_id:", err)
			break
		}

		seqval := uint64(0)

		for k, v := range seq_id {
			seqval |= uint64(v) << uint((7-k)*8)
		}

		// data
		size := int(header[0])<<8 | int(header[1]) - 8
		data := make([]byte, size)
		n, err = io.ReadFull(conn, data)

		if err != nil {
			log.Println("error receving msg:", err)
			break
		}

		if seqval == 0 { // packet forwarding, deliver to MQ
			reader := packet.Reader(data)
			forward_id, err := reader.ReadS32()
			if err != nil {
				log.Println("packet forwarding error")
				goto L
			}

			sess := Query(forward_id)
			if sess == nil {
				log.Println("forward failed, maybe user is offline?")
			} else {
				func() {
					defer func() {
						if x := recover(); x != nil {
							log.Println("forward to MQ failed, the user is so lucky")
						}
					}()
					sess.MQ <- data[reader.Pos():] // the payload is the message
				}()
			}
		} else {
			_wait_ack_lock.Lock()
			if ack, ok := _wait_ack[seqval]; ok {
				ack <- data
				delete(_wait_ack, seqval)
			} else {
				log.Println("Illegal packet sequence number from HUB")
			}
			_wait_ack_lock.Unlock()
		}
	L:
	}
}