Browse Source

CharConv demo working now

Signed-off-by: surenyi <surenyi@gmail.com>
master
surenyi 9 years ago
parent
commit
18356d7efc
  1. 2
      CRC32.cpp
  2. 10
      CharConv.cpp
  3. 17
      Lan.cpp
  4. 13
      Runtime.cpp
  5. 26
      Udp.cpp
  6. 11
      Udp.h
  7. 8
      Wan.cpp
  8. 3
      makefile

2
CRC32.cpp

@ -36,7 +36,7 @@ Crc32::Crc32()
uint32_t Crc32::crc(uint8_t *buffer, int len)
{
uint32_t r = 0xffffffff;
uint32_t r = 0;
r = doCrc(r, buffer, len);
return r;
}

10
CharConv.cpp

@ -59,16 +59,16 @@ void CharConv::onAlarm(const char *str, int len)
buf[idx++] = 0x68;
buf[idx++] = 0x00;
buf[idx++] = 0x00;
buf[idx++] = 0x00;
buf[idx++] = (len >> 8) & 0xff;
buf[idx++] = 0x80;
buf[idx++] = len & 0xff;
buf[idx++] = (len >> 8) & 0xff;
memcpy(buf + idx, str, len);
idx += len;
uint32_t cs = _crc.crc((uint8_t *)str, len);
buf[idx++] = (cs >> 24) & 0xff;
buf[idx++] = (cs >> 16) & 0xff;
buf[idx++] = (cs >> 8) & 0xff;
buf[idx++] = cs & 0xff;
buf[idx++] = (cs >> 8) & 0xff;
buf[idx++] = (cs >> 16) & 0xff;
buf[idx++] = (cs >> 24) & 0xff;
int n = _udp.send(buf, idx);
Log.printf("conv sending: %d bytes\n", n);

17
Lan.cpp

@ -44,24 +44,30 @@ int Lan::begin(const char *group_addr, int port, const char *local_if)
void Lan::doCmd(uint8_t *buf, int len)
{
struct MsgCmd hdr;
struct MsgCmd hdr, *sdp;
struct MsgTermStatus *mts;
uint8_t out;
uint16_t cmd;
uint8_t *dp;
hdr = *(struct MsgCmd *)(buf);
sdp = (struct MsgCmd *)(buf);
hdr = *sdp;
cmd = ntohs(hdr.msgCmd);
Log.printf("Lan cmd [%02x]\n", cmd);
// Log.printf("Lan cmd [%02x]\n", cmd);
switch (cmd) {
case MSG_CMD_HANDSHAKE:
hdr.sender = sdp->recver;
hdr.recver = sdp->sender;
hdr.msgCmd = htons(MSG_RES_HANDSHAKE);
hdr.seqNum = htonl(++_myseq);
#if 0
do {
uint8_t *dp;
dp = (uint8_t *)&hdr;
for (int x= 0; x < 12; ++x) {
Log.printf("%02x ", dp[x]);
}
Log.printf("\n");
} while (0);
#endif
send(&hdr, sizeof hdr);
len -= sizeof hdr;
buf += sizeof hdr;
@ -187,7 +193,6 @@ int Lan::onResponse(int resp_type, struct MsgCmd *hdr, int status)
pkt.msgCmd = htons(MSG_RES_PTTOFF);
break;
}
pkt.seqNum = htonl(++_myseq);
send(&pkt, sizeof pkt);
return 0;

13
Runtime.cpp

@ -25,10 +25,17 @@
* add route for eth1
* route add -net 192.168.31.0/24 dev eth1
*/
#if 1
#define WAN_IF "eth1"
#define WAN_GROUP "225.0.0.5"
#define WAN_GROUP_PORT 8300
#define WAN_STATUS_PORT 8100
#else
#define WAN_IF "eth0"
#define WAN_GROUP "224.0.0.24"
#define WAN_GROUP_PORT 8300
#define WAN_STATUS_PORT 5100
#endif
Runtime::Runtime()
{
@ -50,7 +57,7 @@ Runtime::Runtime()
config = event_config_new();
event_config_avoid_method(config, "select");
/* event_config_require_features(config, EV_FEATURE_ET); */
event_config_require_features(config, EV_FEATURE_ET);
_evbase = event_base_new_with_config(config);
event_config_free(config);
}
@ -125,7 +132,7 @@ public:
virtual void onTimer()
{
char buf[128];
snprintf(buf, sizeof buf, "数量");
snprintf(buf, sizeof buf, "数量这么多数据还是不错");
_c->onAlarm(buf, strlen(buf));
}
};
@ -159,7 +166,7 @@ void Runtime::begin()
exit(-4);
}
#if 0
#if 1
static AlarmTimer at(&_conv);
at.begin();
at.active(1000);

