MCPcopy Create free account
hub / github.com/Moddable-OpenSource/moddable / fxOpenNetwork

Function fxOpenNetwork

tools/serial2xsbug/serial2xsbug_mac.c:75–120  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

73}
74
75txSerialMachine fxOpenNetwork(txSerialTool self, uint32_t value)
76{
77 txSerialMachine* link = &(self->firstMachine);
78 txSerialMachine machine;
79 while ((machine = *link)) {
80 if (machine->value == value)
81 break;
82 link = &(machine->nextMachine);
83 }
84 if (!machine) {
85 CFSocketContext context;
86 struct hostent *host;
87 struct sockaddr_in address;
88
89 machine = calloc(sizeof(txSerialMachineRecord), 1);
90 if (!machine) {
91 fprintf(stderr, "Error allocating machine - %s(%d).\n", strerror(errno), errno);
92 exit(1);
93 }
94 machine->tool = self;
95 machine->value = value;
96 sprintf(machine->tag, "?xs.%8.8X?>\r\n<", value);
97 *link = machine;
98
99 memset(&context, 0, sizeof(CFSocketContext));
100 context.info = (void*)machine;
101 host = gethostbyname(self->host);
102 if (!host) {
103 fprintf(stderr, "Error getting host - %s(%d).\n", strerror(errno), errno);
104 exit(1);
105 }
106 memcpy(&(address.sin_addr), host->h_addr, host->h_length);
107 address.sin_family = AF_INET;
108 address.sin_port = htons(self->port);
109 machine->networkSocket = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_STREAM, IPPROTO_TCP, kCFSocketReadCallBack, fxReadNetwork, &context);
110 CFSocketError err = CFSocketConnectToAddress(machine->networkSocket, CFDataCreate(kCFAllocatorDefault, (const UInt8*)&address, sizeof(address)), (CFTimeInterval)10);
111 if (err) {
112 fprintf(stderr,"Error opening network: %ld.\n (Unable to connect to JavaScript debugger (xsbug). Is it running?)\n",err);
113 exit(1);
114 }
115 machine->networkSource = CFSocketCreateRunLoopSource(NULL, machine->networkSocket, 0);
116 CFRunLoopAddSource(CFRunLoopGetCurrent(), machine->networkSource, kCFRunLoopCommonModes);
117 }
118 machine->receiveCount += 1;
119 return machine;
120}
121
122void fxOpenSerial(txSerialTool self)
123{

Callers 1

fxReadSerialBufferFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected