Skip to content

Commit

Permalink
Added tcp server
Browse files Browse the repository at this point in the history
  • Loading branch information
yan9a authored and yana committed Apr 30, 2024
1 parent 9048230 commit b67a909
Show file tree
Hide file tree
Showing 15 changed files with 919 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ ${PROJECT_SOURCE_DIR}/src/ceLog.cpp
${PROJECT_SOURCE_DIR}/src/ceSerial.cpp
${PROJECT_SOURCE_DIR}/src/ceWxTmr.cpp
${PROJECT_SOURCE_DIR}/src/ceUDP.cpp
${PROJECT_SOURCE_DIR}/src/ceTcpServer.cpp
${PROJECT_SOURCE_DIR}/src/ceMisc.cpp
${PROJECT_SOURCE_DIR}/src/ceFrame.cpp
${PROJECT_SOURCE_DIR}/src/ceFraChecksum.cpp
Expand Down
12 changes: 12 additions & 0 deletions examples/tcpsvr/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
cmake_minimum_required(VERSION 3.0.0)
# directory name to be used as project name
project(tcpsvr VERSION 1.0.1)
set(ADDCV NO)
# CMakeM1 checks ADDCV variable whether to include OpenCV
# and return LIBS variable with the list of libraries to link
# and also set WIN32OPT variable with WIN32 for Windows
include(${PROJECT_SOURCE_DIR}/../../CMakeM1.txt)
add_executable(${PROJECT_NAME} ${WIN32OPT}
${PROJECT_SOURCE_DIR}/src/${PROJECT_NAME}.cpp
)
target_link_libraries(${PROJECT_NAME} ${LIBS})
118 changes: 118 additions & 0 deletions examples/tcpsvr/src/myicon.xpm
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/* XPM */
static char const* sample_xpm[] = {
"64 64 51 1",
" c None",
". c #008000",
"+ c #007F00",
"@ c #007900",
"# c #007D00",
"$ c #6BB56B",
"% c #FDFEFD",
"& c #54AA54",
"* c #FFFFFF",
"= c #007A00",
"- c #F0F7F0",
"; c #309830",
"> c #2F982F",
", c #69B469",
"' c #68B468",
") c #7EBF7E",
"! c #80C080",
"~ c #018001",
"{ c #0B850B",
"] c #72B872",
"^ c #7CBE7C",
"/ c #3A9D3A",
"( c #71B871",
"_ c #FEFEFE",
": c #E4F2E4",
"< c #359A35",
"[ c #007E00",
"} c #E5F2E5",
"| c #68B368",
"1 c #6FB76F",
"2 c #7BBD7B",
"3 c #399D39",
"4 c #349A34",
"5 c #70B870",
"6 c #67B367",
"7 c #6DB66D",
"8 c #71B971",
"9 c #79BC79",
"0 c #D8EBD8",
"a c #D7EBD7",
"b c #7ABD7A",
"c c #D5EAD5",
"d c #D6EBD6",
"e c #55AA55",
"f c #6AB56A",
"g c #FCFEFC",
"h c #6AB46A",
"i c #53A953",
"j c #2F972F",
"k c #6EB76E",
"l c #7DBF7D",
" ................................................ ",
" ...................................................... ",
" ........................................................ ",
" ...........................+@@+........................... ",
" ...........................#$%%$#........................... ",
" ............................&****&............................ ",
" ...........................=-****-=........................... ",
" ...........................;******>........................... ",
"............................,******'............................",
"............................)******)............................",
"............................!******!............................",
"...........~{]^/=...........!******!...........=/^({~...........",
"...........{_***:<[.........!******!.........[<}***_{...........",
"...........(******|#........!******!........#'******(...........",
"...........^*******(#.......!******!.......#1*******2...........",
".........../********(#......!******!......#1********3...........",
"...........=:********(#.....!******!.....#1********:=...........",
"............4*********5#....!******!....#1*********<............",
"............[6*********(#...!******!...#1*********'[............",
".............#7*********8#..!******!..#1*********1#.............",
"..............#7*********8#.!******!.#1*********1#..............",
"...............#7*********8#!******!#1*********1#...............",
"................#7*********89******91*********1#................",
".................#7*********0******a*********1#.................",
"..................#7************************1#..................",
"...................#7**********************1#...................",
"....................#7********************1#....................",
".....................#7******************1#.....................",
"......=;,)!!!!!!!!!!!!bc****************d9!!!!!!!!!!!!),;=......",
"....#e-**************************************************-&#....",
"...+$******************************************************f+...",
"...@%******************************************************%@...",
"...@%******************************************************g@...",
"...+f******************************************************h+...",
"....#&-**************************************************-i#....",
"......=>')!!!!!!!!!!!!9a****************09!!!!!!!!!!!!)'j=......",
".....................#1******************8#.....................",
"....................#1********************8#....................",
"...................#1**********************8#...................",
"..................#1************************8#..................",
".................#1*********d******c*********8#.................",
"................#1*********19******b7*********8#................",
"...............#1*********1#!******!#7*********8#...............",
"..............#1*********1#.!******!.#7*********8#..............",
".............#1*********1#..!******!..#7*********8#.............",
"............[|*********1#...!******!...#7*********,[............",
"............4*********1#....!******!....#k*********<............",
"...........=}********1#.....!******!.....#k********}=...........",
".........../********1#......!******!......#k********/...........",
"...........^*******1#.......!******!.......#k*******2...........",
"...........(******|#........!******!........#6******5...........",
"...........{_***:4[.........!******!.........[<:***_{...........",
"...........~{523=...........!******!...........=32({~...........",
"............................!******!............................",
"............................)******l............................",
"............................,******'............................",
" ...........................;******j........................... ",
" ...........................=-****-=........................... ",
" ............................&****&............................ ",
" ...........................#f%gh#........................... ",
" ...........................+@@+........................... ",
" ........................................................ ",
" ...................................................... ",
" ................................................ "};
177 changes: 177 additions & 0 deletions examples/tcpsvr/src/tcpsvr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
// File: tcpsvr.cpp
// Description: A simple wxWidgets TCP server sample
// Author: Yan Naing Aye
// Web: http://cool-emerald.blogspot.com
// MIT License (https://opensource.org/licenses/MIT)
// Copyright (c) 2018 Yan Naing Aye

