Path: blob/21.2-virgl/src/gallium/auxiliary/rbug/rbug_connection.c
4561 views
/*1* Copyright 2009 VMware, Inc.2* All Rights Reserved.3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* on the rights to use, copy, modify, merge, publish, distribute, sub8* license, and/or sell copies of the Software, and to permit persons to whom9* the Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL18* VMWARE AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,19* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR20* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE21* USE OR OTHER DEALINGS IN THE SOFTWARE.22*/2324#include "rbug.h"25#include "rbug_internal.h"2627#include "util/u_network.h"2829struct rbug_connection30{31int socket;32uint32_t send_serial;33uint32_t recv_serial;34enum rbug_opcode opcode;35};3637/**38* Create a rbug connection from a socket created with u_socket.39*40* Result:41* A new allocated connection using socket as communication path42*/43struct rbug_connection *44rbug_from_socket(int socket)45{46struct rbug_connection *c = CALLOC_STRUCT(rbug_connection);47c->socket = socket;48return c;49}5051/**52* Free a connection, also closes socket.53*/54void55rbug_disconnect(struct rbug_connection *c)56{57u_socket_close(c->socket);58FREE(c);59}6061/**62* Waits for a message to be fully received.63* Also returns the serial for the message, serial is not touched for replys.64*65* Result:66* demarshaled message on success, NULL on connection error67*/68struct rbug_header *69rbug_get_message(struct rbug_connection *c, uint32_t *serial)70{71struct rbug_proto_header header;72struct rbug_header *out;73struct rbug_proto_header *data;74size_t length = 0;75size_t read = 0;76int ret;777879ret = u_socket_peek(c->socket, &header, sizeof(header));80if (ret <= 0) {81return NULL;82}8384length = (size_t)header.length * 4;85data = MALLOC(length);86if (!data) {87return NULL;88}89data->opcode = 0;9091do {92uint8_t *ptr = ((uint8_t*)data) + read;93ret = u_socket_recv(c->socket, ptr, length - read);9495if (ret <= 0) {96FREE(data);97return NULL;98}99100read += ret;101} while(read < length);102103out = rbug_demarshal(data);104if (!out)105FREE(data);106else if (serial)107*serial = c->recv_serial++;108else109c->recv_serial++;110111return out;112}113114/**115* Frees a message and associated data.116*/117void118rbug_free_header(struct rbug_header *header)119{120if (!header)121return;122123FREE(header->__message);124FREE(header);125}126127/**128* Internal function used by rbug_send_* functions.129*130* Start sending a message.131*/132int133rbug_connection_send_start(struct rbug_connection *c, enum rbug_opcode opcode, uint32_t length)134{135c->opcode = opcode;136return 0;137}138139/**140* Internal function used by rbug_send_* functions.141*142* Write data to the socket.143*/144int145rbug_connection_write(struct rbug_connection *c, void *to, uint32_t size)146{147int ret = u_socket_send(c->socket, to, size);148return ret;149}150151/**152* Internal function used by rbug_send_* functions.153*154* Finish writing data to the socket.155* Ups the send_serial and sets the serial argument if supplied.156*/157int rbug_connection_send_finish(struct rbug_connection *c, uint32_t *serial)158{159if (c->opcode < 0)160return 0;161else if (serial)162*serial = c->send_serial++;163else164c->send_serial++;165166return 0;167}168169170