This commit is contained in:
zhouhuang 2024-08-09 08:50:19 +08:00
parent 48cf136b8d
commit d9f3291d5b
8 changed files with 671 additions and 527 deletions

13
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,13 @@
{
"files.associations": {
"xstring": "cpp",
"unordered_map": "cpp",
"locale": "cpp",
"xlocale": "cpp",
"xlocmes": "cpp",
"xlocmon": "cpp",
"xlocnum": "cpp",
"xloctime": "cpp",
"xhash": "cpp"
}
}

View File

@ -4,7 +4,7 @@ set (VERSION 1.0.1)
# set (CMAKE_CXX_STANDARD 11) # set (CMAKE_CXX_STANDARD 11)
option (USE_MQTT "use mqtt protocol" ON) option (USE_MQTT "use mqtt protocol" ON)
option (USE_WEBSOCKET "use websocket" ON) option (USE_WEBSOCKET "use websocket" OFF)
option (USE_SQLITE3 "use sqlite3" ON) option (USE_SQLITE3 "use sqlite3" ON)
if (USE_SQLITE3) if (USE_SQLITE3)
@ -148,7 +148,7 @@ if (USE_WEBSOCKET)
set (APP_LIBS ${APP_LIBS} ssl crypto) set (APP_LIBS ${APP_LIBS} ssl crypto)
add_definitions(-DUSE_WEBSOCKET) add_definitions(-DUSE_WEBSOCKET)
add_definitions(-DNOPOLL_OS_UNIX=1) add_definitions(-DNOPOLL_OS_UNIX=1)
#add_definitions(-DSHOW_DEBUG_LOG) add_definitions(-DSHOW_DEBUG_LOG)
add_definitions(-DNOPOLL_HAVE_VASPRINTF=1) add_definitions(-DNOPOLL_HAVE_VASPRINTF=1)
add_definitions(-DNOPOLL_HAVE_TLSv10_ENABLED=1) add_definitions(-DNOPOLL_HAVE_TLSv10_ENABLED=1)
add_definitions(-DNOPOLL_HAVE_TLSv11_ENABLED=1) add_definitions(-DNOPOLL_HAVE_TLSv11_ENABLED=1)
@ -158,50 +158,50 @@ endif ()
if (USE_MQTT) if (USE_MQTT)
set(APP_SRCS ${APP_SRCS} set(APP_SRCS ${APP_SRCS}
third_party/mqtt/actions.c third_party/mqtt/actions.c
third_party/mqtt/callbacks.c third_party/mqtt/callbacks.c
third_party/mqtt/connect.c third_party/mqtt/connect.c
third_party/mqtt/handle_auth.c third_party/mqtt/handle_auth.c
third_party/mqtt/handle_connack.c third_party/mqtt/handle_connack.c
third_party/mqtt/handle_disconnect.c third_party/mqtt/handle_disconnect.c
third_party/mqtt/handle_ping.c third_party/mqtt/handle_ping.c
third_party/mqtt/handle_pubackcomp.c third_party/mqtt/handle_pubackcomp.c
third_party/mqtt/handle_publish.c third_party/mqtt/handle_publish.c
third_party/mqtt/handle_pubrec.c third_party/mqtt/handle_pubrec.c
third_party/mqtt/handle_pubrel.c third_party/mqtt/handle_pubrel.c
third_party/mqtt/handle_suback.c third_party/mqtt/handle_suback.c
third_party/mqtt/handle_unsuback.c third_party/mqtt/handle_unsuback.c
third_party/mqtt/helpers.c third_party/mqtt/helpers.c
third_party/mqtt/logging_mosq.c third_party/mqtt/logging_mosq.c
third_party/mqtt/loop.c third_party/mqtt/loop.c
third_party/mqtt/memory_mosq.c third_party/mqtt/memory_mosq.c
third_party/mqtt/messages_mosq.c third_party/mqtt/messages_mosq.c
third_party/mqtt/misc_mosq.c third_party/mqtt/misc_mosq.c
third_party/mqtt/mosquitto.c third_party/mqtt/mosquitto.c
third_party/mqtt/net_mosq_ocsp.c third_party/mqtt/net_mosq_ocsp.c
third_party/mqtt/net_mosq.c third_party/mqtt/net_mosq.c
third_party/mqtt/options.c third_party/mqtt/options.c
third_party/mqtt/packet_datatypes.c third_party/mqtt/packet_datatypes.c
third_party/mqtt/packet_mosq.c third_party/mqtt/packet_mosq.c
third_party/mqtt/property_mosq.c third_party/mqtt/property_mosq.c
third_party/mqtt/read_handle.c third_party/mqtt/read_handle.c
third_party/mqtt/send_connect.c third_party/mqtt/send_connect.c
third_party/mqtt/send_disconnect.c third_party/mqtt/send_disconnect.c
third_party/mqtt/send_mosq.c third_party/mqtt/send_mosq.c
third_party/mqtt/send_publish.c third_party/mqtt/send_publish.c
third_party/mqtt/send_subscribe.c third_party/mqtt/send_subscribe.c
third_party/mqtt/send_unsubscribe.c third_party/mqtt/send_unsubscribe.c
third_party/mqtt/send_mosq.c third_party/mqtt/send_mosq.c
third_party/mqtt/socks_mosq.c third_party/mqtt/socks_mosq.c
third_party/mqtt/srv_mosq.c third_party/mqtt/srv_mosq.c
third_party/mqtt/strings_mosq.c third_party/mqtt/strings_mosq.c
third_party/mqtt/thread_mosq.c third_party/mqtt/thread_mosq.c
third_party/mqtt/time_mosq.c third_party/mqtt/time_mosq.c
third_party/mqtt/tls_mosq.c third_party/mqtt/tls_mosq.c
third_party/mqtt/utf8_mosq.c third_party/mqtt/utf8_mosq.c
third_party/mqtt/util_mosq.c third_party/mqtt/util_mosq.c
third_party/mqtt/util_topic.c third_party/mqtt/util_topic.c
third_party/mqtt/will_mosq.c) third_party/mqtt/will_mosq.c)
# set (APP_SRCS ${APP_SRCS} hwmqtt/hwmqtt.cpp) # set (APP_SRCS ${APP_SRCS} hwmqtt/hwmqtt.cpp)
add_definitions(-DUSE_MQTT) add_definitions(-DUSE_MQTT)
add_definitions(-DWITH_SOCKS) add_definitions(-DWITH_SOCKS)
@ -228,6 +228,7 @@ if (UNIX)
set (APP_LIBS ${APP_LIBS} pthread) set (APP_LIBS ${APP_LIBS} pthread)
set (APP_LIBS ${APP_LIBS} dl) set (APP_LIBS ${APP_LIBS} dl)
set (APP_LIBS ${APP_LIBS} rt) set (APP_LIBS ${APP_LIBS} rt)
#set (APP_LIBS ${APP_LIBS} websockets)
endif() endif()
add_executable (application ${APP_SRCS}) add_executable (application ${APP_SRCS})

