Exemple #1
0
func (fd *udtFD) setAsyncOpts() error {

	if C.udt_setsockopt(fd.sock, 0, C.UDT_RCVTIMEO, unsafe.Pointer(&UDT_RCVTIMEO_MS), C.sizeof_int) != 0 {
		return fmt.Errorf("failed to set UDT_RCVTIMEO: %s", lastError())
	}

	if C.udt_setsockopt(fd.sock, 0, C.UDT_SNDTIMEO, unsafe.Pointer(&UDT_SNDTIMEO_MS), C.sizeof_int) != 0 {
		return fmt.Errorf("failed to set UDT_SNDTIMEO: %s", lastError())
	}

	// full async is off

	// options
	// falseint := C.int(0)

	// // set sending to be async
	// if C.udt_setsockopt(fd.sock, 0, C.UDT_SNDSYN, unsafe.Pointer(&falseint), C.sizeof_int) != 0 {
	// 	return fmt.Errorf("failed to set UDT_SNDSYN: %s", lastError())
	// }

	// // set receiving to be async
	// if C.udt_setsockopt(fd.sock, 0, C.UDT_RCVSYN, unsafe.Pointer(&falseint), C.sizeof_int) != 0 {
	// 	return fmt.Errorf("failed to set UDT_RCVSYN: %s", lastError())
	// }

	return nil
}
Exemple #2
0
func (fd *udtFD) SetWriteDeadline(t time.Time) error {
	d := C.int(t.Sub(time.Now()).Nanoseconds() / 1e6)
	if C.udt_setsockopt(fd.sock, 0, C.UDT_SNDTIMEO, unsafe.Pointer(&d), C.sizeof_int) != 0 {
		return fmt.Errorf("failed to set WriteDeadline: %s", lastError())
	}
	return nil
}
Exemple #3
0
func (fd *udtFD) setDefaultOpts() error {

	// reduce maximum size
	if C.udt_setsockopt(fd.sock, 0, C.UDP_RCVBUF, unsafe.Pointer(&UDP_RCVBUF_SIZE), C.sizeof_int) != 0 {
		return fmt.Errorf("failed to set UDP_RCVBUF: %d, %s", UDP_RCVBUF_SIZE, lastError())
	}

	// set UDT_REUSEADDR
	trueint := C.int(1)
	if C.udt_setsockopt(fd.sock, 0, C.UDT_REUSEADDR, unsafe.Pointer(&trueint), C.sizeof_int) != 0 {
		return fmt.Errorf("failed to set UDT_REUSEADDR: %s", lastError())
	}

	// unset UDT_LINGER
	// falseint := C.int(0)
	// if C.udt_setsockopt(fd.sock, 0, C.UDT_LINGER, unsafe.Pointer(&falseint), C.sizeof_int) != 0 {
	// 	return fmt.Errorf("failed to set UDT_LINGER: %s", lastError())
	// }

	return nil
}
Exemple #4
0
func Setsockopt(socket *Socket, option string, value interface{}) (retval int, err error) {
	var data []byte
	if option != UDT_LINGER {
		data, err = getBytes(value)
		if err != nil {
			return -1, fmt.Errorf("Unable to convert interface to byte array %s", err)
		}
	}
	switch option {
	case UDT_MSS:
		{
			if reflect.TypeOf(value).Kind() != reflect.Uint16 {
				return -1, fmt.Errorf("Requires Uint16 type")
			}
			retval = int(C.udt_setsockopt(socket.sock, C.int(0), C.UDT_UDT_MSS,
				unsafe.Pointer(&data[0]), C.int(len(data))))
		}

	case UDT_SNDSYN:
		{
			if reflect.TypeOf(value).Kind() != reflect.Uint64 {
				return -1, fmt.Errorf("Requires Uint64 type")
			}
			retval = int(C.udt_setsockopt(socket.sock, C.int(0), C.UDT_UDT_SNDSYN,
				unsafe.Pointer(&data[0]), C.int(len(data))))
		}
	case UDT_RCVSYN:
		{
			if reflect.TypeOf(value).Kind() != reflect.Uint64 {
				return -1, fmt.Errorf("Requires Uint64 type")
			}
			retval = int(C.udt_setsockopt(socket.sock, C.int(0), C.UDT_UDT_RCVSYN,
				unsafe.Pointer(&data[0]), C.int(len(data))))
		}

	case UDT_CC:
		{
			return -1, fmt.Errorf("UDT CCC Not implemented.")
		}

	case UDT_FC:
		{
			if reflect.TypeOf(value).Kind() != reflect.Uint16 {
				return -1, fmt.Errorf("Requires Uint16 type")
			}
			retval = int(C.udt_setsockopt(socket.sock, C.int(0), C.UDT_UDT_FC,
				unsafe.Pointer(&data[0]), C.int(len(data))))
		}

	case UDT_SNDBUF:
		{
			if reflect.TypeOf(value).Kind() != reflect.Uint16 {
				return -1, fmt.Errorf("Requires Uint16 type")
			}
			retval = int(C.udt_setsockopt(socket.sock, C.int(0), C.UDT_UDT_SNDBUF,
				unsafe.Pointer(&data[0]), C.int(len(data))))
		}

	case UDT_RCVBUF:
		{
			if reflect.TypeOf(value).Kind() != reflect.Uint16 {
				return -1, fmt.Errorf("Requires Uint16 type")
			}
			retval = int(C.udt_setsockopt(socket.sock, C.int(0), C.UDT_UDT_RCVBUF,
				unsafe.Pointer(&data[0]), C.int(len(data))))
		}

	case UDP_SNDBUF:
		{
			if reflect.TypeOf(value).Kind() != reflect.Uint16 {
				return -1, fmt.Errorf("Requires Uint16 type")
			}
			retval = int(C.udt_setsockopt(socket.sock, C.int(0), C.UDT_UDP_SNDBUF,
				unsafe.Pointer(&data[0]), C.int(len(data))))
		}
	case UDP_RCVBUF:
		{
			if reflect.TypeOf(value).Kind() != reflect.Uint16 {
				return -1, fmt.Errorf("Requires Uint16 type")
			}
			retval = int(C.udt_setsockopt(socket.sock, C.int(0), C.UDT_UDP_RCVBUF,
				unsafe.Pointer(&data[0]), C.int(len(data))))
		}

	case UDT_LINGER:
		{
			var clingerin Linger = value.(Linger)
			var clinger C.struct_linger
			clinger.l_onoff = C.int(clingerin.l_onoff)
			clinger.l_linger = C.int(clingerin.l_linger)
			retval = int(C.udt_setsockopt(socket.sock, C.int(0), C.UDT_UDT_LINGER,
				unsafe.Pointer(&clinger), C.int(unsafe.Sizeof(clinger))))
		}

	case UDT_RENDEZVOUS:
		{
			if reflect.TypeOf(value).Kind() != reflect.Uint64 {
				return -1, fmt.Errorf("Requires Uint64 type")
			}
			retval = int(C.udt_setsockopt(socket.sock, C.int(0), C.UDT_UDT_RENDEZVOUS,
				unsafe.Pointer(&data[0]), C.int(len(data))))
		}

	case UDT_REUSEADDR:
		{
			if reflect.TypeOf(value).Kind() != reflect.Uint64 {
				return -1, fmt.Errorf("Requires Uint64 type")
			}
			retval = int(C.udt_setsockopt(socket.sock, C.int(0), C.UDT_UDT_REUSEADDR,
				unsafe.Pointer(&data[0]), C.int(len(data))))
		}

	case UDT_MAXBW:
		{
			if reflect.TypeOf(value).Kind() != reflect.Uint64 {
				return -1, fmt.Errorf("Requires Uint64 type")
			}
			retval = int(C.udt_setsockopt(socket.sock, C.int(0), C.UDT_UDT_MAXBW,
				unsafe.Pointer(&data[0]), C.int(len(data))))
		}

	default:
		{
			return -1, fmt.Errorf("Invalid option %s", option)
		}
	}

	if retval < 0 {
		return retval, udtErrDesc("Unable set option")
	}

	return
}