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

Function fxOpenNetwork

tools/serial2xsbug/serial2xsbug_win.c:76–126  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

74}
75
76txSerialMachine fxOpenNetwork(txSerialTool self, uint32_t value)
77{
78 txSerialMachine* link = &(self->firstMachine);
79 txSerialMachine machine;
80 while ((machine = *link)) {
81 if (machine->value == value)
82 break;
83 link = &(machine->nextMachine);
84 }
85 if (!machine) {
86 struct sockaddr_in address;
87
88 if (self->count == 2 + mxMachinesCount)
89 mxThrowError(ERROR_NOT_ENOUGH_MEMORY);
90
91 machine = calloc(sizeof(txSerialMachineRecord), 1);
92 mxThrowElse(machine != NULL);
93 machine->tool = self;
94 machine->value = value;
95 sprintf(machine->tag, "?xs.%8.8X?>\r\n<", value);
96 *link = machine;
97
98 memset(&address, 0, sizeof(address));
99 address.sin_family = AF_INET;
100 address.sin_addr.s_addr = inet_addr(self->host);
101 if (address.sin_addr.s_addr == INADDR_NONE) {
102 struct hostent *host = gethostbyname(self->host);
103 mxWSAThrowElse(host);
104 memcpy(&(address.sin_addr), host->h_addr, host->h_length);
105 }
106 address.sin_port = htons(self->port);
107
108 machine->networkConnection = socket(AF_INET, SOCK_STREAM, 0);
109 mxWSAThrowElse(machine->networkConnection != INVALID_SOCKET);
110 mxWSAThrowElse(!connect(machine->networkConnection, (struct sockaddr*)&address, sizeof(address)));
111
112 machine->networkEvent = WSACreateEvent();
113 mxThrowElse(machine->networkEvent);
114
115 machine->networkOverlapped.hEvent = WSACreateEvent();
116 mxWSAThrowElse(machine->networkOverlapped.hEvent);
117 machine->networkBuf.len = mxNetworkBufferSize - 1;
118 machine->networkBuf.buf = machine->networkBuffer;
119
120 fxCountMachines(self);
121
122 fxReadNetwork(machine, fxReadNetworkAux(machine));
123 }
124 machine->receiveCount += 1;
125 return machine;
126}
127
128void fxOpenSerial(txSerialTool self)
129{

Callers

nothing calls this directly

Calls 4

fxReadNetworkAuxFunction · 0.85
fxCountMachinesFunction · 0.70
fxReadNetworkFunction · 0.70
connectFunction · 0.50

Tested by

no test coverage detected