在本文中,我们将详细介绍Delphi协议缓冲区?的各个方面,并为您提供关于delphi协程的相关解答,同时,我们也将为您带来关于AndroidHttp请求和响应使用协议缓冲区、C++中套接字上的协议缓
在本文中,我们将详细介绍Delphi协议缓冲区?的各个方面,并为您提供关于delphi 协程的相关解答,同时,我们也将为您带来关于Android Http请求和响应使用协议缓冲区、C ++中套接字上的协议缓冲区、c – OpenGL:只获取模板缓冲区而没有深度缓冲区?、c – 一个大的OpenGL顶点缓冲区,还是许多小缓冲区?的有用知识。
本文目录一览:- Delphi协议缓冲区?(delphi 协程)
- Android Http请求和响应使用协议缓冲区
- C ++中套接字上的协议缓冲区
- c – OpenGL:只获取模板缓冲区而没有深度缓冲区?
- c – 一个大的OpenGL顶点缓冲区,还是许多小缓冲区?
Delphi协议缓冲区?(delphi 协程)
解决方法
http://sourceforge.net/projects/protobuf-delphi/
Android Http请求和响应使用协议缓冲区
我对协议缓冲区主题非常陌生.
但我知道json解析等等
现在,我实际上正在此协议缓冲区上工作,我正在制作一个使用带有协议缓冲区的android进行Http请求和响应的应用程序.
我正在使用android中的协议缓冲区制作一个登录页面.
服务中的一切正常工作墙返回了我想要的每个字段的响应,但是
服务提供给我的信息与服务器发出的响应不同.
我具有有关.proto文件的协议缓冲区的基本知识以及用于从proto编译Java文件的工具,并且也完成了所有连接,我只需要响应或如何序列化和反序列化响应消息.
**AuthenticateUserRequest.Builder abr = AuthenticateUserRequest
.newBuilder();
abr.setUserID(p_UserName);
abr.setPassword(p_Password);
URL url = new URL(
"http://10.0.2.2:49847/Services");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// ResCode = conn.getResponseCode();
// URLConnection conn = url.openConnection();
conn.setRequestProperty("content-type", "application/x-protobuf");
conn.setDoOutput(true);
OutputStream os = conn.getoutputStream();
abr.build().writeto(os);
os.flush();
os.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
byte[] result = String.valueOf(sb).getBytes();
AuthenticateUserResponse.parseFrom(result).toBuilder();**
那就是我任何人的代码都可以帮助我解决此问题.
提前致谢.
解决方法:
您的问题是您试图将编码的protobuf响应视为文本.协议缓冲区是二进制序列化格式.如果将二进制数据转换为String,或者尝试使用Reader读取它,则数据将被破坏.
要解决此问题,请用以下代码替换整个代码的第二部分(从创建BufferedReader的行开始):
AuthenticateUserResponse response =
AuthenticateUserResponse.parseFrom(conn.getInputStream());
C ++中套接字上的协议缓冲区
我正在尝试在Linux平台上探索协议缓冲区(PB),我的编码语言是C
++。我在协议缓冲区在线文档中找到了示例,但没有特定于套接字发送和接收的内容(或者我完全错过了:))。因此,我决定在实际消息之前添加消息Length,并通过套接字发送它。如果有人可以提出比我计划做的更好的解决方案,并且在PB中已经准备好任何用于创建此类数据包的东西,我将不胜感激。
但是我仍然在服务器端遇到问题,必须对数据包进行解码。假设客户端发送了一个10字节的数据包,其中前4个字节是该数据包的长度;但是不可能在解码分组之前知道长度。因此,即使我读取了第一个4字节,也如何使用Protocol
Buffer推断一半读取数据包的值。
答案1
小编典典最后我可以使它工作了。我将代码发布在这里,以便人们可以对其进行评论和评论,以及是否有人想要在c
++中实现它,这部分代码可以提供帮助。我的初衷是为了使Protobuf以长度前缀的方式工作,这是一个简陋的代码。我从某个我不记得的站点上获取了客户端服务器的代码,并对其进行了修改以适应protobuf。在这里,服务器首先窥视套接字并获取总包的长度,然后进行实际的套接字读取以读取整个包。可能有无数种方法可以做到,但是为了快速解决,我以这种方式做到了。但是我需要找到一种更好的方法来避免每个数据包2个recv,但是在我的情况下,所有消息的大小都是不同的,所以这是我猜测的唯一方法。
原始文件
message log_packet { required fixed64 log_time =1; required fixed32 log_micro_sec =2; required fixed32 sequence_no =3; required fixed32 shm_app_id =4; required string packet_id =5; required string log_level=6; required string log_msg=7; }
协议缓冲区客户端代码
#include <unistd.h>#include "message.pb.h"#include <iostream>#include <google/protobuf/message.h>#include <google/protobuf/descriptor.h>#include <google/protobuf/io/zero_copy_stream_impl.h>#include <google/protobuf/io/coded_stream.h>#include <google/protobuf/io/zero_copy_stream_impl_lite.h>using namespace google::protobuf::io;using namespace std;int main(int argv, char** argc){/* Coded output stram */log_packet payload ;payload.set_log_time(10);payload.set_log_micro_sec(10);payload.set_sequence_no(1);payload.set_shm_app_id(101);payload.set_packet_id("TST");payload.set_log_level("DEBUG");payload.set_log_msg("What shall we say then");cout<<"size after serilizing is "<<payload.ByteSize()<<endl;int siz = payload.ByteSize()+4;char *pkt = new char [siz];google::protobuf::io::ArrayOutputStream aos(pkt,siz);CodedOutputStream *coded_output = new CodedOutputStream(&aos);coded_output->WriteVarint32(payload.ByteSize());payload.SerializeToCodedStream(coded_output); int host_port= 1101; char* host_name="127.0.0.1"; struct sockaddr_in my_addr; char buffer[1024]; int bytecount; int buffer_len=0; int hsock; int * p_int; int err; hsock = socket(AF_INET, SOCK_STREAM, 0); if(hsock == -1){ printf("Error initializing socket %d\n",errno); goto FINISH; } p_int = (int*)malloc(sizeof(int)); *p_int = 1; if( (setsockopt(hsock, SOL_SOCKET, SO_REUSEADDR, (char*)p_int, sizeof(int)) == -1 )|| (setsockopt(hsock, SOL_SOCKET, SO_KEEPALIVE, (char*)p_int, sizeof(int)) == -1 ) ){ printf("Error setting options %d\n",errno); free(p_int); goto FINISH; } free(p_int); my_addr.sin_family = AF_INET ; my_addr.sin_port = htons(host_port); memset(&(my_addr.sin_zero), 0, 8); my_addr.sin_addr.s_addr = inet_addr(host_name); if( connect( hsock, (struct sockaddr*)&my_addr, sizeof(my_addr)) == -1 ){ if((err = errno) != EINPROGRESS){ fprintf(stderr, "Error connecting socket %d\n", errno); goto FINISH; } } for (int i =0;i<10000;i++){ for (int j = 0 ;j<10;j++) { if( (bytecount=send(hsock, (void *) pkt,siz,0))== -1 ) { fprintf(stderr, "Error sending data %d\n", errno); goto FINISH; } printf("Sent bytes %d\n", bytecount); usleep(1); } } delete pkt;FINISH: close(hsock);}
协议缓冲区服务器代码
#include <fcntl.h>#include <string.h>#include <stdlib.h>#include <errno.h>#include <stdio.h>#include <netinet/in.h>#include <resolv.h>#include <sys/socket.h>#include <arpa/inet.h>#include <unistd.h>#include <pthread.h>#include "message.pb.h"#include <iostream>#include <google/protobuf/io/coded_stream.h>#include <google/protobuf/io/zero_copy_stream_impl.h>using namespace std;using namespace google::protobuf::io;void* SocketHandler(void*);int main(int argv, char** argc){ int host_port= 1101; struct sockaddr_in my_addr; int hsock; int * p_int ; int err; socklen_t addr_size = 0; int* csock; sockaddr_in sadr; pthread_t thread_id=0; hsock = socket(AF_INET, SOCK_STREAM, 0); if(hsock == -1){ printf("Error initializing socket %d\n", errno); goto FINISH; } p_int = (int*)malloc(sizeof(int)); *p_int = 1; if( (setsockopt(hsock, SOL_SOCKET, SO_REUSEADDR, (char*)p_int, sizeof(int)) == -1 )|| (setsockopt(hsock, SOL_SOCKET, SO_KEEPALIVE, (char*)p_int, sizeof(int)) == -1 ) ){ printf("Error setting options %d\n", errno); free(p_int); goto FINISH; } free(p_int); my_addr.sin_family = AF_INET ; my_addr.sin_port = htons(host_port); memset(&(my_addr.sin_zero), 0, 8); my_addr.sin_addr.s_addr = INADDR_ANY ; if( bind( hsock, (sockaddr*)&my_addr, sizeof(my_addr)) == -1 ){ fprintf(stderr,"Error binding to socket, make sure nothing else is listening on this port %d\n",errno); goto FINISH; } if(listen( hsock, 10) == -1 ){ fprintf(stderr, "Error listening %d\n",errno); goto FINISH; } //Now lets do the server stuff addr_size = sizeof(sockaddr_in); while(true){ printf("waiting for a connection\n"); csock = (int*)malloc(sizeof(int)); if((*csock = accept( hsock, (sockaddr*)&sadr, &addr_size))!= -1){ printf("---------------------\nReceived connection from %s\n",inet_ntoa(sadr.sin_addr)); pthread_create(&thread_id,0,&SocketHandler, (void*)csock ); pthread_detach(thread_id); } else{ fprintf(stderr, "Error accepting %d\n", errno); } }FINISH:;//oops}google::protobuf::uint32 readHdr(char *buf){ google::protobuf::uint32 size; google::protobuf::io::ArrayInputStream ais(buf,4); CodedInputStream coded_input(&ais); coded_input.ReadVarint32(&size);//Decode the HDR and get the size cout<<"size of payload is "<<size<<endl; return size;}void readBody(int csock,google::protobuf::uint32 siz){ int bytecount; log_packet payload; char buffer [siz+4];//size of the payload and hdr //Read the entire buffer including the hdr if((bytecount = recv(csock, (void *)buffer, 4+siz, MSG_WAITALL))== -1){ fprintf(stderr, "Error receiving data %d\n", errno); } cout<<"Second read byte count is "<<bytecount<<endl; //Assign ArrayInputStream with enough memory google::protobuf::io::ArrayInputStream ais(buffer,siz+4); CodedInputStream coded_input(&ais); //Read an unsigned integer with Varint encoding, truncating to 32 bits. coded_input.ReadVarint32(&siz); //After the message''s length is read, PushLimit() is used to prevent the CodedInputStream //from reading beyond that length.Limits are used when parsing length-delimited //embedded messages google::protobuf::io::CodedInputStream::Limit msgLimit = coded_input.PushLimit(siz); //De-Serialize payload.ParseFromCodedStream(&coded_input); //Once the embedded message has been parsed, PopLimit() is called to undo the limit coded_input.PopLimit(msgLimit); //Print the message cout<<"Message is "<<payload.DebugString();}void* SocketHandler(void* lp){ int *csock = (int*)lp; char buffer[4]; int bytecount=0; string output,pl; log_packet logp; memset(buffer, ''\0'', 4); while (1) { //Peek into the socket and get the packet size if((bytecount = recv(*csock, buffer, 4, MSG_PEEK))== -1){ fprintf(stderr, "Error receiving data %d\n", errno); }else if (bytecount == 0) break; cout<<"First read byte count is "<<bytecount<<endl; readBody(*csock,readHdr(buffer)); }FINISH: free(csock); return 0;}
c – OpenGL:只获取模板缓冲区而没有深度缓冲区?
是否可以仅请求带有颜色缓冲区的模板缓冲区?
如果可能的话,大多数OpenGL实现是否可以批准该请求?
我使用的OpenGL版本是2.0
编辑:
我用来调用OpenGL的API是SFML,它通常不支持它的FBO包装器对象的模板分配,尽管它允许它用于显示表面的帧缓冲.我自己编辑了这个功能,虽然这就是我被困住的地方.
glrenderbufferStorageEXT(GL_RENDERBUFFER_EXT,GL_DEPTH24_STENCIL8_EXT,width,height));
该行决定了我假设的存储类型.但是,GL_DEPTH24_STENCIL8_EXT是我发现的唯一一个指定模板缓冲区创建的定义. (至少没有GL_STENCIL8或类似的东西)
解决方法
NEVER EVER MAKE A STENCIL buffer. All GPUs and all drivers do not support an independent stencil buffer. If you need a stencil buffer,then you need to make a Depth=24,Stencil=8 buffer,also called D24S8.
压力测试两种不同的分配方案,GL_STENCIL_INDEX8_EXT与GL_DEPTH24_STENCIL8_EXT,结果在内存使用和性能方面大致相等.我怀疑它无论如何都用24位填充模板缓冲区.因此,为了便于携带,只使用深度和模板包装方案.
c – 一个大的OpenGL顶点缓冲区,还是许多小缓冲区?
以下哪两种选择更好?
>使用glGenBuffer创建的一个大的预分配缓冲区,每个实体将使用(缓冲区的id作为参数传递给Render方法),通过glBufferSubData将其顶点写入缓冲区.
>每个实体都创建并使用自己的缓冲区.
如果一个大缓冲区更好,我如何使用适当的着色器和所有内容正确渲染此缓冲区中的所有顶点(来自所有实体)?
解决方法
缓冲区必须有多大以避免过多的开销,这取决于很多因素,甚至几乎不可能给出经验法则.发挥作用的因素包括:
>硬件性能特征.
>驾驶员效率.
>相对于片段数(三角形大小)的顶点数.
>着色器的复杂性.
通常,保留通常在单个顶点缓冲区中同时绘制的相似/相关对象是有意义的.
将所有内容放在一个缓冲区中似乎极端,实际上可能会产生不利影响.假设您有一个大的“世界”,您只在任何给定的帧中渲染一个小子集.如果你走到极端,一个巨大的缓冲区中包含所有顶点,那么每次绘制调用都需要GPU访问该缓冲区.根据体系结构以及缓冲区的分配方式,这可能意味着:
>尝试将缓冲区保留在专用的GPU内存(例如VRAM)中,如果它太大,可能会出现问题.
>将内存映射到GPU地址空间.
>固定/连接内存.
如果上述任何一个需要应用于一个非常大的缓冲区,但最终只使用它的一小部分来渲染一个帧,那么这些操作就会有很大的浪费.在具有VRAM的系统中,它还可以防止其他分配(如纹理)适合VRAM.
如果使用只能访问参数给出的缓冲区子集的调用来完成渲染,例如glDrawArrays()或glDrawRangeElements(),则驱动程序可能会避免使整个缓冲区GPU可访问.但我不一定指望这种情况发生.
关于Delphi协议缓冲区?和delphi 协程的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Android Http请求和响应使用协议缓冲区、C ++中套接字上的协议缓冲区、c – OpenGL:只获取模板缓冲区而没有深度缓冲区?、c – 一个大的OpenGL顶点缓冲区,还是许多小缓冲区?等相关知识的信息别忘了在本站进行查找喔。
本文标签: