X-Git-Url: http://git.shiar.net/netris.git/blobdiff_plain/968bf843bad5fc3d0c2d96a26fc39f3f01d94bfc..776faa936ea4b3f5ccaacda0f05a2ed1e945d304:/inet.c diff --git a/inet.c b/inet.c index dbfe748..6aadecc 100644 --- a/inet.c +++ b/inet.c @@ -30,6 +30,7 @@ #include #define HEADER_SIZE sizeof(netint2[2]) +#define HEADER_SIZE3 sizeof(netint4[3]) static MyEventType NetGenFunc(EventGenRec *gen, MyEvent *event); @@ -37,7 +38,7 @@ static int sock = -1; static EventGenRec netGen = { NULL, 0, FT_read, -1, NetGenFunc, EM_net }; static char netBuf[64]; -static int netBufSize, netBufGoal = HEADER_SIZE; +static int netBufSize, netBufGoal = HEADER_SIZE3; static int isServer, lostConn, gotEndConn; ExtFunc void InitNet(void) @@ -73,26 +74,34 @@ ExtFunc int WaitForConnection(char *portStr) die("bind"); if (listen(sockListen, 1) < 0) die("listen"); + + // while(1) { addrLen = sizeof(addr); - sock = accept(sockListen, (struct sockaddr *)&addr, &addrLen); - if (sock < 0) + if ((sock = accept(sockListen, (struct sockaddr *)&addr, &addrLen)) < 0) die("accept"); - close(sockListen); - val2.l_onoff = 1; - val2.l_linger = 0; - setsockopt(sock, SOL_SOCKET, SO_LINGER, - (void *)&val2, sizeof(val2)); - netGen.fd = sock; - strcpy(opponentHost, "???"); - if (addr.sin_family == AF_INET) { - host = gethostbyaddr((void *)&addr.sin_addr, - sizeof(struct in_addr), AF_INET); - if (host) { - strncpy(opponentHost, host->h_name, sizeof(opponentHost)-1); - opponentHost[sizeof(opponentHost)-1] = 0; + fprintf(stderr, "Connection: %s\n", inet_ntoa(addr.sin_addr)); + // if (!fork()) { + close(sockListen); + val2.l_onoff = 1; + val2.l_linger = 0; + setsockopt(sock, SOL_SOCKET, SO_LINGER, + (void *)&val2, sizeof(val2)); + netGen.fd = sock; + strcpy(opponentHost, "???"); + if (addr.sin_family == AF_INET) { + host = gethostbyaddr((void *)&addr.sin_addr, + sizeof(struct in_addr), AF_INET); + if (host) { + strncpy(opponentHost, host->h_name, sizeof(opponentHost)-1); + opponentHost[sizeof(opponentHost)-1] = 0; + } } - } - AddEventGen(&netGen); + AddEventGen(&netGen); +// close(sock); + // exit(0); + // } +// close(sock); + // } isServer = 1; return 0; } @@ -137,8 +146,8 @@ ExtFunc int InitiateConnection(char *hostStr, char *portStr) static MyEventType NetGenFunc(EventGenRec *gen, MyEvent *event) { int result; - short type, size; - netint2 data[2]; + short uid, type, size; + netint4 data[3]; result = MyRead(sock, netBuf + netBufSize, netBufGoal - netBufSize); if (result < 0) { @@ -149,16 +158,17 @@ static MyEventType NetGenFunc(EventGenRec *gen, MyEvent *event) if (netBufSize < netBufGoal) return E_none; memcpy(data, netBuf, sizeof(data)); - type = ntoh2(data[0]); - size = ntoh2(data[1]); + uid = ntoh4(data[0]); + type = ntoh4(data[1]); + size = ntoh4(data[2]); netBufGoal = size; if (netBufSize < netBufGoal) return E_none; netBufSize = 0; - netBufGoal = HEADER_SIZE; + netBufGoal = HEADER_SIZE3; event->u.net.type = type; - event->u.net.size = size - HEADER_SIZE; - event->u.net.data = netBuf + HEADER_SIZE; + event->u.net.size = size - HEADER_SIZE3; + event->u.net.data = netBuf + HEADER_SIZE3; if (type == NP_endConn) { gotEndConn = 1; return E_lostConn; @@ -174,13 +184,14 @@ ExtFunc void CheckNetConn(void) { } -ExtFunc void SendPacket(NetPacketType type, int size, void *data) +ExtFunc void SendPacket(short uid, NetPacketType type, int size, void *data) { - netint2 header[2]; + netint4 header[3]; - header[0] = hton2(type); - header[1] = hton2(size + HEADER_SIZE); - if (MyWrite(sock, header, HEADER_SIZE) != HEADER_SIZE) + header[0] = hton4(uid); + header[1] = hton4(type); + header[2] = hton4(size + HEADER_SIZE3); + if (MyWrite(sock, header, HEADER_SIZE3) != HEADER_SIZE3) die("write"); if (size > 0 && data && MyWrite(sock, data, size) != size) die("write"); @@ -192,7 +203,7 @@ ExtFunc void CloseNet(void) if (sock >= 0) { if (!lostConn) { - SendPacket(NP_endConn, 0, NULL); + SendPacket(0, NP_endConn, 0, NULL); if (isServer) { while (!lostConn) WaitMyEvent(&event, EM_net); @@ -200,7 +211,7 @@ ExtFunc void CloseNet(void) else { while (!gotEndConn) WaitMyEvent(&event, EM_net); - SendPacket(NP_byeBye, 0, NULL); + SendPacket(0, NP_byeBye, 0, NULL); } } close(sock);