// References
// [1] Guillermo Rodriguez Garcia, Vadim Zeitlin, "Server for wxSocket demo",
// https://github.com/wxWidgets/wxWidgets/blob/master/samples/sockets/server.cpp, 2009.
// [2] Julian Smart and Kevin Hock, "Cross-Platform GUI Programming with wxWidgets,"
// Pearson Education, Inc. 2006. ISBN: 0-13-147381-6.

#include "ce/ceUtil.h"
#include "myicon.xpm"
using namespace ce;
// IDs for the controls and the menu commands
enum
{
ID_TXTRX=101,
SOCKET_ID,
SERVER_ID,
// menu items
Minimal_Quit = wxID_EXIT,
Minimal_About = wxID_ABOUT
};

class MyFrame;
class MyApp : public wxApp
{
ceTcpServer* _tcpsvr;
MyFrame *frame;
public:
void OnTcpServerEvent(wxThreadEvent& event);
void OnTcpSocketEvent(wxThreadEvent& event);
virtual bool OnInit();
};

// Define a new frame type: this is going to be our main frame
class MyFrame : public wxFrame
{
public:
// ctor(s)
MyFrame(const wxString& title);
~MyFrame();
// event handlers (these functions should _not_ be virtual)
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
void Print(std::string str);
private:
wxTextCtrl *txtRx;
// any class wishing to process wxWidgets events must use this macro
wxDECLARE_EVENT_TABLE();
};

// event tables and other macros for wxWidgets
wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
EVT_MENU(Minimal_About, MyFrame::OnAbout)
wxEND_EVENT_TABLE()

IMPLEMENT_APP(MyApp)

// 'Main program' equivalent: the program execution "starts" here
bool MyApp::OnInit()
{
if ( !wxApp::OnInit() )
return false;

// depending on your system, some ports such as 3000, 8000, 8080 should be used with cautions
this->_tcpsvr = new ceTcpServer(this,SERVER_ID,SOCKET_ID,7225);
this->_tcpsvr->Open();
Connect(SERVER_ID, wxEVT_THREAD, wxThreadEventHandler(MyApp::OnTcpServerEvent));
Connect(SOCKET_ID, wxEVT_THREAD, wxThreadEventHandler(MyApp::OnTcpSocketEvent));
frame = new MyFrame("TCP Server using ceUtil lib");
frame->Show(true);
this->frame->Print("TCP Server listening");
return true;
}

