void ANetworkController::ReceivedSeekAndPauseReplay(msgpack_object* Data) {
	AReplayPlayer* Player = NULL;
	for (TActorIterator<AReplayPlayer> ObjIt(GetWorld()); ObjIt; ++ObjIt) {
		Player = *ObjIt;
		break;
	}
	check(Player != NULL);

	float Seconds = Unpack<double>(Data);
	Player->SeekAndPause(Seconds);

	msgpack_sbuffer sbuf;
	msgpack_sbuffer_init(&sbuf);

	msgpack_packer pk;
	msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);

	msgpack_pack_array(&pk, 2);
	msgpack_pack_uint16(&pk, EPacketType::PT_SeekAndPauseReplay);

	msgpack_pack_float(&pk, Player->GetTime());

	int32 BytesSent;
	TcpSocket->Send((uint8*)sbuf.data, sbuf.size, BytesSent);

	msgpack_sbuffer_destroy(&sbuf);
}
void ANetworkController::ReceivedGetGitHash() {
	msgpack_sbuffer sbuf;
	msgpack_sbuffer_init(&sbuf);

	msgpack_packer pk;
	msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);

	msgpack_pack_array(&pk, 2);
	msgpack_pack_uint16(&pk, EPacketType::PT_GetGitHash);

	msgpack_pack_str(&pk, GIT_HASH_LEN);
	msgpack_pack_str_body(&pk, GIT_HASH, GIT_HASH_LEN);

	int32 BytesSent;
	TcpSocket->Send((uint8*)sbuf.data, sbuf.size, BytesSent);

	msgpack_sbuffer_destroy(&sbuf);
}
Example #3
0
static void *client_thread(void *arg) {

    ssize_t nwritten;

    pthread_detach(pthread_self());

    int client_fd = (int) (long) arg;

    int yes = 1, tcp_keepalive_probes = 3, tcp_keepalive_time = 5, tcp_keepalive_intvl = 2;
    int nodelay = 1;

    if (setsockopt(client_fd, SOL_SOCKET, SO_KEEPALIVE, &yes, sizeof(yes)) == -1) {
        log_error("setsockopt failed: %s", strerror(errno));
        close(client_fd);
        return NULL;
    }

    if (setsockopt(client_fd, SOL_TCP, TCP_KEEPCNT, &tcp_keepalive_probes, sizeof(tcp_keepalive_probes)) == -1) {
        log_error("setsockopt failed: %s", strerror(errno));
        close(client_fd);
        return NULL;
    }

    if (setsockopt(client_fd, SOL_TCP, TCP_KEEPIDLE, &tcp_keepalive_time, sizeof(tcp_keepalive_time)) == -1) {
        log_error("setsockopt failed: %s", strerror(errno));
        close(client_fd);
        return NULL;
    }

    if (setsockopt(client_fd, SOL_TCP, TCP_KEEPINTVL, &tcp_keepalive_intvl, sizeof(tcp_keepalive_intvl)) == -1) {
        log_error("setsockopt failed: %s", strerror(errno));
        close(client_fd);
        return NULL;
    }

    if (setsockopt(client_fd, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof(nodelay)) == -1) {
        log_error("setsockopt failed: %s", strerror(errno));
        close(client_fd);
        return NULL;
    }

    size_t request_size = 8192;
    struct request *request_back = NULL;
    struct request *request = (struct request *) malloc(request_size);
    if (!request) {
        log_error("malloc failed: %s", strerror(errno));
        close(client_fd);
        return NULL;
    }

    msgpack_unpacked msg;
    msgpack_unpacked_init(&msg);

    bool enable_gzip = false;

    while (1) {

        struct response_header response_header;

        ssize_t recvsize = readn(client_fd, request, sizeof(struct request));

        if (!recvsize) {
            log_warn("peer closed connection");
            break;
        }

        if (recvsize < (ssize_t) sizeof(struct request)) {
            log_warn("error while receiving header, received %zd", recvsize);
            break;
        }

        if (request->payload_size > 256 * 1024 * 1024) {
            log_warn("payload size %"PRIu32" too large", request->payload_size);
            break;
        }

        if (sizeof(struct request) + request->payload_size > request_size) {
            request_size = sizeof(struct request) + request->payload_size;
            request_back = request;
            request = realloc(request, request_size);
            if (!request) {
                log_error("realloc failed: %s", strerror(errno));
                free(request_back);
                break;
            }
        }

        recvsize = readn(client_fd, request->payload, request->payload_size);

        if (!recvsize) {
            log_warn("peer closed connection");
            break;
        }

        if (recvsize < request->payload_size) {
            log_warn("error while receiving payload, received %zd (should be %"PRIu32")", recvsize, request->payload_size);
            break;
        }

        if (request->flags & REQUEST_FLAG_GZIP) {
            enable_gzip = true;
            uLongf destLen = 16;
            Bytef *dest = malloc(destLen);
            if (!dest) {
                log_error("malloc failed: %s", strerror(errno));
                break;
            }

            int ret;
keep_malloc:
            ret = uncompress(dest, &destLen, (Bytef *) request->payload, request->payload_size);
            if (ret == Z_BUF_ERROR) {
                destLen = destLen * 2;
                free(dest);
                dest = malloc(destLen);
                if (!dest) {
                    log_error("malloc failed: %s", strerror(errno));
                    break;
                }
                goto keep_malloc;
            }

            if (ret != Z_OK) {
                free(dest);
                break;
            }

            request->flags &= ~REQUEST_FLAG_GZIP;
            if (sizeof(struct request) + destLen > request_size) {
                request_size = sizeof(struct request) + destLen;
                request_back = request;
                request = realloc(request, request_size);
                if (!request) {
                    log_error("realloc failed: %s", strerror(errno));
                    free(request_back);
                    free(dest);
                    break;
                }
            }
            memcpy(request->payload, dest, destLen);
            request->payload_size = destLen;

            free(dest);
        }

        bool success = msgpack_unpack_next(&msg, request->payload, request->payload_size, NULL);
        if (!success) {
            log_warn("error while parsing payload");
            break;
        }

        msgpack_sbuffer* buffer = msgpack_sbuffer_new();
        msgpack_packer* pk = msgpack_packer_new(buffer, msgpack_sbuffer_write);

        response_header.seq = request->seq;

        __sync_add_and_fetch(&pending_commands, 1);
        if (server->terminated) {
            __sync_add_and_fetch(&pending_commands, -1);
            break;
        }

        if (server->redirection) {
            response_header.status = RESPONSE_STATUS_REDIRECTION;
            msgpack_pack_map(pk, 2);
            mp_pack_string(pk, "host");
            mp_pack_string(pk, server->redirection->host);
            mp_pack_string(pk, "port");
            msgpack_pack_uint16(pk, server->redirection->port);
        }
        else {
            response_header.status = execute_command(request, request->command, msg.data, pk);
        }
        __sync_add_and_fetch(&pending_commands, -1);

        msgpack_packer_free(pk);

        if (!(request->flags & REQUEST_FLAG_NO_REPLY)) {
            if (!(buffer->size > 0)) {
                response_header.payload_size = 0;
                nwritten = writen(client_fd, &response_header, sizeof(response_header));
                if (!nwritten) {
                    log_error("writen failed: %s", strerror(errno));
                    msgpack_sbuffer_free(buffer);
                    break;
                }
            }
            else {
                if (!enable_gzip) {
                    response_header.payload_size = buffer->size;
                    nwritten = writen(client_fd, &response_header, sizeof(response_header));
                    if (!nwritten) {
                        log_error("writen failed: %s", strerror(errno));
                        msgpack_sbuffer_free(buffer);
                        break;
                    }

                    nwritten = writen(client_fd, buffer->data, buffer->size);
                    if (!nwritten) {
                        log_error("writen failed: %s", strerror(errno));
                        msgpack_sbuffer_free(buffer);
                        break;
                    }
                }
                else {
                    uLongf destLen = buffer->size * 1.00101 + 13;
                    Bytef *dest = malloc(destLen);
                    if (!dest) {
                        log_error("malloc failed: %s", strerror(errno));
                        msgpack_sbuffer_free(buffer);
                        break;
                    }
                    int ret = compress(dest, &destLen, (Bytef *) buffer->data, buffer->size);

                    while (ret == Z_BUF_ERROR) {
                        destLen = destLen * 2 + 16;
                        free(dest);
                        dest = malloc(destLen);
                        if (!dest) {
                            log_error("malloc failed: %s", strerror(errno));
                            msgpack_sbuffer_free(buffer);
                            break;
                        }
                        ret = compress(dest, &destLen, (Bytef *) buffer->data, buffer->size);
                    }

                    if (ret != Z_OK) {
                        log_error("error while compressing response: %d", ret);
                        if (dest)
                            free(dest);
                        break;
                    }

                    response_header.payload_size = destLen;
                    nwritten = writen(client_fd, &response_header, sizeof(response_header));
                    if (!nwritten) {
                        log_warn("peer closed connection");
                        msgpack_sbuffer_free(buffer);
                        free(dest);
                        break;
                    }

                    if (nwritten < 0) {
                        log_error("send response header failed: %s", strerror(errno));
                        msgpack_sbuffer_free(buffer);
                        free(dest);
                        break;
                    }

                    if (nwritten < (ssize_t)sizeof(response_header)) {
                        log_warn("error while sending response header, sent %zd (should be %"PRIu64")", nwritten, sizeof(response_header));
                        msgpack_sbuffer_free(buffer);
                        free(dest);
                        break;
                    }

                    nwritten = writen(client_fd, dest, destLen);
                    if (!nwritten) {
                        log_warn("peer closed connection");
                        msgpack_sbuffer_free(buffer);
                        free(dest);
                        break;
                    }

                    if (nwritten < 0) {
                        log_error("send response failed: %s", strerror(errno));
                        msgpack_sbuffer_free(buffer);
                        free(dest);
                        break;
                    }

                    if (nwritten < (ssize_t)destLen) {
                        log_warn("error while sending response, sent %zd (should be %"PRIu64")", nwritten, destLen);
                        msgpack_sbuffer_free(buffer);
                        free(dest);
                        break;
                    }

                    free(dest);
                }
            }
        }

        msgpack_sbuffer_free(buffer);

        if (request_size > 65536) {
            request_size = 8192;
            request_back = request;
            request = realloc(request, request_size);
            if (!request) {
                log_error("realloc failed: %s", strerror(errno));
                free(request_back);
                break;
            }
        }
    }

    close(client_fd);

    free(request);
    msgpack_unpacked_destroy(&msg);

    return NULL;
}
Example #4
0
int msgpack_pack_uint16_wrap(msgpack_packer* pk, uint16_t d)
{
  return msgpack_pack_uint16(pk, d);
}
void ANetworkController::ReceivedGetBehaviors() {
	msgpack_sbuffer sbuf;
	msgpack_sbuffer_init(&sbuf);

	/* serialize values into the buffer using msgpack_sbuffer_write callback function. */
	msgpack_packer pk;
	msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);

	msgpack_pack_array(&pk, 2);
	msgpack_pack_uint16(&pk, EPacketType::PT_GetBehaviors);

	TArray<ABehaviours*> BehaviorsList;
	for (TActorIterator<ABehaviours> ObjIt(GetWorld()); ObjIt; ++ObjIt) {
		ABehaviours* Behaviors = *ObjIt;
		BehaviorsList.Add(Behaviors);
	}

	int32 BehaviorsListNum = BehaviorsList.Num();
	msgpack_pack_array(&pk, BehaviorsListNum);
	for (int i = 0; i < BehaviorsListNum; i++) {
		ABehaviours* Behaviors = BehaviorsList[i];
		msgpack_pack_map(&pk, 4);
		msgpack_pack_str(&pk, 13);
		msgpack_pack_str_body(&pk, "FlockingState", 13);
		msgpack_pack_uint8(&pk, (uint8)Behaviors->FlockingState);
		msgpack_pack_str(&pk, 13);
		msgpack_pack_str_body(&pk, "ThrottleGains", 13);
		Pack(&pk, Behaviors->ThrottleGains);
		msgpack_pack_str(&pk, 13);
		msgpack_pack_str_body(&pk, "SteeringGains", 13);
		Pack(&pk, Behaviors->SteeringGains);

		int32 BehaviorsNum = Behaviors->Behaviours.Num();
		msgpack_pack_str(&pk, 9);
		msgpack_pack_str_body(&pk, "Behaviors", 9);
		msgpack_pack_array(&pk, BehaviorsNum);
		for (int j = 0; j < BehaviorsNum; j++) {
			UBehaviour* Behavior = Behaviors->Behaviours[j];

			TArray<UProperty*> Properties;
			for (TFieldIterator<UProperty> PropIt(Behavior->GetClass()); PropIt; ++PropIt) {
				UProperty* Property = *PropIt;
				if (Cast<UNumericProperty>(Property) || Cast<UBoolProperty>(Property)) {
					Properties.Add(Property);
				}
			}

			msgpack_pack_map(&pk, Properties.Num() + 1);
			msgpack_pack_str(&pk, 4);
			msgpack_pack_str_body(&pk, "Name", 4);
			FString Name = Behavior->GetClass()->GetName();
			msgpack_pack_str(&pk, Name.Len());
			msgpack_pack_str_body(&pk, TCHAR_TO_UTF8(*Name), Name.Len());
			for (auto PropIt(Properties.CreateIterator()); PropIt; ++PropIt) {
				UProperty* Property = *PropIt;
				const void* Value = Property->ContainerPtrToValuePtr<uint8>(Behavior);
				FString Name = Property->GetName();
				msgpack_pack_str(&pk, Name.Len());
				msgpack_pack_str_body(&pk, TCHAR_TO_UTF8(*Name), Name.Len());
				if (UNumericProperty *NumericProperty = Cast<UNumericProperty>(Property)) {
					if (NumericProperty->IsFloatingPoint()) {
						msgpack_pack_double(&pk, NumericProperty->GetFloatingPointPropertyValue(Value));
					}
					else if (NumericProperty->IsInteger()) {
						msgpack_pack_int(&pk, NumericProperty->GetSignedIntPropertyValue(Value));
					}
				}
				else if (UBoolProperty *BoolProperty = Cast<UBoolProperty>(Property)) {
					if (BoolProperty->GetPropertyValue(Value)) {
						msgpack_pack_true(&pk);
					}
					else {
						msgpack_pack_false(&pk);
					}
				}
			}
		}
	}

	int32 BytesSent;
	TcpSocket->Send((uint8*)sbuf.data, sbuf.size, BytesSent);

	msgpack_sbuffer_destroy(&sbuf);
}
void ANetworkController::SendData() {
	if (!TcpSocket) {
		return;
	}

	// Clear the data if there is anything on the stream
	TryReceiveData();

	AVehiclestats* Stats = NULL;
	for (TActorIterator<AVehiclestats> ObjIt(GetWorld()); ObjIt; ++ObjIt) {
		Stats = *ObjIt;
		break;
	}
	if (Stats == NULL) {
		return;
	}

	// Send crashlog

	msgpack_sbuffer sbuf;
	msgpack_sbuffer_init(&sbuf);

	msgpack_packer pk;
	msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);


	for (auto Iter = Stats->IncidentLog.CreateConstIterator(); Iter; ++Iter) {
		msgpack_pack_array(&pk, 2);
		msgpack_pack_uint16(&pk, EPacketType::PT_IncidentLog);
		Pack(&pk, *Iter);
	}

	Stats->IncidentLog.Empty();


	// Send the statestics

	msgpack_pack_array(&pk, 2);
	msgpack_pack_uint16(&pk, EPacketType::PT_GetStats);

	msgpack_pack_map(&pk, 6);

	msgpack_pack_str(&pk, 7);
	msgpack_pack_str_body(&pk, "spawned", 7);
	msgpack_pack_uint32(&pk, Stats->GetNumVehiclesSpawned());

	msgpack_pack_str(&pk, 6);
	msgpack_pack_str_body(&pk, "onroad", 6);
	msgpack_pack_uint32(&pk, Stats->GetNumVehiclesOnTheRoad());

	msgpack_pack_str(&pk, 7);
	msgpack_pack_str_body(&pk, "crashed", 7);
	msgpack_pack_uint32(&pk, Stats->GetNumVehiclesCrashed());

	msgpack_pack_str(&pk, 9);
	msgpack_pack_str_body(&pk, "incidents", 9);
	msgpack_pack_uint32(&pk, Stats->GetNumVehiclesIncidents());

	TArray<float> Throughputs;

	for (TActorIterator<AMeasurementGate> ObjIt(GetWorld()); ObjIt; ++ObjIt) {
		AMeasurementGate* Gate = *ObjIt;
		Throughputs.Add(Gate->GetThroughput(0));
		Throughputs.Add(Gate->GetThroughput(1));
	}

	msgpack_pack_str(&pk, 11);
	msgpack_pack_str_body(&pk, "throughputs", 11);
	msgpack_pack_array(&pk, Throughputs.Num());
	for (auto Iter = Throughputs.CreateConstIterator(); Iter; ++Iter) {
		msgpack_pack_float(&pk, *Iter);
	}

	msgpack_pack_str(&pk, 4);
	msgpack_pack_str_body(&pk, "time", 4);
	msgpack_pack_float(&pk, Stats->GetTime());

	int32 BytesLeft = sbuf.size;
	while (BytesLeft > 0) {
		int32 Index = sbuf.size - BytesLeft;
		int32 BytesSent;
		TcpSocket->Send((uint8*)sbuf.data + Index, BytesLeft, BytesSent);
		BytesLeft = BytesLeft - BytesSent;
		if (BytesSent == -1) {
			break;
		}
	}

	msgpack_sbuffer_destroy(&sbuf);
}
Example #7
0
void msgpack_rpc_from_position(Position result, msgpack_packer *res)
{
  msgpack_pack_array(res, 2);;
  msgpack_pack_uint16(res, result.row);
  msgpack_pack_uint16(res, result.col);
}