File diff suppressed because it is too large Load Diff

View File

@ -9,16 +9,13 @@
static void* main_run_process(void* param) static void* main_run_process(void* param)
{ {
CProcess* proc = (CProcess *)param; CProcess* proc = (CProcess *)param;
DWORD old_ticks = 0; DWORD old_ticks = 0;
while (TRUE) while (TRUE) {
{
if (!proc->m_bRunFlag) break; if (!proc->m_bRunFlag) break;
proc->Run(); proc->Run();
if (old_ticks != system32.ticks) if (old_ticks != system32.ticks) {
{
old_ticks = system32.ticks; old_ticks = system32.ticks;
proc->OnTimer(); //every 10ms proc->OnTimer(); //every 10ms
} }

View File

@ -2255,44 +2255,49 @@ void* idle_process(void* param)
yk(); yk();
yt(); yt();
//操作保存系统操作Log //操作保存系统操作Log
if ((system32.timers % 3600) == 0) if ((system32.timers % 3600) == 0) { //过整点保存
{ //过整点保存 if (system32.log_enabled) {
if (system32.log_enabled)
{
yxbw.DumpYXBW(); yxbw.DumpYXBW();
soe.DumpSOE(); soe.DumpSOE();
yklog.DumpYKLog(); yklog.DumpYKLog();
ytlog.DumpYTLog(); ytlog.DumpYTLog();
} }
yxbw_save = yxbw.GetSavePos(); yxbw_save = yxbw.GetSavePos();
if (yxbw_load != yxbw_save) if (yxbw_load != yxbw_save) { //有变位信息
{ //有变位信息
WriteDatabaseCFG(); WriteDatabaseCFG();
yxbw_load = yxbw_save; yxbw_load = yxbw_save;
} }
} }
for (int i = 0; i < PROCESSES_NUM; i++) for (int i = 0; i < PROCESSES_NUM; i++) {
{
config.processes[i].softdog++; config.processes[i].softdog++;
if (config.processes[i].softdog > PROCESS_WATCHDOG_TIME) if (config.processes[i].softdog > PROCESS_WATCHDOG_TIME) {
{
config.processes[i].softdog = PROCESS_WATCHDOG_TIME; config.processes[i].softdog = PROCESS_WATCHDOG_TIME;
} } else {
else
{
} }
} }
for (int i = 0; i < UNIT_NUM; i++) for (int i = 0; i < UNIT_NUM; i++) {
{
config.units[i].softdog++; config.units[i].softdog++;
if (config.units[i].softdog > UNIT_WATCHDOG_TIME) if (config.units[i].softdog > UNIT_WATCHDOG_TIME) {
{
config.units[i].softdog = UNIT_WATCHDOG_TIME; config.units[i].softdog = UNIT_WATCHDOG_TIME;
config.units[i].value = SPI_ON; config.units[i].value = SPI_ON;
} } else {
else
{
config.units[i].value = SPI_OFF; config.units[i].value = SPI_OFF;
if ((config.units[i].state & 0x80) != 0x80) {
int j = 0;
for (j = 0; j < config.units[i].yxcount; j++) {
if ((config.units[i].yxs[j].qds & 0x80) == 0x80) break;
}
if (j < config.units[i].yxcount) {
continue;
}
for (j = 0; j < config.units[i].yccount; j++) {
if ((config.units[i].ycs[j].qds & 0x80) == 0x80) break;
}
if (j < config.units[i].yccount) {
continue;
}
config.units[i].state |= 0x80;
}
} }
} }
} }

View File

@ -278,38 +278,33 @@ public:
if (point < 0 || point >= pUnit->yxcount) return; if (point < 0 || point >= pUnit->yxcount) return;
udb = pUnit->yxs[point].order; udb = pUnit->yxs[point].order;
if (udb < 0 || udb >= DATABASE_YX_NUM) if (pUnit->yxs[point].invert) {
{ //遥信点号不在数据库定义范围内,只刷新单元数据 value = !value;
if (pUnit->yxs[point].value != value) }
{ //update value
if (udb < 0 || udb >= DATABASE_YX_NUM) { //遥信点号不在数据库定义范围内,只刷新单元数据
if (pUnit->yxs[point].value != value) { //update value
pUnit->yxs[point].value = value; pUnit->yxs[point].value = value;
pUnit->yxs[point].update_time = system32.timers; pUnit->yxs[point].update_time = system32.timers;
pUnit->yxs[point].yxbw = TRUE; pUnit->yxs[point].yxbw = TRUE;
if (bAddYXBW) if (bAddYXBW) {
{
yxbw.PushYXBW(system32.now, udb, value, qds, uid, point, YXBWT_AUTO); yxbw.PushYXBW(system32.now, udb, value, qds, uid, point, YXBWT_AUTO);
} }
} }
} } else if (database.yxs[udb].value != value) { //update value
else if (database.yxs[udb].value != value)
{ //update value
pUnit->yxs[point].value = value; pUnit->yxs[point].value = value;
pUnit->yxs[point].yxbw = TRUE; pUnit->yxs[point].yxbw = TRUE;
pUnit->yxs[point].update_time = system32.timers; pUnit->yxs[point].update_time = system32.timers;
database.yxs[udb].value = value; database.yxs[udb].value = value;
database.yxs[udb].bw_time = system32.timers; //设置刷新时间 database.yxs[udb].bw_time = system32.timers; //设置刷新时间
database.yxs[udb].update_time = system32.timers; //设置刷新时间 database.yxs[udb].update_time = system32.timers; //设置刷新时间
database.yxs[udb].op_unit = uid; database.yxs[udb].op_unit = uid;
database.yxs[udb].qds = qds; database.yxs[udb].qds = qds;
if (bAddYXBW) if (bAddYXBW) {
{
yxbw.PushYXBW(system32.now, udb, value, qds, uid, point, YXBWT_AUTO); yxbw.PushYXBW(system32.now, udb, value, qds, uid, point, YXBWT_AUTO);
} }
} } else {
else if (pUnit->yxs[point].value != value) {
{
if (pUnit->yxs[point].value != value)
{
pUnit->yxs[point].value = value; pUnit->yxs[point].value = value;
pUnit->yxs[point].update_time = system32.timers; pUnit->yxs[point].update_time = system32.timers;
//若数据库中的遥信位置是正确的则不额外产生变位信息 //若数据库中的遥信位置是正确的则不额外产生变位信息
@ -389,23 +384,17 @@ public:
if (point < 0 || point >= pUnit->yccount) return; if (point < 0 || point >= pUnit->yccount) return;
udb = pUnit->ycs[point].order; udb = pUnit->ycs[point].order;
if (udb < 0 || udb >= DATABASE_YC_NUM) if (udb < 0 || udb >= DATABASE_YC_NUM) { //遥测点号不在数据库定义范围内,只刷新本单元数据
{ //遥测点号不在数据库定义范围内,只刷新本单元数据 if (pUnit->ycs[point].value != value) { //update value
if (pUnit->ycs[point].value != value)
{ //update value
pUnit->ycs[point].value = value; pUnit->ycs[point].value = value;
pUnit->ycs[point].update_time = system32.timers; pUnit->ycs[point].update_time = system32.timers;
pUnit->ycs[point].ycbw = TRUE; pUnit->ycs[point].ycbw = TRUE;
} }
} } else if (database.ycs[udb].value != value) { //update value
else if (database.ycs[udb].value != value)
{ //update value
pUnit->ycs[point].value = value; pUnit->ycs[point].value = value;
pUnit->ycs[point].update_time = system32.timers; pUnit->ycs[point].update_time = system32.timers;
if (pUnit->ycs[point].change_pos >= 0 && bAddYCBW) if (pUnit->ycs[point].change_pos >= 0 && bAddYCBW) {
{ if (abs(pUnit->ycs[point].value - database.ycs[udb].value) >= pUnit->ycs[point].change_pos) { //40码值变化量认为是遥测变位
if (abs(pUnit->ycs[point].value - database.ycs[udb].value) >= pUnit->ycs[point].change_pos)
{ //40码值变化量认为是遥测变位
pUnit->ycs[point].ycbw = TRUE; pUnit->ycs[point].ycbw = TRUE;
ycbw.PushYCBW(system32.now, udb, value, qds, uid, point, YCBWT_AUTO); ycbw.PushYCBW(system32.now, udb, value, qds, uid, point, YCBWT_AUTO);
} }
@ -414,11 +403,8 @@ public:
database.ycs[udb].op_unit = uid; database.ycs[udb].op_unit = uid;
database.ycs[udb].update_time = system32.timers; //设置刷新时间 database.ycs[udb].update_time = system32.timers; //设置刷新时间
database.ycs[udb].qds = qds; database.ycs[udb].qds = qds;
} } else {
else if (pUnit->ycs[point].value != value) {
{
if (pUnit->ycs[point].value != value)
{
pUnit->ycs[point].value = value; pUnit->ycs[point].value = value;
pUnit->ycs[point].update_time = system32.timers; pUnit->ycs[point].update_time = system32.timers;
pUnit->ycs[point].ycbw = TRUE; pUnit->ycs[point].ycbw = TRUE;
@ -439,23 +425,17 @@ public:
udb = pUnit->ycs[point].order; udb = pUnit->ycs[point].order;
nvalue = (long)(pUnit->ycs[point].factor * value); nvalue = (long)(pUnit->ycs[point].factor * value);
if (udb < 0 || udb >= DATABASE_YC_NUM) if (udb < 0 || udb >= DATABASE_YC_NUM) { //遥测点号不在数据库定义范围内,只刷新本单元数据
{ //遥测点号不在数据库定义范围内,只刷新本单元数据 if (pUnit->ycs[point].value != nvalue) { //update value
if (pUnit->ycs[point].value != nvalue)
{ //update value
pUnit->ycs[point].value = nvalue; pUnit->ycs[point].value = nvalue;
pUnit->ycs[point].update_time = system32.timers; pUnit->ycs[point].update_time = system32.timers;
pUnit->ycs[point].ycbw = TRUE; pUnit->ycs[point].ycbw = TRUE;
} }
} } else if (database.ycs[udb].value != nvalue) { //update value
else if (database.ycs[udb].value != nvalue)
{ //update value
pUnit->ycs[point].value = nvalue; pUnit->ycs[point].value = nvalue;
pUnit->ycs[point].update_time = system32.timers; pUnit->ycs[point].update_time = system32.timers;
if (pUnit->ycs[point].change_pos >= 0 && bAddYCBW) if (pUnit->ycs[point].change_pos >= 0 && bAddYCBW) {
{ if (abs(pUnit->ycs[point].value - database.ycs[udb].value) >= pUnit->ycs[point].change_pos) { //40码值变化量认为是遥测变位
if (abs(pUnit->ycs[point].value - database.ycs[udb].value) >= pUnit->ycs[point].change_pos)
{ //40码值变化量认为是遥测变位
pUnit->ycs[point].ycbw = TRUE; pUnit->ycs[point].ycbw = TRUE;
ycbw.PushYCBW(system32.now, udb, nvalue, qds, uid, point, YCBWT_AUTO); ycbw.PushYCBW(system32.now, udb, nvalue, qds, uid, point, YCBWT_AUTO);
} }
@ -464,11 +444,8 @@ public:
database.ycs[udb].op_unit = uid; database.ycs[udb].op_unit = uid;
database.ycs[udb].update_time = system32.timers; //设置刷新时间 database.ycs[udb].update_time = system32.timers; //设置刷新时间
database.ycs[udb].qds = qds; database.ycs[udb].qds = qds;
} } else {
else if (pUnit->ycs[point].value != nvalue) {
{
if (pUnit->ycs[point].value != nvalue)
{
pUnit->ycs[point].value = nvalue; pUnit->ycs[point].value = nvalue;
pUnit->ycs[point].update_time = system32.timers; pUnit->ycs[point].update_time = system32.timers;
pUnit->ycs[point].ycbw = TRUE; pUnit->ycs[point].ycbw = TRUE;
@ -501,7 +478,7 @@ public:
pYC->update_time = database.ycs[udb].update_time; pYC->update_time = database.ycs[udb].update_time;
pYC->qds = database.ycs[udb].qds; pYC->qds = database.ycs[udb].qds;
} }
if (pYC->factor > 1 && pUnit->type == 0x00) if (pYC->factor > 1 && (pUnit->type & 0x0f) == 0x00)
{ //系数有效,且为转发单元 { //系数有效,且为转发单元
value /= pYC->factor; value /= pYC->factor;
} }
@ -680,7 +657,8 @@ public:
database.yms[udb].value = value; database.yms[udb].value = value;
database.yms[udb].update_time = system32.timers; //设置刷新时间 database.yms[udb].update_time = system32.timers; //设置刷新时间
database.yms[udb].op_unit = uid; database.yms[udb].op_unit = uid;
}; }
inline DWORD GetUnitYM(int uid, int order) const inline DWORD GetUnitYM(int uid, int order) const
{ {
int udb; int udb;
@ -695,7 +673,8 @@ public:
pUnit->yms[order].value = database.yms[udb].value; pUnit->yms[order].value = database.yms[udb].value;
pUnit->yms[order].update_time = database.yms[udb].update_time; pUnit->yms[order].update_time = database.yms[udb].update_time;
return database.yms[udb].value; return database.yms[udb].value;
}; }
void SetUnitYMQDS(int uid, int point, BYTE qds); void SetUnitYMQDS(int uid, int point, BYTE qds);
BYTE GetUnitYMQDS(int uid, int point) const; BYTE GetUnitYMQDS(int uid, int point) const;
inline float GetUnitYMReal(int uid, int order) const inline float GetUnitYMReal(int uid, int order) const

View File

@ -188,30 +188,14 @@ typedef int SOCKET;
#define PROTOCOL_HOST_MODBUS_TCP 16 //MODBUS tcp主 #define PROTOCOL_HOST_MODBUS_TCP 16 //MODBUS tcp主
#define PROTOCOL_SUB_MODBUS_TCP 17 //MODBUS RTU over tcp从 #define PROTOCOL_SUB_MODBUS_TCP 17 //MODBUS RTU over tcp从
#define PROTOCOL_HOST_MODBUS_RTU_TCP 18 //MODBUS RTU over tcp主 #define PROTOCOL_HOST_MODBUS_RTU_TCP 18 //MODBUS RTU over tcp主
//#define PROTOCOL_HOST_MODBUS_RTU_NCY 19 //NCY-6100系列微机保护装置MODBUS RTU主
//#define PROTOCOL_HOST_MODBUS_RTU_LIYEZG 20 //浙江立业电器MODBUS RTU主
#define PROTOCOL_RTU_STATE 21 //主控状态 #define PROTOCOL_RTU_STATE 21 //主控状态
#define PROTOCOL_LOCAL_DEBUG 22 //本地调试 #define PROTOCOL_LOCAL_DEBUG 22 //本地调试
//#define PROTOCOL_SUB_XT9712 23 //XT9712从
//#define PROTOCOL_HISDATA 24 //历史数据协议
#define PROTOCOL_BF_FTP 25 //倍福FTP数据协议 #define PROTOCOL_BF_FTP 25 //倍福FTP数据协议
//#define PROTOCOL_HOST_NSA 27 //NSA主 #define PROTOCOL_HOST_DLT645V2007 32 //dlt645v2007协议
#define PROTOCOL_HOST_DLT645V2007 30 //dlt645v2007协议
#define PROTOCOL_HOST_DLT645V2007_HR 31 //中电华瑞dlt645v2007协议
#define PROTOCOL_HOST_DLT645V2007_OVERTCP 32 //中电华瑞dlt645v2007 over tcp协议
#define PROTOCOL_CALC 37 //计算 #define PROTOCOL_CALC 37 //计算
//#define PROTOCOL_DLT_1867 40 //需求响应协议dlt1867-2008
#define PROTOCOL_SUB_GDW104 47 //国网104从 #define PROTOCOL_SUB_GDW104 47 //国网104从
//#define PROTOCOL_AGC 50 //功率自动控制app
//#define PROTOCOL_AVC 51 //电压自动控制app
#define PROTOCOL_HW_MQTT 72 //华为物联平台ROMA #define PROTOCOL_HW_MQTT 72 //华为物联平台ROMA
//#define PROTOCOL_ISS_MQTT 73 //软通动力mqtt
//#define PROTOCOL_LM_MQTT 74 //罗米mqtt
//#define PROTOCOL_GRPC_PUBLISH 76 //gRPC上传发布
//#define PROTOCOL_GRPC_SUBSCRIBE 77 //gRPC下载订阅
#define PROTOCOL_OPCUA 78 //opcua协议 #define PROTOCOL_OPCUA 78 //opcua协议
//#define PROTOCOL_HOST_MODBUS_RTU_RDS100AFT 80 //珠海瑞捷电气股份有限公司RD系列保护装置MODBUS RTU主
//#define PROTOCOL_HOST_MODBUS_RTU_APF 81 //江苏沃海电气有限公司APFSVGMODBUS RTU主
#define ADDR_TYPE_NORMAL 0 #define ADDR_TYPE_NORMAL 0
@ -512,6 +496,9 @@ typedef struct
DWORD ticks; DWORD ticks;
DWORD timers; DWORD timers;
DWORD auto_reset_interval; DWORD auto_reset_interval;
char projectName[64]; //项目名称
char version[64]; //配置文件版本信息
} struSystem; } struSystem;
typedef struct typedef struct

View File

@ -289,17 +289,13 @@ NOPOLL_SOCKET __nopoll_conn_sock_connect_opts_internal (noPollCtx * ctx,
nopoll_conn_set_sock_block (session, nopoll_false); nopoll_conn_set_sock_block (session, nopoll_false);
/* do a tcp connect */ /* do a tcp connect */
if (connect (session, res->ai_addr, res->ai_addrlen) < 0) { if (connect (session, res->ai_addr, res->ai_addrlen) < 0) {
if(errno != NOPOLL_EINPROGRESS && errno != NOPOLL_EWOULDBLOCK && errno != NOPOLL_ENOTCONN) { if(errno != NOPOLL_EINPROGRESS && errno != NOPOLL_EWOULDBLOCK && errno != NOPOLL_ENOTCONN) {
nopoll_log (ctx, NOPOLL_LEVEL_WARNING, "unable to connect to remote host %s:%s errno=%d", nopoll_log (ctx, NOPOLL_LEVEL_WARNING, "unable to connect to remote host %s:%s errno=%d", host, port, errno);
host, port, errno); shutdown (session, SHUT_RDWR);
nopoll_close_socket (session);
shutdown (session, SHUT_RDWR);
nopoll_close_socket (session);
/* relase address info */ /* relase address info */
freeaddrinfo (res); freeaddrinfo (res);
return -1; return -1;
} /* end if */ } /* end if */
} /* end if */ } /* end if */
@ -385,6 +381,7 @@ char * __nopoll_conn_get_client_init (noPollConn * conn, noPollConnOpts * opts)
conn->handshake->expected_accept = nopoll_strdup (key); conn->handshake->expected_accept = nopoll_strdup (key);
/* send initial handshake */ /* send initial handshake */
#if 0
return nopoll_strdup_printf ("GET %s HTTP/1.1" return nopoll_strdup_printf ("GET %s HTTP/1.1"
"\r\nHost: %s" "\r\nHost: %s"
"\r\nUpgrade: websocket" "\r\nUpgrade: websocket"
@ -413,6 +410,36 @@ char * __nopoll_conn_get_client_init (noPollConn * conn, noPollConnOpts * opts)
conn->protocols ? conn->protocols : "", conn->protocols ? conn->protocols : "",
/* extra arbitrary headers */ /* extra arbitrary headers */
(opts && opts->extra_headers) ? opts->extra_headers : ""); (opts && opts->extra_headers) ? opts->extra_headers : "");
#else
return nopoll_strdup_printf ("GET %s HTTP/1.1"
"\r\nHost: %s:%s"
"\r\nUpgrade: websocket"
"\r\nConnection: Upgrade"
"\r\nSec-WebSocket-Key: %s"
"\r\nSec-WebSocket-Version: %d"
"%s%s"
"%s%s" /* Cookie */
"%s%s" /* protocol part */
"%s" /* extra arbitrary headers */
"\r\n\r\n",
conn->get_url,
conn->host_name, conn->port,
/* sec-websocket-key */
key,
/* sec-websocket-version */
conn->ctx->protocol_version,
/* Origin (support not sending Origin: header in case it is not defined) */
(conn->origin != NULL && (opts == NULL || opts->add_origin_header)) ? "\r\nOrigin: " : "",
(conn->origin != NULL && (opts == NULL || opts->add_origin_header)) ? conn->origin : "",
/* Cookie */
(opts && opts->cookie) ? "\r\nCookie: " : "",
(opts && opts->cookie) ? opts->cookie : "",
/* protocol part */
conn->protocols ? "\r\nSec-WebSocket-Protocol: " : "",
conn->protocols ? conn->protocols : "",
/* extra arbitrary headers */
(opts && opts->extra_headers) ? opts->extra_headers : "");
#endif
} }
@ -882,6 +909,7 @@ noPollConn * __nopoll_conn_new_common (noPollCtx * ctx,
/* get client init payload */ /* get client init payload */
content = __nopoll_conn_get_client_init (conn, options); content = __nopoll_conn_get_client_init (conn, options);
if (content == NULL) { if (content == NULL) {
nopoll_log (ctx, NOPOLL_LEVEL_CRITICAL, "Failed to build client init message, unable to connect"); nopoll_log (ctx, NOPOLL_LEVEL_CRITICAL, "Failed to build client init message, unable to connect");
nopoll_conn_shutdown (conn); nopoll_conn_shutdown (conn);
@ -1041,15 +1069,19 @@ noPollConn * __nopoll_conn_new_common (noPollCtx * ctx,
/* call to send content */ /* call to send content */
remaining_timeout = ctx->conn_connect_std_timeout; remaining_timeout = ctx->conn_connect_std_timeout;
while (remaining_timeout > 0) { while (remaining_timeout > 0) {
if (size != conn->send (conn, content, size)) { int sent_size = conn->send (conn, content, size);
//fprintf(stderr, "here sent size is: %d.\n", sent_size);
if (size != sent_size) {
/* for some reason, under FreeBSD, a ENOTCONN is reported when they should be returning EINPROGRESS and/or EWOULDBLOCK */ /* for some reason, under FreeBSD, a ENOTCONN is reported when they should be returning EINPROGRESS and/or EWOULDBLOCK */
if (errno == NOPOLL_EWOULDBLOCK || errno == NOPOLL_EINPROGRESS || errno == NOPOLL_ENOTCONN) { if (errno == NOPOLL_EWOULDBLOCK || errno == NOPOLL_EINPROGRESS || errno == NOPOLL_ENOTCONN) {
/* nopoll_log (ctx, NOPOLL_LEVEL_CRITICAL, "Connection in progress (errno=%d), session: %d", errno, session); */ /* nopoll_log (ctx, NOPOLL_LEVEL_CRITICAL, "Connection in progress (errno=%d), session: %d", errno, session); */
//fprintf(stderr, "Connection in progress (errno=%d,%s), session: %d\n", errno, strerror(errno), session);
nopoll_sleep (10000); nopoll_sleep (10000);
remaining_timeout -= 10000; remaining_timeout -= 10000;
continue; continue;
} /* end if */ } /* end if */
//fprintf(stderr, "Failed to send websocket init message, error code was: %d,%s (2), closing session\n", errno, strerror(errno));
nopoll_log (ctx, NOPOLL_LEVEL_CRITICAL, "Failed to send websocket init message, error code was: %d (2), closing session", errno); nopoll_log (ctx, NOPOLL_LEVEL_CRITICAL, "Failed to send websocket init message, error code was: %d (2), closing session", errno);
nopoll_conn_shutdown (conn); nopoll_conn_shutdown (conn);
} /* end if */ } /* end if */
@ -1057,6 +1089,7 @@ noPollConn * __nopoll_conn_new_common (noPollCtx * ctx,
break; break;
} }
//fprintf (stderr, "Web socket initial client handshake sent");
nopoll_log (ctx, NOPOLL_LEVEL_DEBUG, "Web socket initial client handshake sent"); nopoll_log (ctx, NOPOLL_LEVEL_DEBUG, "Web socket initial client handshake sent");
/* release content */ /* release content */