void MyApp::OnTcpServerEvent(wxThreadEvent& event)
{
std::vector<char> v = event.GetPayload<std::vector<char>>();
std::string s = event.GetString().ToStdString();
int i = event.GetInt();
std::string str = "Server event: ip ="+s+", port = "+ std::to_string(i)+ (v[0]?", connected, clients: ":", disconnected, clients: ") + std::to_string(v[1]);
printf("%s\n",str.c_str());
this->frame->Print(str);
}

void MyApp::OnTcpSocketEvent(wxThreadEvent& event)
{
std::vector<char> v = event.GetPayload<std::vector<char>>();
std::string s = event.GetString().ToStdString();
int i = event.GetInt();
std::string str = "Socket event: Rx vec = "+ceMisc::cvec2hex(v)+", ip ="+s+", port = "+ std::to_string(i);
printf("%s\n",str.c_str());
this->frame->Print(str);

// write back
std::vector<char> tb{'O','K'};
this->_tcpsvr->Tx(tb);
this->frame->Print("Tx: "+ ce::ceMisc::cvec2hex(tb));
}

// frame constructor
MyFrame::MyFrame(const wxString& title)
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(390, 280),
wxDEFAULT_FRAME_STYLE ^ wxRESIZE_BORDER)
{
// set the frame icon
SetIcon(wxICON(sample));

#if wxUSE_MENUS
// create a menu bar
wxMenu *fileMenu = new wxMenu;

// the "About" item should be in the help menu
wxMenu *helpMenu = new wxMenu;
helpMenu->Append(Minimal_About, "&About\tF1", "Show about dialog");

fileMenu->Append(Minimal_Quit, "E&xit\tAlt-X", "Quit this program");

// now append the freshly created menu to the menu bar...
wxMenuBar *menuBar = new wxMenuBar();
menuBar->Append(fileMenu, "&File");
menuBar->Append(helpMenu, "&Help");

// ... and attach this menu bar to the frame
SetMenuBar(menuBar);
#endif // wxUSE_MENUS

#if wxUSE_STATUSBAR
// create a status bar just for fun (by default with 1 pane only)
CreateStatusBar(2);
SetStatusText("TCP server using ceUtil lib");
#endif // wxUSE_STATUSBAR
txtRx = new wxTextCtrl(this, ID_TXTRX, wxT(""), wxPoint(5, 5),
wxSize(365, 125), wxTE_MULTILINE);


}

MyFrame::~MyFrame()
{
// No delayed deletion here, as the frame is dying anyway
//delete _tcpsvr;
}

// event handlers
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
// true is to force the frame to close
Close(true);
}

void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
wxMessageBox(wxString::Format
(
"ceUtil TCP server sample\n"
"\n"
"Author: Yan Naing Aye \n"
"Web: http://cool-emerald.blogspot.com"
),
"About ceUtil TCP server sample",
wxOK | wxICON_INFORMATION,
this);
}

void MyFrame::Print(std::string str)
{
txtRx->AppendText(wxString::Format(wxT("%s\n"),str));
}



60 changes: 60 additions & 0 deletions examples/tcpsvr/tcpsvr.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/bin/bash

SCRIPTNAME=`basename "$0"`
PRJNAME="${SCRIPTNAME%.*}"
SCRIPTDIR="${0%/$PRJNAME.*}"
echo "Project: $PRJNAME"
echo "Script directory: $SCRIPTDIR"

if [ $# == 1 ]; then
opt_sh=$1
else
echo "You can input argument:"
echo " 'cmake' : to generate cmake files, build, and install"
echo " 'build': to build and run"
echo " ..."
read -p "Input an option: " opt_sh
fi

if [[ "$opt_sh" == "" ]]; then
opt_sh="build"
fi

echo "Option: $opt_sh"
echo " ."
echo " ."
echo " ."

cd $SCRIPTDIR
if [[ "$opt_sh" == "cmake" ]]; then
echo "Preparing cmake file"
if [[ ! -d "./build" ]]; then
mkdir -p build
fi
rm -r ./build/*
cd build
cmake -D CMAKE_BUILD_TYPE=Release \
-D CMAKE_INSTALL_PREFIX=/usr/local \
..
cd ..
echo " ."
echo " ."
echo " ."
fi

if [[ $opt_sh == "cmake" ]] || [[ $opt_sh == "build" ]]; then
echo "Building ..."
cd $SCRIPTDIR/build && make # && sudo make install
if [[ $? == 0 ]]; then
echo "Build successful"
echo "Running ..."
./$PRJNAME
else
echo "Error in compiling"
fi
else
echo "Running ..."
./$PRJNAME
fi


Loading

0 comments on commit b67a909

Please sign in to comment.