I took this chrome app example for NativeMessaging, it allows a python program to run and talk back to app. I converted it to extension and am trying to replace the python script with a C program but my C program is not able to communicate back to extension.my question is, Is it possible to use C instead python (looks possible to me, at least). if it is possible then what am I doing wrong?
Code for replacement of python:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#define NUM_THREADS 2
int t1=0;
void *thr_fwrite(void *arg) {
FILE *p;
if(arg==NULL)
p=(FILE*)arg;
char buffer[200]={"0023 hello how are you q"};;
int len=27;
while(1)
{
if(t1==0){
fwrite(buffer,len,sizeof(char),stdout);
fwrite(buffer,len,sizeof(char),p);
t1=1;
}
}
pthread_exit(NULL);
}
/* thread function */
void *thr_fread(void *arg) {
FILE *p;
if(arg==NULL)
p=(FILE*)arg;
char buffer[200]={""};
int len=0;
while(1)
{
if(t1==1){
fread(buffer,1,sizeof(int),stdin);
if(strcmp(buffer,"")==0)
exit(0);
len=atoi(buffer);
if(len==0 || len>200)
exit(0);
fread(buffer,len,sizeof(char),stdin);
fwrite(buffer,len,sizeof(char),p);
t1=0;
}
}
pthread_exit(NULL);
}
int main(int argc, char **argv) {
pthread_t thr[NUM_THREADS];
int i, rc;
FILE *p;
FILE *q;
p=fopen("~/temp.txt","w+");
q=fopen("~/temp1.txt","w+");
/* create threads */
if ((rc = pthread_create(&thr[0], NULL, thr_fwrite, &p))) {
fprintf(stderr, "error: pthread_create, rc: %d\n", rc);
return EXIT_FAILURE;
}
if ((rc = pthread_create(&thr[1], NULL, thr_fread, &q))) {
fprintf(stderr, "error: pthread_create, rc: %d\n", rc);
return EXIT_FAILURE;
}
/* block until all threads complete */
for (i = 0; i < NUM_THREADS; ++i) {
pthread_join(thr[i], NULL);
}
return EXIT_SUCCESS;
}
partial code for chrome extension code is:
function ConnectHost() {
var hostName = "com.example.nativehost";
port = chrome.runtime.connectNative(hostName);
if(port == null)
{
alert("Could not connect to Host Client");
}
else
{
port.onMessage.addListener(onNativeMessage);
port.onDisconnect.addListener(onDisconnected);
}
function onDisconnected() {
alert(chrome.runtime.lastError.message);
port = null;
}
function onNativeMessage(message) {
alert(message);
SendToNative("exit");
}
Do we have any example already which uses C instead of python?