26
Udp.cpp

@ -1,17 +1,20 @@
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <event2/util.h>
#include "Udp.h"
#include "Log.h"
Udp::Udp()
:sock(-1), isMcast(false)
{
memset(&destAddr, 0, sizeof destAddr);
}
int Udp::bind(const char *addr, int p)
{
struct sockaddr_in local_addr;
@ -28,9 +31,11 @@ int Udp::open()
{
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock >= 0) {
evutil_make_socket_nonblocking(sock);
int reuse = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof reuse);
setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &reuse, sizeof reuse);
isMcast = false;
return 0;
}
return -1;
@ -47,7 +52,12 @@ int Udp::close()
int Udp::send(const void *buf, int len)
{
if (isMcast) {
return sendto(sock, buf, len, 0, (struct sockaddr *)&destAddr,
sizeof destAddr);
} else {
return write(sock, buf, len);
}
}
int Udp::recv(void *buf, int len)
@ -79,6 +89,7 @@ int Udp::openMulticast(const char *grp_addr, int port, const char *local)
goto done;
}
evutil_make_socket_nonblocking(sock);
do {
int reuse = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof reuse);
@ -117,6 +128,13 @@ int Udp::openMulticast(const char *grp_addr, int port, const char *local)
setsockopt(sock, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof loop);
} while (0);
memset(&destAddr, 0, sizeof destAddr);
destAddr.sin_family = AF_INET;
destAddr.sin_port = htons(port);
destAddr.sin_addr.s_addr = inet_addr(grp_addr);
isMcast = true;
sm = 0;
done:
return sm;

11
Udp.h

@ -1,11 +1,18 @@
#ifndef __UDP_H___
#define __UDP_H___
#include <netinet/in.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
class Udp {
private:
protected:
int sock;
struct sockaddr_in destAddr;
bool isMcast;
public:
Udp() : sock(-1) {}
Udp();
Udp(const Udp &_u) { sock = _u.sock;}
int getFd() { return sock;}
int open();

8
Wan.cpp

@ -1,4 +1,5 @@
#include <time.h>
#include <errno.h>
#include <string.h>
#include "UdpEvent.h"
#include "Wan.h"
@ -102,7 +103,10 @@ void Wan::onStatus(uint8_t id, uint8_t status)
}
_stBuf.checkNum = cs & 0xff;
_stSock.send(&_stBuf, sizeof _stBuf);
// Log.printf("onStatus-> id %02x, status %d\n", id, status);
len = _stSock.send(&_stBuf, sizeof _stBuf);
if (len < 0) {
Log.printf("%s:%d errno (%d) %s\n", __FILE__, __LINE__, errno, strerror(errno));
}
// Log.printf("status sending %d bytes\n", len);
}

3
makefile

@ -5,7 +5,8 @@ CC := cc
INCLUDE := $(TARGET_ROOT)/include
LIBDIR := $(TARGET_ROOT)/lib
DEBUG := -g -ggdb
#DEBUG := -g -ggdb
DEBUG := -O2 -s
CFLAGS += -D_GNU_SOURCE -I$(PWD) -I$(PWD)/class -I$(INCLUDE) -I$(INCLUDE)/luajit-2.0 -Wall -Wextra $(DEBUG)
CXXFLAGS += $(CFLAGS)

Loading…
Cancel
Save