Skip to content

Instantly share code, notes, and snippets.

@aorjoa
Created November 14, 2012 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aorjoa/4073185 to your computer and use it in GitHub Desktop.
Save aorjoa/4073185 to your computer and use it in GitHub Desktop.
/*
* libwebsockets-test-server - libwebsockets test implementation
*
* Copyright (C) 2010-2011 Andy Green <andy@warmcat.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation:
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
* Develope by Bhuridech Sudsee
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <iostream>
#include <C:/libwebsockets/lib/libwebsockets.h>
#include "websock-w32.h"
#include "gettimeofday.h"
#include <winsock.h>
#include <mysql.h>
using namespace std;
//MySQL Declaration
MYSQL* conn;
MYSQL_RES *res;
MYSQL_ROW row;
unsigned int numrows;
const char* query;
string check;
size_t lens;
#ifndef DATADIR
#define DATADIR "."
#endif
void sentB(unsigned char*p);
struct libwebsocket *client[100];
unsigned int num_client = 0;
string email,password,lemail,lpassword,code,q;
unsigned char *pass;
static int call_back_logic(struct libwebsocket_context *e,
struct libwebsocket *wsi,
enum libwebsocket_callback_reasons reason,
void *user, void *in, size_t len)
{
switch (reason) {
case LWS_CALLBACK_ESTABLISHED: // just log message that someone is connecting
printf("connection established\n");
client[num_client++] = wsi;
break;
case LWS_CALLBACK_RECEIVE: {
string text = (char *)in;
unsigned char *buf = (unsigned char*) malloc(LWS_SEND_BUFFER_PRE_PADDING + len +
LWS_SEND_BUFFER_POST_PADDING);
unsigned int i=0;
lens=len;
for (i=0; i < len; i++) {
buf[LWS_SEND_BUFFER_PRE_PADDING + i ] = ((char *) in)[i];
}
if(text.find("RegHeader|Email")==0){
email=text.replace(text.find("RegHeader|Email"), sizeof("RegHeader|Email")-1, "");
}
if(text.find("RegHeader|PassWord")==0){
password=text.replace(text.find("RegHeader|PassWord"), sizeof("RegHeader|PassWord")-1, "");
}
if(text.find("RegHeader|Code")==0){
code=text.replace(text.find("RegHeader|Code"), sizeof("RegHeader|Code")-1, "");
}
if(text.find("LogIn|Email") == 0){
lemail=text.replace(text.find("LogIn|Email"), sizeof("LogIn|Email")-1, "");
}
if(text.find("LogIn|PassWord") == 0){
lpassword=text.replace(text.find("LogIn|PassWord"), sizeof("LogIn|PassWord")-1, "");
}
if(email.length()>0 && password.length()>0 && code.length()>0){
q="SELECT email FROM project.member WHERE email=";
q+="'";q+=email;q+="'";
query = q.c_str();
mysql_query(conn,query);
res = mysql_store_result(conn);
numrows = (unsigned int)mysql_num_rows(res);
q="";query="";
if(numrows>0){
check="failcon";
}else{
q="";query="";
q="INSERT INTO project.member(email,password,code) VALUES (";
q+="'";q+=email;q+="'";q+=",";
q+="'";q+=password;q+="'";q+=",";
q+="'";q+=code;q+="'";q+=")";
query = q.c_str();
mysql_query(conn,query);
q="SELECT codepromo FROM project.code WHERE codepromo=";
q+="'";q+=code;q+="'";
query = q.c_str();
mysql_query(conn,query);
res = mysql_store_result(conn);
if((unsigned int)mysql_num_rows(res)>0){
q="UPDATE project.member SET class='admin' WHERE email=";
q+="'";q+=email;q+="'";
query = q.c_str();
mysql_query(conn,query);
}
check="registed";
}
}else if(lemail.length()>0 && lpassword.length()>0){
q="";query="";
q="SELECT email FROM project.member WHERE email=";
q+="'";q+=lemail;q+="'";q+=" AND ";
q+="password = '";q+=lpassword;q+="'";
query = q.c_str();
mysql_query(conn,query);
res = mysql_store_result(conn);
numrows=0;
numrows = (unsigned int)mysql_num_rows(res);
if(numrows>0){
cout<<"Correct!"<<endl;
check="correct";
q="";query="";
q="SELECT email FROM project.member WHERE email=";
q+="'";q+=lemail;q+="'";q+=" AND ";
q+="password = '";q+=lpassword;q+="'";
q+=" AND "; q+="class = 'admin'";
query = q.c_str();
mysql_query(conn,query);
res = mysql_store_result(conn);
numrows=0;
numrows = (unsigned int)mysql_num_rows(res);
if(numrows>0){
check="correctadmin";
}
}else{
check="incorrect";
}
}
if(lemail.length()>0 && lpassword.length()>0 || email.length()>0 && password.length()>0 && code.length()>0){
email=password=lemail=lpassword=code="";
pass = (unsigned char *)check.c_str();
sentB(pass);
cout<<"Successful : "<<pass<<endl;
}
free(buf);
break;
}
default:
break;
}
return 0;
}
static struct libwebsocket_protocols protocols[] = {
{
"project2", // protocol name - very important!
call_back_logic, // callback
0 // we don't use any per session data
},
{
NULL, NULL, 0 /* End of list */
}
};
void sentB(unsigned char *p){
libwebsocket_write(client[num_client-1],(unsigned char *)p, lens, LWS_WRITE_TEXT);
}
int main() {
//MySQL Connection
conn = mysql_init(NULL);
mysql_real_connect(conn,"127.0.0.1","root","12341111","project",0,NULL,0);
int port = 9000; // Server port
const char *interface = NULL;
struct libwebsocket_context *context;
// we're not using ssl
const char *cert_path = NULL;
const char *key_path = NULL;
// no special options
int opts = 0;
// create libwebsocket context representing this server
context = libwebsocket_create_context(port, interface, protocols,
libwebsocket_internal_extensions,
cert_path, key_path,NULL, -1, -1, opts);
if (context == NULL) {
fprintf(stderr, "libwebsocket init failed\n");
return -1;
}
printf("starting server[Test]...\n");
// infinite loop, to end this server send SIGTERM. (CTRL+C)
while (1) {
libwebsocket_service(context, 50);
}
mysql_close(conn);
libwebsocket_context_destroy(context);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment