Merged athile's work
commit
0de4bb9d6c
@ -0,0 +1,2 @@
|
||||
project(clientconsole)
|
||||
add_executable(clientconsole client.cpp)
|
@ -0,0 +1,138 @@
|
||||
#include <iostream>
|
||||
#include <boost/array.hpp>
|
||||
#include <boost/asio.hpp>
|
||||
|
||||
using boost::asio::ip::tcp;
|
||||
|
||||
#pragma warning( disable : 4966 )
|
||||
|
||||
class Client
|
||||
{
|
||||
protected:
|
||||
struct Header
|
||||
{
|
||||
char magic[4];
|
||||
boost::uint32_t dataLength;
|
||||
};
|
||||
|
||||
boost::asio::io_service mIOService;
|
||||
tcp::socket* mpSocket;
|
||||
|
||||
public:
|
||||
|
||||
bool connect(const char* port)
|
||||
{
|
||||
tcp::resolver resolver(mIOService);
|
||||
tcp::resolver::query query("localhost", port);
|
||||
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
|
||||
tcp::resolver::iterator end;
|
||||
|
||||
mpSocket = new tcp::socket(mIOService);
|
||||
boost::system::error_code error = boost::asio::error::host_not_found;
|
||||
while (error && endpoint_iterator != end)
|
||||
{
|
||||
mpSocket->close();
|
||||
mpSocket->connect(*endpoint_iterator++, error);
|
||||
}
|
||||
|
||||
return (error) ? false : true;
|
||||
}
|
||||
void disconnect()
|
||||
{
|
||||
mpSocket->close();
|
||||
mIOService.stop();
|
||||
}
|
||||
|
||||
bool send (const char* msg)
|
||||
{
|
||||
const size_t slen = strlen(msg);
|
||||
const size_t plen = sizeof(Header) + slen + 1;
|
||||
|
||||
std::vector<char> packet(plen);
|
||||
Header* pHeader = reinterpret_cast<Header*>(&packet[0]);
|
||||
strncpy(pHeader->magic, "OMW0", 4);
|
||||
pHeader->dataLength = slen + 1; // Include the null terminator
|
||||
strncpy(&packet[8], msg, pHeader->dataLength);
|
||||
|
||||
boost::system::error_code ec;
|
||||
boost::asio::write(*mpSocket, boost::asio::buffer(packet),
|
||||
boost::asio::transfer_all(), ec);
|
||||
if (ec)
|
||||
std::cout << "Error: " << ec.message() << std::endl;
|
||||
|
||||
return !ec;
|
||||
}
|
||||
|
||||
bool receive (std::string& reply)
|
||||
{
|
||||
Header header;
|
||||
boost::system::error_code error;
|
||||
mpSocket->read_some(boost::asio::buffer(&header, sizeof(Header)), error);
|
||||
|
||||
if (error != boost::asio::error::eof)
|
||||
{
|
||||
if (strncmp(header.magic, "OMW0", 4) == 0)
|
||||
{
|
||||
std::vector<char> msg;
|
||||
msg.resize(header.dataLength);
|
||||
|
||||
boost::system::error_code error;
|
||||
mpSocket->read_some(boost::asio::buffer(&msg[0], header.dataLength), error);
|
||||
if (!error)
|
||||
{
|
||||
reply = &msg[0];
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else
|
||||
throw std::exception("Unexpected header!");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
std::cout << "OpenMW client console" << std::endl;
|
||||
std::cout << "=====================" << std::endl;
|
||||
std::cout << "Type 'quit' to exit." << std::endl;
|
||||
std::cout << "Connecting...";
|
||||
|
||||
Client client;
|
||||
if (client.connect("27917"))
|
||||
{
|
||||
std::cout << "success." << std::endl;
|
||||
|
||||
bool bDone = false;
|
||||
do
|
||||
{
|
||||
std::cout << "Client> ";
|
||||
std::string buffer;
|
||||
std::getline(std::cin, buffer);
|
||||
|
||||
if (buffer == "quit")
|
||||
bDone = true;
|
||||
else
|
||||
{
|
||||
if (client.send(buffer.c_str()))
|
||||
{
|
||||
std::string reply;
|
||||
if (client.receive(reply))
|
||||
std::cout << "Server: " << reply << std::endl;
|
||||
else
|
||||
bDone = true;
|
||||
}
|
||||
else
|
||||
bDone = true;
|
||||
}
|
||||
|
||||
} while (!bDone);
|
||||
|
||||
client.disconnect();
|
||||
}
|
||||
else
|
||||
std::cout << "failed." << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
project(MWCompiler)
|
||||
|
||||
set(TOOLS_MWCOMPILER ${COMPILER}
|
||||
main.cpp
|
||||
context.cpp
|
||||
context.hpp)
|
||||
|
||||
add_executable(mwcompiler ${TOOLS_MWCOMPILER})
|
||||
|
@ -0,0 +1,9 @@
|
||||
project(MWInterpreter)
|
||||
|
||||
set(TOOLS_MWINTERPRETER
|
||||
${INTERPRETER}
|
||||
main.cpp
|
||||
context.cpp
|
||||
context.hpp)
|
||||
|
||||
add_executable(mwinterpreter ${TOOLS_MWINTERPRETER})
|
@ -0,0 +1,19 @@
|
||||
#ifndef COMMANDSERVER_COMMAND_HPP
|
||||
#define COMMANDSERVER_COMMAND_HPP
|
||||
|
||||
namespace OMW
|
||||
{
|
||||
///
|
||||
/// A Command is currently defined as a string input that, when processed,
|
||||
/// will generate a string output. The string output is passed to the
|
||||
/// mReplyFunction as soon as the command has been processed.
|
||||
///
|
||||
class Command
|
||||
{
|
||||
public:
|
||||
std::string mCommand;
|
||||
boost::function1<void, std::string> mReplyFunction;
|
||||
};
|
||||
}
|
||||
|
||||
#endif COMMANDSERVER_COMMAND_HPP
|
@ -0,0 +1,199 @@
|
||||
|
||||
#include "server.hpp"
|
||||
|
||||
using boost::asio::ip::tcp;
|
||||
|
||||
//
|
||||
// Namespace for containing implementation details that the
|
||||
// rest of OpenMW doesn't need to worry about
|
||||
//
|
||||
namespace OMW { namespace CommandServer { namespace Detail {
|
||||
|
||||
struct Header
|
||||
{
|
||||
char magic[4];
|
||||
size_t dataLength;
|
||||
} header;
|
||||
|
||||
///
|
||||
/// Tracks an active connection to the CommandServer
|
||||
///
|
||||
class Connection
|
||||
{
|
||||
public:
|
||||
Connection (boost::asio::io_service& io_service, Server* pServer);
|
||||
|
||||
void start();
|
||||
void stop();
|
||||
|
||||
tcp::socket& socket();
|
||||
void reply (std::string s);
|
||||
|
||||
protected:
|
||||
void handle ();
|
||||
|
||||
tcp::socket mSocket;
|
||||
Server* mpServer;
|
||||
boost::thread* mpThread;
|
||||
};
|
||||
|
||||
Connection::Connection (boost::asio::io_service& io_service, Server* pServer)
|
||||
: mSocket (io_service)
|
||||
, mpServer (pServer)
|
||||
{
|
||||
}
|
||||
|
||||
void Connection::start()
|
||||
{
|
||||
mpThread = new boost::thread(boost::bind(&Connection::handle, this));
|
||||
}
|
||||
|
||||
///
|
||||
/// Stops and disconnects the connection
|
||||
///
|
||||
void Connection::stop()
|
||||
{
|
||||
mSocket.close();
|
||||
mpThread->join();
|
||||
}
|
||||
|
||||
tcp::socket& Connection::socket()
|
||||
{
|
||||
return mSocket;
|
||||
}
|
||||
|
||||
void Connection::reply (std::string reply)
|
||||
{
|
||||
const size_t plen = sizeof(Header) + reply.length() + 1;
|
||||
|
||||
std::vector<char> packet(plen);
|
||||
Header* pHeader = reinterpret_cast<Header*>(&packet[0]);
|
||||
strncpy(pHeader->magic, "OMW0", 4);
|
||||
pHeader->dataLength = reply.length() + 1; // Include the null terminator
|
||||
strncpy(&packet[8], reply.c_str(), pHeader->dataLength);
|
||||
|
||||
boost::system::error_code ec;
|
||||
boost::asio::write(mSocket, boost::asio::buffer(packet),
|
||||
boost::asio::transfer_all(), ec);
|
||||
if (ec)
|
||||
std::cout << "Error: " << ec.message() << std::endl;
|
||||
}
|
||||
|
||||
void Connection::handle ()
|
||||
{
|
||||
bool bDone = false;
|
||||
while (!bDone)
|
||||
{
|
||||
// Read the header
|
||||
boost::system::error_code error;
|
||||
mSocket.read_some(boost::asio::buffer(&header, sizeof(Header)), error);
|
||||
|
||||
if (error != boost::asio::error::eof)
|
||||
{
|
||||
if (strncmp(header.magic, "OMW0", 4) == 0)
|
||||
{
|
||||
std::vector<char> msg;
|
||||
msg.resize(header.dataLength);
|
||||
|
||||
boost::system::error_code error;
|
||||
mSocket.read_some(boost::asio::buffer(&msg[0], header.dataLength), error);
|
||||
if (!error)
|
||||
mpServer->postCommand(this, &msg[0]);
|
||||
else
|
||||
bDone = true;
|
||||
}
|
||||
else
|
||||
throw std::exception("Unexpected header!");
|
||||
}
|
||||
else
|
||||
bDone = true;
|
||||
}
|
||||
mpServer->removeConnection(this);
|
||||
}
|
||||
|
||||
}}}
|
||||
|
||||
namespace OMW { namespace CommandServer {
|
||||
|
||||
using namespace Detail;
|
||||
|
||||
Server::Server (Deque* pCommandQueue, const int port)
|
||||
: mAcceptor (mIOService, tcp::endpoint(tcp::v4(), port))
|
||||
, mpCommandQueue (pCommandQueue)
|
||||
, mbStopping (false)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void Server::start()
|
||||
{
|
||||
mIOService.run();
|
||||
mpThread = new boost::thread(boost::bind(&Server::threadMain, this));
|
||||
}
|
||||
|
||||
void Server::stop()
|
||||
{
|
||||
// (1) Stop accepting new connections
|
||||
// (2) Wait for the listener thread to finish
|
||||
mAcceptor.close();
|
||||
mpThread->join();
|
||||
|
||||
// Now that no new connections are possible, close any existing
|
||||
// open connections
|
||||
{
|
||||
boost::mutex::scoped_lock lock(mConnectionsMutex);
|
||||
mbStopping = true;
|
||||
for (ConnectionSet::iterator it = mConnections.begin();
|
||||
it != mConnections.end();
|
||||
++it)
|
||||
{
|
||||
(*it)->stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Server::removeConnection (Connection* ptr)
|
||||
{
|
||||
// If the server is shutting down (rather the client closing the
|
||||
// connection), don't remove the connection from the list: that
|
||||
// would corrupt the iterator the server is using to shutdown all
|
||||
// clients.
|
||||
if (!mbStopping)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(mConnectionsMutex);
|
||||
std::set<Connection*>::iterator it = mConnections.find(ptr);
|
||||
if (it != mConnections.end())
|
||||
mConnections.erase(it);
|
||||
}
|
||||
delete ptr;
|
||||
}
|
||||
|
||||
void Server::postCommand (Connection* pConnection, const char* s)
|
||||
{
|
||||
Command cmd;
|
||||
cmd.mCommand = s;
|
||||
cmd.mReplyFunction = std::bind1st(std::mem_fun(&Connection::reply), pConnection);
|
||||
mpCommandQueue->push_back(cmd);
|
||||
}
|
||||
|
||||
void Server::threadMain()
|
||||
{
|
||||
// Loop until accept() fails, which will cause the break statement to be hit
|
||||
while (true)
|
||||
{
|
||||
std::auto_ptr<Connection> spConnection(new Connection(mAcceptor.io_service(), this));
|
||||
boost::system::error_code ec;
|
||||
mAcceptor.accept(spConnection->socket(), ec);
|
||||
if (!ec)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(mConnectionsMutex);
|
||||
mConnections.insert(spConnection.get());
|
||||
spConnection->start();
|
||||
spConnection.release();
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}}
|
@ -0,0 +1,63 @@
|
||||
#ifndef CONSOLESERVER_H
|
||||
#define CONSOLESERVER_H
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <deque>
|
||||
#include <set>
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/thread.hpp>
|
||||
|
||||
#include "components/misc/tsdeque.hpp"
|
||||
#include "components/commandserver/command.hpp"
|
||||
|
||||
namespace OMW { namespace CommandServer
|
||||
{
|
||||
//
|
||||
// Forward Declarations
|
||||
//
|
||||
namespace Detail
|
||||
{
|
||||
class Connection;
|
||||
}
|
||||
|
||||
//
|
||||
// Server that opens a port to listen for string commands which will be
|
||||
// put into the deque provided in the Server constructor.
|
||||
//
|
||||
class Server
|
||||
{
|
||||
public:
|
||||
typedef TsDeque<Command> Deque;
|
||||
|
||||
Server (Deque* pCommandQueue, const int port);
|
||||
|
||||
void start();
|
||||
void stop();
|
||||
|
||||
protected:
|
||||
friend class Detail::Connection;
|
||||
typedef std::set<Detail::Connection*> ConnectionSet;
|
||||
|
||||
void removeConnection (Detail::Connection* ptr);
|
||||
void postCommand (Detail::Connection*, const char* s);
|
||||
|
||||
void threadMain();
|
||||
|
||||
// Objects used to set up the listening server
|
||||
boost::asio::io_service mIOService;
|
||||
boost::asio::ip::tcp::acceptor mAcceptor;
|
||||
boost::thread* mpThread;
|
||||
bool mbStopping;
|
||||
|
||||
// Track active connections
|
||||
ConnectionSet mConnections;
|
||||
mutable boost::mutex mConnectionsMutex;
|
||||
|
||||
// Pointer to command queue
|
||||
Deque* mpCommandQueue;
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
#endif // CONSOLESERVER_H
|
@ -0,0 +1,56 @@
|
||||
#ifndef TSDEQUE_H
|
||||
#define TSDEQUE_H
|
||||
|
||||
#include <boost/thread.hpp>
|
||||
|
||||
//
|
||||
// Adapted from http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html
|
||||
//
|
||||
template<typename Data>
|
||||
class TsDeque
|
||||
{
|
||||
private:
|
||||
std::deque<Data> the_queue;
|
||||
mutable boost::mutex the_mutex;
|
||||
boost::condition_variable the_condition_variable;
|
||||
|
||||
public:
|
||||
void push_back(Data const& data)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(the_mutex);
|
||||
the_queue.push_back(data);
|
||||
lock.unlock();
|
||||
the_condition_variable.notify_one();
|
||||
}
|
||||
|
||||
bool empty() const
|
||||
{
|
||||
boost::mutex::scoped_lock lock(the_mutex);
|
||||
return the_queue.empty();
|
||||
}
|
||||
|
||||
bool try_pop_front(Data& popped_value)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(the_mutex);
|
||||
if(the_queue.empty())
|
||||
return false;
|
||||
|
||||
popped_value=the_queue.front();
|
||||
the_queue.pop_front();
|
||||
return true;
|
||||
}
|
||||
|
||||
void wait_and_pop_front(Data& popped_value)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(the_mutex);
|
||||
while(the_queue.empty())
|
||||
{
|
||||
the_condition_variable.wait(lock);
|
||||
}
|
||||
|
||||
popped_value=the_queue.front();
|
||||
the_queue.pop_front();
|
||||
}
|
||||
};
|
||||
|
||||
#endif // TSDEQUE_H
|
Loading…
Reference in New Issue