-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Read JSON document from socket and file descriptor #92
Comments
Glad you're enjoying it! Look into the WJReader docs (also on the wiki here on github) about reader callbacks to get data from anywhere, including a socket. It wouldn't hurt to accumulate some examples/helpers if you want to contribute what you come up with. (The same goes for WJWriter, you can write to a socket or anything else with a pretty simple callback.) I'm not sure what you're asking when it comes to merges. Is any of that helpful? |
In general, it is about non-blocking reading of the data stream from the socket. This stream is a json document. There can be many documents one after the other. Currently I have solved it using Newline Delimited JSON (ndjson.org). It's just that individual JSON documents are separated by '\ n'. Then reading and parsing documents is done with the following code:
I was wondering if this could be solved in a different way without using NDJSON. |
Well, it might be doable, but I think your current solution is better.
The parser should stop parsing when it reaches the end of a document, as long
as the JSON was not malformed in any way. In theory you can then start parsing
another.
I think that relying on finishing a document is going to be error prone though.
The approach you're using of having a marker between documents is safer.
- Micah
…On Mon, Oct 05, 2020 at 11:50:26PM -0700, Lucas286 wrote:
In general, it is about non-blocking reading of the data stream from
the socket. This stream is a json document. There can be many documents
one after the other. Currently I have solved it using Newline Delimited
JSON [1](ndjson.org). It's just that individual JSON documents are
separated by '\ n'. Then reading and parsing documents is done with the
following code:
// ipc client
typedef struct
{
int fd;
struct timespec timeout;
char* buff;
int buff_idx;
} client_t;
// *****************************************************************************
***********************
int load_client_ndjson(client_t *client)
{
struct timeval t;
fd_set fdr;
do
{
bzero(&t, sizeof(t));
FD_ZERO(&fdr);
FD_SET(client->fd, &fdr);
if (select(client->fd + 1, &fdr, NULL, NULL, &t) > 0)
{
if (FD_ISSET(client->fd, &fdr))
{
char c;
if (read(client->fd, &c, 1))
{
client->buff[client->buff_idx++] = c;
}
else
{
// socket disconected
ipc_close_client(client);
break;
}
if (c == '\n')
{
return 1;
}
}
}
else
{
return 0;
}
}
while (client->buff_idx < IPC_TCP_BUFFER_SIZE - 1);
// overflow, clear buffer
bzero(client->buff, IPC_TCP_BUFFER_SIZE);
client->buff_idx = 0;
return 0;
}
// *****************************************************************************
***********************
void ipc_client_process(client_t *client)
{
// here descriptor i ready to read, but I am not sure that the entire doc
ument is readable
// load full Newline Delimited JSON
if (load_client_ndjson(client))
{
WJElement ndjson = NULL;
ndjson = WJEParse(client->buff);
if (ndjson)
{
// wjelement operations....
}
}
}
I was wondering if this could be solved in a different way without
using NDJSON.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, [2]view it on GitHub, or [3]unsubscribe.
References
1. http://ndjson.org/
2. #92 (comment)
3. https://github.com/notifications/unsubscribe-auth/AADSRCAE2PTXQ7KZ73ZLDW3SJK43FANCNFSM4SADVGQA
|
Hello,
I really like the library. Is there any safe way to load a document from a socket having its descriptor? How do I deal with framing and merging two different documents?
The text was updated successfully, but these errors were encountered: