Example #1
0
//Read UDP data, right after UDP data recieve from UDP Socket
func (socket *Socket) ReadUDP(rlen int, buf_receive []byte, remote *net.UDPAddr) bool {
	if rlen != UdpPocketLength {
		return false
	}
	buf := buf_receive[:rlen]
	var (
		converted    bool
		data_id      uint32
		pocket_index uint32
	)
	data_id, converted = lib.BytesToUint32(buf[0:4])
	if !converted {
		lib.Log("Unable to convert data_id from UDP")
		return false
	}
	//If socket incoming data doesn't contains data_id return
	if _, ok := socket.DataIn[data_id]; !ok {
		return false
	}
	socket.RemoteUDP = remote //if DataID is same so maybe Remote address will change
	//If UDP data received for this data_id return
	if socket.DataIn[data_id].StopUDP {
		return true
	}

	pocket_index, converted = lib.BytesToUint32(buf[4:8])
	if !converted {
		lib.Log("Unable to convert pocket_index from UDP")
		return true
	}

	//If all data received
	if len(socket.DataIn[data_id].DataStack) == int(socket.DataIn[data_id].PocketCount) {
		m := socket.DataIn[data_id]
		m.StopUDP = true
		socket.DataIn[data_id] = m
		var stop_data []byte
		stop_data = append(stop_data, buf[0:4]...)
		stop_data = append(stop_data, StopBytes...)
		socket.WriteTCP(stop_data)
		all_data := lib.CombineBytesMap(socket.DataIn[data_id].DataStack)
		go socket.DataReceived(all_data[:socket.DataIn[data_id].Length])
		delete(socket.DataIn, data_id)
		return true
	}

	//if index already received return
	if _, ok := socket.DataIn[data_id].DataStack[pocket_index]; ok {
		return true
	}
	if socket.DataIn[data_id].DataStack == nil {
		m := socket.DataIn[data_id]
		m.DataStack = make(map[uint32][]byte)
		socket.DataIn[data_id] = m
	}
	socket.DataIn[data_id].DataStack[pocket_index] = append(socket.DataIn[data_id].DataStack[pocket_index], buf[8:]...)
	return true
}
Example #2
0
//Crates MetaData from TCP received headers
func CreateMetaData(data []byte) (meta MetaData, converted bool) {
	converted = true
	meta.DataId, converted = lib.BytesToUint32(data[0:4])
	if !converted {
		lib.Log("Error Converting DataID !!")
		return
	}
	meta.Length, converted = lib.BytesToUint32(data[4:8])
	if !converted {
		lib.Log("Error Converting Length !!")
		return
	}
	meta.PocketCount = lib.CalcPocketCount(meta.Length, uint32(PocketLength))
	meta.StopUDP = false
	meta.DataStack = make(map[uint32][]byte)
	return
}
Example #3
0
//Reading Data from TCP connection, must be started concurrently and MAYBE WILL RUN IN EVENT LOOP
func (socket *Socket) ReadTCP() {
	buf_receive := make([]byte, 8) //If I'm typing 8 byte it's alwase giving EOF error from TCP
	for {
		if socket.closed {
			return
		}
		rlen, err := socket.TCPSocket.Read(buf_receive)
		if err != nil {
			socket.TCPSocket.Close()
			go socket.Disconnected(err)
			return
		}
		var buf []byte
		buf = append(buf, buf_receive[:rlen]...)
		if rlen != 8 {
			continue
		}

		data_id, converted := lib.BytesToUint32(buf[0:4])
		if !converted {
			lib.Log("Unable to convert data_id in TCP END DATA block")
			continue
		}
		if _, ok := socket.DataOut[data_id]; ok && string(buf[4:5]) == "$" {
			m := socket.DataOut[data_id]
			m.StopUDP = true
			socket.DataOut[data_id] = m
			continue
		}

		meta, converted := CreateMetaData(buf)
		if !converted {
			continue
		}
		socket.DataIn[meta.DataId] = meta
	}
}