mongoose library

Headers, Bindings, Libraries for use with FreeBASIC, Please include example of use to help ensure they are tested and usable.
Post Reply
fan2006
Posts: 32
Joined: Jun 07, 2020 3:05

mongoose library

Post by fan2006 »

Mongoose - Embedded Web Server / Embedded Network Library.Mongoose is a network library for C/C++. It provides event-driven non-blocking APIs for TCP, UDP, HTTP, WebSocket, MQTT, and other protocols.

source from :https://github.com/cesanta/mongoose/
Easy to integrate: just copy mongoose.c and mongoose.h files to your source tree.
compile static lib with cmdline:

Code: Select all

gcc -c mongoose.c -DMG_ENABLE_MBEDTLS=1 -I ../mbedtls/include
ar rcs libmongoose.a mongoose.o
MG_ENABLE_MBEDTLS=1 means we use medtls(need related mbedtls static lib when this definition is used). because mongoose has built-in tls,so you can -DMG_TLS_BUILTIN=3 to use built-in tls.
when use fbfrog to generate the bi file,we need to define the MG_ARCH LISTED.

Code: Select all

#define MG_ARCH_CUSTOM 0        // User creates its own mongoose_config.h
#define MG_ARCH_UNIX 1          // Linux, BSD, Mac, ...
#define MG_ARCH_WIN32 2         // Windows
#define MG_ARCH_ESP32 3         // ESP32
#define MG_ARCH_ESP8266 4       // ESP8266
#define MG_ARCH_FREERTOS 5      // FreeRTOS
#define MG_ARCH_AZURERTOS 6     // MS Azure RTOS
#define MG_ARCH_ZEPHYR 7        // Zephyr RTOS
#define MG_ARCH_NEWLIB 8        // Bare metal ARM
#define MG_ARCH_CMSIS_RTOS1 9   // CMSIS-RTOS API v1 (Keil RTX)
#define MG_ARCH_TIRTOS 10       // Texas Semi TI-RTOS
#define MG_ARCH_PICOSDK 11      // Raspberry Pi Pico-SDK (RP2040, RP2350)
#define MG_ARCH_ARMCC 12        // Keil MDK-Core with Configuration Wizard
#define MG_ARCH_CMSIS_RTOS2 13  // CMSIS-RTOS API v2 (Keil RTX5, FreeRTOS)
#define MG_ARCH_RTTHREAD 14     // RT-Thread RTOS
#define MG_ARCH_ARMCGT 15       // Texas Semi ARM-CGT
JUSE LIKE :

Code: Select all

fbfrog.exe mongoose.h  -removeinclude xxx.h  -define MG_TLS_BUILTIN 3 -define MG_ARCH MG_ARCH_WIN32 -rename_ type
with some fix to the bi,the examples are running fine .
i just get the bi and test for windows platform.
bi and test files download
fan2006
Posts: 32
Joined: Jun 07, 2020 3:05

Re: mongoose library

Post by fan2006 »

this is an http client example in mongoose tutorials translated to freebasic.

Code: Select all

#include once "mongoose.bi"
#include once "crt/stdlib.bi"
#inclib "msvcr100" '/wstat32i36/'
#ifndef __FB_64BIT__
#libpath "win32"
#else
#libpath "win64"
#endif
Dim Shared s_url As  ZString Ptr = @"http://info.cern.ch/"
Dim Shared s_post_data As  ZString Ptr = NULL
Dim Shared s_timeout_ms As  ULongInt = 1500

Private Sub fn(ByVal c As mg_connection Ptr, ByVal ev As Long, ByVal ev_data As Any Ptr)
   If ev = MG_EV_OPEN Then
   *CPtr(ULongInt Ptr, @c->data_)= mg_millis() + s_timeout_ms  
   ElseIf ev = MG_EV_POLL Then
      If ((mg_millis() > (*CPtr(ULongInt Ptr, @c->data_))) AndAlso (c->is_connecting OrElse c->is_resolving)) Then
         Print "Connect timeout"
      End If
   ElseIf ev = MG_EV_CONNECT Then
      Dim host As mg_str = mg_url_host(s_url)
      If mg_url_is_ssl(s_url) Then
         Dim opts As mg_tls_opts
         opts.ca = mg_unpacked(Cast(ZString Ptr,@"/certs/ca.pem"))
         opts.name_ = mg_url_host(s_url)
         mg_tls_init(c, @opts)
      End If
      Dim content_length As Long = IIf(s_post_data, strlen(s_post_data), 0) 
    
      mg_printf(c,@!"%s %s HTTP/1.0\r\nHost: %.*s\r\nContent-Type: octet-stream\r\nContent-Length: %d\r\n\r\n", IIf(s_post_data, "POST", "GET"), mg_url_uri(s_url), CLng(host.len_), host.buf, content_length)
      mg_send(c, Cast(Any Ptr, s_post_data), content_length)
   ElseIf ev = MG_EV_HTTP_MSG Then
      Dim hm As mg_http_message Ptr =CPtr(mg_http_message Ptr, ev_data)
     printf("%.*s", CLng(hm->message.len_), *hm->message.buf)
     c->is_draining = 1
      *CPtr(BOOL Ptr, @c->fn_data) = True'
   ElseIf ev = MG_EV_ERROR Then
      *CPtr(BOOL Ptr, @c->fn_data) = True
   End If
End Sub

   Dim log_level As  ZString Ptr = getenv("LOG_LEVEL")
   If log_level = NULL Then
      log_level = @"4"
   End If
   Dim mgr As mg_mgr 
   Dim As BOOL done =0
   mg_log_set(MG_LL_DEBUG)
   mg_mgr_init(@mgr)  
   mg_http_connect(@mgr, s_url, Cast(Any Ptr, @fn),@done)
   While done = 0
    mg_mgr_poll(@mgr, 500)   
   Wend
    mg_mgr_free(@mgr)
 Sleep
fan2006
Posts: 32
Joined: Jun 07, 2020 3:05

Re: mongoose library

Post by fan2006 »

websocket server /client examples are included in the downloaded files.so i will stop here.
Post Reply