//Send a message over UDP void sendUDPMessage(const char* hostname, int port, VyMessage* message) { //Make sure there is an open socket and someone to send the data too if(message == NULL || hostname == NULL) return; int sock; struct sockaddr_in server_addr; //Create a socket and make sure it is valid if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) return; //Show debug and display information printf(" + UUID: <%s> ",message->senderID); printf(": Sending UDP message: [%s]",message->command); printf(" <%i> ",message->messageSize); printf(" [%s]\n",message->data); //Setup UDP information server_addr.sin_family = AF_INET; server_addr.sin_port = htons(port); server_addr.sin_addr.s_addr = inet_addr(hostname); bzero(&(server_addr.sin_zero),8); //Convert VyMessage to char array unsigned char* unparsedMessage = unparseNewMessage(message); //Send the message sendto(sock, unparsedMessage, getMessageSize(message), 0, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)); free(unparsedMessage); }
void MessageQueueBase::setSelectedMessageForReading(int message) { ASSERT(message >= 0); ASSERT(message < numberOfMessages); if(messageIndex) selectedMessageForReadingPosition = messageIndex[message]; else { int m = message; if(m >= lastMessage) { ASSERT(lastMessage < numberOfMessages); m -= lastMessage; } else selectedMessageForReadingPosition = 0; for(int i = 0; i < m; ++i) selectedMessageForReadingPosition += getMessageSize() + headerSize; } readPosition = 0; lastMessage = message; }
bool GraspitProtobufConnection::readMessage() { // If a retry is pending, don't continue if(tryRereadTimer.isActive()) return false; quint32 message_length = getMessageSize(); // The expected message size bool readSucceeded = false; // Test if there is a valid message in the buffer if(message_length && sock->bytesAvailable() >= message_length) { //Move input cursor forward to beginning of message sock->seek(sizeof(message_length)); // Read the message data and try to parse it. QByteArray inputByteArray = sock->read(message_length); readSucceeded = msg->ParseFromArray(inputByteArray.data(), inputByteArray.size()); if(!readSucceeded) { DBGA("GraspitProtobufServer::Failed to parse message with size " << inputByteArray.size()); } } int readDelay = readSucceeded ? 1:rereadLatency; if(sock->bytesAvailable()) { scheduleReread(readDelay); } return readSucceeded; }
void MessageQueueBase::createIndex() { freeIndex(); messageIndex = new unsigned[numberOfMessages]; selectedMessageForReadingPosition = 0; for(int i = 0; i < numberOfMessages; ++i) { messageIndex[i] = selectedMessageForReadingPosition; selectedMessageForReadingPosition += getMessageSize() + headerSize; } }
void MessageQueueBase::removeMessage(int message) { freeIndex(); selectedMessageForReadingPosition = 0; int i; for(i = 0; i < message; ++i) selectedMessageForReadingPosition += getMessageSize() + headerSize; usedSize = selectedMessageForReadingPosition; for(++i; i < numberOfMessages; ++i) { int mlength = getMessageSize() + headerSize; selectedMessageForReadingPosition += mlength; memcpy(buf + usedSize, buf + selectedMessageForReadingPosition, mlength); usedSize = selectedMessageForReadingPosition; } readPosition = 0; --numberOfMessages; selectedMessageForReadingPosition = 0; lastMessage = 0; }
//Send a message over TCP void sendMessage(int socket, VyMessage* message) { //Make sure there is data to send if(message == NULL) return; //Show debug and display information printf(" + UUID: <%s> ",message->senderID); printf(": Sending TCP message: [%s]",message->command); printf(" <%i> ",message->messageSize); printf(" [%s]\n",message->data); //Convert VyMessage to char array unsigned char* unparsedMessage = unparseNewMessage(message); //Send the message write(socket, unparsedMessage, getMessageSize(message)); free(unparsedMessage); }
//Send a message over SSL void sendSSLMessage(VySSL* ssl, VyMessage* message) { //Make sure there is an open connection and data to send if(message == NULL || ssl == NULL) return; //Show debug and display information printf(" + UUID: <%s> ",message->senderID); printf(": Sending SSL message: [%s]",message->command); printf(" <%i> ",message->messageSize); printf(" [%s]\n",message->data); //Convert the VyMessage to a char array unsigned char* unparsedMessage = unparseNewMessage(message); //Send the message if not null if(unparsedMessage != NULL) { SSL_write(ssl->ssl, unparsedMessage, getMessageSize(message)); //free(unparsedMessage); } }
bool ClientData::getMessage(string & message) { int bufferLength; if (!getMessageSize(bufferLength)) { return false; } char * buffer = new char[bufferLength + 1]; buffer[bufferLength] = '\0'; // last character to be null terminator int check = recv(Connection, buffer, bufferLength, NULL); message = buffer; delete[] buffer; if (check == SOCKET_ERROR) { cout << "Problem with getMessage occured" << endl; return false; } return true; }
/** * \brief Calculate checksum * \param data Oregon message */ void OregonTemp::calculateAndSet8bitsChecksum() { byte messageSize = getMessageSize()-1; OregonMessageBuffer[messageSize] = ((Sum(messageSize) - 0xa) & 0xFF); }
/** * \brief Send an Oregon message * \param data The Oregon message */ void OSV2::sendOregon() { sendPreamble(); sendData(OregonMessageBuffer, getMessageSize()); sendPostamble(); }
void MessageQueueBase::read(void* p, size_t size) { ASSERT(readPosition + static_cast<int>(size) <= getMessageSize()); memcpy(p, buf + selectedMessageForReadingPosition + headerSize + readPosition, size); readPosition += static_cast<int>(size); }
void MessageQueueBase::removeRepetitions() { ASSERT(!messageIndex); unsigned short messagesPerType[5][numOfMessageIDs]; unsigned char numberOfProcesses = 0, processes[26], currentProcess = 0; memset(messagesPerType, 0, sizeof(messagesPerType)); memset(processes, 255, sizeof(processes)); selectedMessageForReadingPosition = 0; for(int i = 0; i < numberOfMessages; ++i) { if(getMessageID() == idProcessBegin) { unsigned char process = getData()[0] - 'a'; if(processes[process] == 255) processes[process] = numberOfProcesses++; currentProcess = processes[process]; } ++messagesPerType[currentProcess][getMessageID()]; selectedMessageForReadingPosition += getMessageSize() + headerSize; } selectedMessageForReadingPosition = 0; usedSize = 0; int numOfDeleted = 0; int frameBegin = -1; bool frameEmpty = true; for(int i = 0; i < numberOfMessages; ++i) { int mlength = getMessageSize() + headerSize; bool copy; switch(getMessageID()) { // accept up to 20 times, process id is not important case idText: copy = --messagesPerType[currentProcess][idText] <= 20; break; // accept always, process id is not important case idDebugRequest: case idDebugResponse: case idDebugDataResponse: case idPlot: case idConsole: case idAudioData: case idAnnotation: case idLogResponse: copy = true; break; // data only from latest frame case idStopwatch: case idDebugImage: case idDebugJPEGImage: case idDebugDrawing: case idDebugDrawing3D: copy = messagesPerType[currentProcess][idProcessFinished] == 1; break; // always accept, but may be reverted later case idProcessBegin: if(frameBegin != -1) // nothing between last idProcessBegin and this one, so remove idProcessBegin as well { usedSize = frameBegin; ++numOfDeleted; } currentProcess = processes[getData()[0] - 'a']; copy = true; break; case idProcessFinished: ASSERT(currentProcess == processes[getData()[0] - 'a']); copy = !frameEmpty; // nothing since last idProcessBegin or idProcessFinished, no new idProcessFinished required --messagesPerType[currentProcess][idProcessFinished]; break; default: if(getMessageID() < numOfDataMessageIDs) // data only from latest frame copy = messagesPerType[currentProcess][idProcessFinished] == 1; else // only the latest other messages copy = --messagesPerType[currentProcess][getMessageID()] == 0; } if(copy) { // Remember position of begin of frame, but forget it, when another message was copied. // So idProcessBegin idProcessFinished+ will be removed. if(getMessageID() == idProcessBegin) // remember begin of frame { frameBegin = usedSize; frameEmpty = true; // assume next frame as empty } else if(getMessageID() == idProcessFinished) frameEmpty = true; // assume next frame as empty else // we copy a message within a frame so the idProcessBegin/Finished must stay { frameBegin = -1; frameEmpty = false; } //this message is important, it shall be copied if(usedSize != selectedMessageForReadingPosition) memmove(buf + usedSize, buf + selectedMessageForReadingPosition, mlength); usedSize += mlength; } else ++numOfDeleted; selectedMessageForReadingPosition += mlength; } numberOfMessages -= numOfDeleted; readPosition = 0; selectedMessageForReadingPosition = 0; lastMessage = 0; }