Dev Update

Update my Dev repo to bs12's dev post master merge. Should allow pull
request to automerge now.
This commit is contained in:
Vetinari
2014-03-05 12:23:49 +11:00
parent 411b8784ba
commit ee617bc4f8
31 changed files with 444 additions and 171 deletions

View File

@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="DLLSocket" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin\Debug\DLLSocket" prefix_auto="1" extension_auto="1" />
<Option object_output="obj\Debug\" />
<Option type="3" />
<Option compiler="gcc" />
<Option createDefFile="1" />
<Option createStaticLib="1" />
<Compiler>
<Add option="-Wall" />
<Add option="-DBUILD_DLL" />
<Add option="-g" />
</Compiler>
<Linker>
<Add library="user32" />
</Linker>
</Target>
<Target title="Release">
<Option output="bin\Release\DLLSocket" prefix_auto="1" extension_auto="1" />
<Option object_output="obj\Release\" />
<Option type="3" />
<Option compiler="gcc" />
<Option createDefFile="1" />
<Option createStaticLib="1" />
<Compiler>
<Add option="-Wall" />
<Add option="-DBUILD_DLL" />
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
<Add library="user32" />
</Linker>
</Target>
</Build>
<Unit filename="main.cpp" />
<Unit filename="main.h" />
<Extensions>
<code_completion />
<debugger />
</Extensions>
</Project>
</CodeBlocks_project_file>

1
lib/DLLSocket/compile.sh Normal file
View File

@@ -0,0 +1 @@
g++ -static -shared -O3 -fPIC main.cpp -o DLLSocket.so

133
lib/DLLSocket/main.cpp Normal file
View File

@@ -0,0 +1,133 @@
// OS-specific networking includes
// -------------------------------
#ifdef __WIN32
#include <winsock2.h>
typedef int socklen_t;
#else
extern "C" {
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
}
typedef int SOCKET;
typedef sockaddr_in SOCKADDR_IN;
typedef sockaddr SOCKADDR;
#define SOCKET_ERROR -1
#endif
// Socket used for all communications
SOCKET sock;
// Address of the remote server
SOCKADDR_IN addr;
// Buffer used to return dynamic strings to the caller
#define BUFFER_SIZE 1024
char return_buffer[BUFFER_SIZE];
// exposed functions
// ------------------------------
const char* SUCCESS = "1\0"; // string representing success
#ifdef __WIN32
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __attribute__ ((visibility ("default")))
#endif
// arg1: ip(in the xx.xx.xx.xx format)
// arg2: port(a short)
// return: NULL on failure, SUCCESS otherwise
extern "C" DLL_EXPORT const char* establish_connection(int n, char *v[])
{
// extract args
// ------------
if(n < 2) return 0;
const char* ip = v[0];
const char* port_s = v[1];
unsigned short port = atoi(port_s);
// set up network stuff
// --------------------
#ifdef __WIN32
WSADATA wsa;
WSAStartup(MAKEWORD(2,0),&wsa);
#endif
sock = socket(AF_INET,SOCK_DGRAM,0);
// make the socket non-blocking
// ----------------------------
#ifdef __WIN32
unsigned long iMode=1;
ioctlsocket(sock,FIONBIO,&iMode);
#else
fcntl(sock, F_SETFL, O_NONBLOCK);
#endif
// establish a connection to the server
// ------------------------------------
memset(&addr,0,sizeof(SOCKADDR_IN));
addr.sin_family=AF_INET;
addr.sin_port=htons(port);
// convert the string representation of the ip to a byte representation
addr.sin_addr.s_addr=inet_addr(ip);
return SUCCESS;
}
// arg1: string message to send
// return: NULL on failure, SUCCESS otherwise
extern "C" DLL_EXPORT const char* send_message(int n, char *v[])
{
// extract the args
if(n < 1) return 0;
const char* msg = v[0];
// send the message
int rc = sendto(sock,msg,strlen(msg),0,(SOCKADDR*)&addr,sizeof(SOCKADDR));
// check for errors
if (rc != -1) {
return SUCCESS;
}
else {
return 0;
}
}
// no args
// return: message if any received, NULL otherwise
extern "C" DLL_EXPORT const char* recv_message(int n, char *v[])
{
SOCKADDR_IN sender; // we will store the sender address here
socklen_t sender_byte_length = sizeof(sender);
// Try receiving messages until we receive one that's valid, or there are no more messages
while(1) {
int rc = recvfrom(sock, return_buffer, BUFFER_SIZE,0,(SOCKADDR*) &sender,&sender_byte_length);
if(rc > 0) {
// we could read something
if(sender.sin_addr.s_addr != addr.sin_addr.s_addr) {
continue; // not our connection, ignore and try again
} else {
return_buffer[rc] = 0; // 0-terminate the string
return return_buffer;
}
}
else {
break; // no more messages, stop trying to receive
}
}
return 0;
}

View File

@@ -0,0 +1,51 @@
import subprocess
import socket
import urlparse
UDP_IP="127.0.0.1"
UDP_PORT=8019
sock = socket.socket( socket.AF_INET, # Internet
socket.SOCK_DGRAM ) # UDP
sock.bind( (UDP_IP,UDP_PORT) )
last_ticker_state = None
def handle_message(data, addr):
global last_ticker_state
params = urlparse.parse_qs(data)
print(data)
try:
if params["type"][0] == "log" and str(params["log"][0]) and str(params["message"][0]):
open(params["log"][0],"a+").write(params["message"][0]+"\n")
except IOError:
pass
except KeyError:
pass
try:
if params["type"][0] == "ticker_state" and str(params["message"][0]):
last_ticker_state = str(params["message"][0])
except KeyError:
pass
try:
if params["type"][0] == "startup" and last_ticker_state:
open("crashlog.txt","a+").write("Server exited, last ticker state was: "+last_ticker_state+"\n")
except KeyError:
pass
sock.settimeout(60*6) # 10 minute timeout
while True:
try:
data, addr = sock.recvfrom( 1024 ) # buffer size is 1024 bytes
handle_message(data,addr)
except socket.timeout:
# try to start the server again
print("Server timed out.. attempting restart.")
if last_ticker_state:
open("crashmsg.txt","a+").write("Server crashed, trying to reboot. last ticker state: "+last_ticker_state+"\n")
subprocess.call("killall -9 DreamDaemon")
subprocess.call("./start")