Screw convention; moving include files alongside source files.
They now will show up in xcode/etc.
This commit is contained in:
36
src/xenia/dbg/client.cc
Normal file
36
src/xenia/dbg/client.cc
Normal file
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2013 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#include <xenia/dbg/client.h>
|
||||
|
||||
#include <xenia/dbg/debugger.h>
|
||||
|
||||
|
||||
using namespace xe;
|
||||
using namespace xe::dbg;
|
||||
|
||||
|
||||
Client::Client(Debugger* debugger) :
|
||||
debugger_(debugger) {
|
||||
debugger_->AddClient(this);
|
||||
}
|
||||
|
||||
Client::~Client() {
|
||||
debugger_->RemoveClient(this);
|
||||
}
|
||||
|
||||
void Client::OnMessage(const uint8_t* data, size_t length) {
|
||||
debugger_->Dispatch(this, data, length);
|
||||
}
|
||||
|
||||
void Client::Write(uint8_t* buffer, size_t length) {
|
||||
uint8_t* buffers[] = {buffer};
|
||||
size_t lengths[] = {length};
|
||||
Write(buffers, lengths, 1);
|
||||
}
|
||||
44
src/xenia/dbg/client.h
Normal file
44
src/xenia/dbg/client.h
Normal file
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2013 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef XENIA_KERNEL_DBG_CLIENT_H_
|
||||
#define XENIA_KERNEL_DBG_CLIENT_H_
|
||||
|
||||
#include <xenia/common.h>
|
||||
#include <xenia/core.h>
|
||||
|
||||
|
||||
namespace xe {
|
||||
namespace dbg {
|
||||
|
||||
|
||||
class Debugger;
|
||||
|
||||
|
||||
class Client {
|
||||
public:
|
||||
Client(Debugger* debugger);
|
||||
virtual ~Client();
|
||||
|
||||
virtual int Setup() = 0;
|
||||
|
||||
void OnMessage(const uint8_t* data, size_t length);
|
||||
void Write(uint8_t* buffer, size_t length);
|
||||
virtual void Write(uint8_t** buffers, size_t* lengths, size_t count) = 0;
|
||||
|
||||
protected:
|
||||
Debugger* debugger_;
|
||||
};
|
||||
|
||||
|
||||
} // namespace dbg
|
||||
} // namespace xe
|
||||
|
||||
|
||||
#endif // XENIA_KERNEL_DBG_CLIENT_H_
|
||||
26
src/xenia/dbg/content_source.cc
Normal file
26
src/xenia/dbg/content_source.cc
Normal file
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2013 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#include <xenia/dbg/content_source.h>
|
||||
|
||||
|
||||
using namespace xe;
|
||||
using namespace xe::dbg;
|
||||
|
||||
|
||||
ContentSource::ContentSource(Debugger* debugger, uint32_t source_id) :
|
||||
debugger_(debugger), source_id_(source_id) {
|
||||
}
|
||||
|
||||
ContentSource::~ContentSource() {
|
||||
}
|
||||
|
||||
uint32_t ContentSource::source_id() {
|
||||
return source_id_;
|
||||
}
|
||||
43
src/xenia/dbg/content_source.h
Normal file
43
src/xenia/dbg/content_source.h
Normal file
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2013 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef XENIA_KERNEL_DBG_CONTENT_SOURCE_H_
|
||||
#define XENIA_KERNEL_DBG_CONTENT_SOURCE_H_
|
||||
|
||||
#include <xenia/common.h>
|
||||
#include <xenia/core.h>
|
||||
|
||||
#include <xenia/dbg/client.h>
|
||||
|
||||
|
||||
namespace xe {
|
||||
namespace dbg {
|
||||
|
||||
|
||||
class ContentSource {
|
||||
public:
|
||||
ContentSource(Debugger* debugger, uint32_t source_id);
|
||||
virtual ~ContentSource();
|
||||
|
||||
uint32_t source_id();
|
||||
|
||||
virtual int Dispatch(Client* client, uint8_t type, uint32_t request_id,
|
||||
const uint8_t* data, size_t length) = 0;
|
||||
|
||||
protected:
|
||||
Debugger* debugger_;
|
||||
uint32_t source_id_;
|
||||
};
|
||||
|
||||
|
||||
} // namespace dbg
|
||||
} // namespace xe
|
||||
|
||||
|
||||
#endif // XENIA_KERNEL_DBG_CONTENT_SOURCE_H_
|
||||
125
src/xenia/dbg/debugger.cc
Normal file
125
src/xenia/dbg/debugger.cc
Normal file
@@ -0,0 +1,125 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2013 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#include <xenia/dbg/debugger.h>
|
||||
|
||||
#include <gflags/gflags.h>
|
||||
|
||||
#include <xenia/dbg/content_source.h>
|
||||
#include <xenia/dbg/ws_listener.h>
|
||||
|
||||
|
||||
using namespace xe;
|
||||
using namespace xe::dbg;
|
||||
|
||||
|
||||
DEFINE_bool(wait_for_debugger, false,
|
||||
"Whether to wait for the debugger to attach before launching.");
|
||||
DEFINE_int32(remote_debug_port, 6200,
|
||||
"Websocket port to listen for debugger connections on.");
|
||||
|
||||
|
||||
Debugger::Debugger(xe_pal_ref pal) {
|
||||
pal_ = xe_pal_retain(pal);
|
||||
|
||||
listener_ = auto_ptr<Listener>(new WsListener(
|
||||
this, pal_, FLAGS_remote_debug_port));
|
||||
}
|
||||
|
||||
Debugger::~Debugger() {
|
||||
for (std::vector<Client*>::iterator it = clients_.begin();
|
||||
it != clients_.end(); ++it) {
|
||||
delete *it;
|
||||
}
|
||||
clients_.clear();
|
||||
|
||||
for (std::map<uint32_t, ContentSource*>::iterator it =
|
||||
content_sources_.begin(); it != content_sources_.end(); ++it) {
|
||||
delete it->second;
|
||||
}
|
||||
content_sources_.clear();
|
||||
|
||||
xe_pal_release(pal_);
|
||||
}
|
||||
|
||||
void Debugger::RegisterContentSource(ContentSource* content_source) {
|
||||
content_sources_.insert(std::pair<uint32_t, ContentSource*>(
|
||||
content_source->source_id(), content_source));
|
||||
}
|
||||
|
||||
int Debugger::Startup() {
|
||||
// Start listener.
|
||||
// This may launch a thread and such.
|
||||
if (listener_->Setup()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// If desired, wait until the first client connects.
|
||||
if (FLAGS_wait_for_debugger) {
|
||||
XELOGI(XT("Waiting for debugger on port %d..."), FLAGS_remote_debug_port);
|
||||
if (listener_->WaitForClient()) {
|
||||
return 1;
|
||||
}
|
||||
XELOGI(XT("Debugger attached, continuing..."));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Debugger::Broadcast(uint32_t source_id,
|
||||
const uint8_t* data, size_t length) {
|
||||
uint32_t header[] = {
|
||||
0x00000001,
|
||||
source_id,
|
||||
0,
|
||||
length,
|
||||
};
|
||||
uint8_t* buffers[] = {
|
||||
(uint8_t*)header,
|
||||
(uint8_t*)data,
|
||||
};
|
||||
size_t lengths[] = {
|
||||
sizeof(header),
|
||||
length,
|
||||
};
|
||||
for (std::vector<Client*>::iterator it = clients_.begin();
|
||||
it != clients_.end(); ++it) {
|
||||
Client* client = *it;
|
||||
client->Write(buffers, lengths, XECOUNT(buffers));
|
||||
}
|
||||
}
|
||||
|
||||
void Debugger::AddClient(Client* client) {
|
||||
clients_.push_back(client);
|
||||
}
|
||||
|
||||
void Debugger::RemoveClient(Client* client) {
|
||||
for (std::vector<Client*>::iterator it = clients_.begin();
|
||||
it != clients_.end(); ++it) {
|
||||
if (*it == client) {
|
||||
clients_.erase(it);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int Debugger::Dispatch(Client* client, const uint8_t* data, size_t length) {
|
||||
uint32_t type = XEGETUINT32LE(data + 0);
|
||||
uint32_t source_id = XEGETUINT32LE(data + 4);
|
||||
uint32_t request_id = XEGETUINT32LE(data + 8);
|
||||
uint32_t size = XEGETUINT32LE(data + 12);
|
||||
|
||||
std::map<uint32_t, ContentSource*>::iterator it =
|
||||
content_sources_.find(source_id);
|
||||
if (it == content_sources_.end()) {
|
||||
XELOGW(XT("Content source %d not found, ignoring message"), source_id);
|
||||
return 1;
|
||||
}
|
||||
return it->second->Dispatch(client, type, request_id, data + 16, size);
|
||||
}
|
||||
59
src/xenia/dbg/debugger.h
Normal file
59
src/xenia/dbg/debugger.h
Normal file
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2013 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef XENIA_KERNEL_DBG_DEBUGGER_H_
|
||||
#define XENIA_KERNEL_DBG_DEBUGGER_H_
|
||||
|
||||
#include <xenia/common.h>
|
||||
#include <xenia/core.h>
|
||||
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
|
||||
namespace xe {
|
||||
namespace dbg {
|
||||
|
||||
|
||||
class Client;
|
||||
class ContentSource;
|
||||
class Listener;
|
||||
|
||||
|
||||
class Debugger {
|
||||
public:
|
||||
Debugger(xe_pal_ref pal);
|
||||
virtual ~Debugger();
|
||||
|
||||
void RegisterContentSource(ContentSource* content_source);
|
||||
|
||||
int Startup();
|
||||
|
||||
void Broadcast(uint32_t source_id, const uint8_t* data, size_t length);
|
||||
|
||||
private:
|
||||
void AddClient(Client* client);
|
||||
void RemoveClient(Client* client);
|
||||
int Dispatch(Client* client, const uint8_t* data, size_t length);
|
||||
|
||||
friend class Client;
|
||||
|
||||
private:
|
||||
xe_pal_ref pal_;
|
||||
auto_ptr<Listener> listener_;
|
||||
std::vector<Client*> clients_;
|
||||
std::map<uint32_t, ContentSource*> content_sources_;
|
||||
};
|
||||
|
||||
|
||||
} // namespace dbg
|
||||
} // namespace xe
|
||||
|
||||
|
||||
#endif // XENIA_KERNEL_DBG_DEBUGGER_H_
|
||||
24
src/xenia/dbg/listener.cc
Normal file
24
src/xenia/dbg/listener.cc
Normal file
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2013 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#include <xenia/dbg/listener.h>
|
||||
|
||||
|
||||
using namespace xe;
|
||||
using namespace xe::dbg;
|
||||
|
||||
|
||||
Listener::Listener(Debugger* debugger, xe_pal_ref pal) :
|
||||
debugger_(debugger) {
|
||||
pal_ = xe_pal_retain(pal);
|
||||
}
|
||||
|
||||
Listener::~Listener() {
|
||||
xe_pal_release(pal_);
|
||||
}
|
||||
42
src/xenia/dbg/listener.h
Normal file
42
src/xenia/dbg/listener.h
Normal file
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2013 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef XENIA_KERNEL_DBG_LISTENER_H_
|
||||
#define XENIA_KERNEL_DBG_LISTENER_H_
|
||||
|
||||
#include <xenia/common.h>
|
||||
#include <xenia/core.h>
|
||||
|
||||
|
||||
namespace xe {
|
||||
namespace dbg {
|
||||
|
||||
|
||||
class Debugger;
|
||||
|
||||
|
||||
class Listener {
|
||||
public:
|
||||
Listener(Debugger* debugger, xe_pal_ref pal);
|
||||
virtual ~Listener();
|
||||
|
||||
virtual int Setup() = 0;
|
||||
virtual int WaitForClient() = 0;
|
||||
|
||||
protected:
|
||||
Debugger* debugger_;
|
||||
xe_pal_ref pal_;
|
||||
};
|
||||
|
||||
|
||||
} // namespace dbg
|
||||
} // namespace xe
|
||||
|
||||
|
||||
#endif // XENIA_KERNEL_DBG_LISTENER_H_
|
||||
17
src/xenia/dbg/sources.gypi
Normal file
17
src/xenia/dbg/sources.gypi
Normal file
@@ -0,0 +1,17 @@
|
||||
# Copyright 2013 Ben Vanik. All Rights Reserved.
|
||||
{
|
||||
'sources': [
|
||||
'client.cc',
|
||||
'client.h',
|
||||
'content_source.cc',
|
||||
'content_source.h',
|
||||
'debugger.cc',
|
||||
'debugger.h',
|
||||
'listener.cc',
|
||||
'listener.h',
|
||||
'ws_client.cc',
|
||||
'ws_client.h',
|
||||
'ws_listener.cc',
|
||||
'ws_listener.h',
|
||||
],
|
||||
}
|
||||
397
src/xenia/dbg/ws_client.cc
Normal file
397
src/xenia/dbg/ws_client.cc
Normal file
@@ -0,0 +1,397 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2013 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#include <xenia/dbg/ws_client.h>
|
||||
|
||||
#include <xenia/dbg/debugger.h>
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <poll.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <sys/socket.h>
|
||||
#include <wslay/wslay.h>
|
||||
|
||||
#include <openssl/bio.h>
|
||||
#include <openssl/buffer.h>
|
||||
#include <openssl/hmac.h>
|
||||
#include <openssl/sha.h>
|
||||
|
||||
|
||||
using namespace xe;
|
||||
using namespace xe::dbg;
|
||||
|
||||
|
||||
WsClient::WsClient(Debugger* debugger, int socket_id) :
|
||||
Client(debugger),
|
||||
socket_id_(socket_id) {
|
||||
mutex_ = xe_mutex_alloc(1000);
|
||||
|
||||
int notify_ids[2];
|
||||
socketpair(PF_LOCAL, SOCK_STREAM, 0, notify_ids);
|
||||
notify_rd_id_ = notify_ids[0];
|
||||
notify_wr_id_ = notify_ids[1];
|
||||
}
|
||||
|
||||
WsClient::~WsClient() {
|
||||
xe_mutex_t* mutex = mutex_;
|
||||
xe_mutex_lock(mutex);
|
||||
mutex_ = NULL;
|
||||
shutdown(socket_id_, SHUT_WR);
|
||||
close(socket_id_);
|
||||
socket_id_ = 0;
|
||||
xe_mutex_unlock(mutex);
|
||||
xe_mutex_free(mutex);
|
||||
|
||||
close(notify_rd_id_);
|
||||
close(notify_wr_id_);
|
||||
}
|
||||
|
||||
int WsClient::socket_id() {
|
||||
return socket_id_;
|
||||
}
|
||||
|
||||
int WsClient::Setup() {
|
||||
// Prep the socket.
|
||||
int opt_value;
|
||||
opt_value = 1;
|
||||
setsockopt(socket_id_, SOL_SOCKET, SO_KEEPALIVE,
|
||||
&opt_value, sizeof(opt_value));
|
||||
opt_value = 1;
|
||||
setsockopt(socket_id_, IPPROTO_TCP, TCP_NODELAY,
|
||||
&opt_value, sizeof(opt_value));
|
||||
|
||||
// TODO(benvanik): win32 support - put simple threads in PAL
|
||||
pthread_attr_t attr;
|
||||
pthread_attr_init(&attr);
|
||||
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
||||
pthread_t thread_handle;
|
||||
int result_code = pthread_create(
|
||||
&thread_handle,
|
||||
&attr,
|
||||
&StartCallbackPthreads,
|
||||
this);
|
||||
pthread_attr_destroy(&attr);
|
||||
|
||||
return result_code;
|
||||
}
|
||||
|
||||
void* WsClient::StartCallbackPthreads(void* param) {
|
||||
WsClient* thread = reinterpret_cast<WsClient*>(param);
|
||||
thread->EventThread();
|
||||
return 0;
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
ssize_t WsClientSendCallback(wslay_event_context_ptr ctx,
|
||||
const uint8_t* data, size_t len, int flags,
|
||||
void* user_data) {
|
||||
WsClient* client = reinterpret_cast<WsClient*>(user_data);
|
||||
|
||||
int sflags = 0;
|
||||
#ifdef MSG_MORE
|
||||
if (flags & WSLAY_MSG_MORE) {
|
||||
sflags |= MSG_MORE;
|
||||
}
|
||||
#endif // MSG_MORE
|
||||
|
||||
ssize_t r;
|
||||
while ((r = send(client->socket_id(), data, len, sflags)) == -1 &&
|
||||
errno == EINTR);
|
||||
if (r == -1) {
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
||||
wslay_event_set_error(ctx, WSLAY_ERR_WOULDBLOCK);
|
||||
} else {
|
||||
wslay_event_set_error(ctx, WSLAY_ERR_CALLBACK_FAILURE);
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
ssize_t WsClientRecvCallback(wslay_event_context_ptr ctx,
|
||||
uint8_t* data, size_t len, int flags,
|
||||
void* user_data) {
|
||||
WsClient* client = reinterpret_cast<WsClient*>(user_data);
|
||||
ssize_t r;
|
||||
while ((r = recv(client->socket_id(), data, len, 0)) == -1 &&
|
||||
errno == EINTR);
|
||||
if (r == -1) {
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
||||
wslay_event_set_error(ctx, WSLAY_ERR_WOULDBLOCK);
|
||||
} else {
|
||||
wslay_event_set_error(ctx, WSLAY_ERR_CALLBACK_FAILURE);
|
||||
}
|
||||
} else if (r == 0) {
|
||||
wslay_event_set_error(ctx, WSLAY_ERR_CALLBACK_FAILURE);
|
||||
r = -1;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
void WsClientOnMsgCallback(wslay_event_context_ptr ctx,
|
||||
const struct wslay_event_on_msg_recv_arg* arg,
|
||||
void* user_data) {
|
||||
if (wslay_is_ctrl_frame(arg->opcode)) {
|
||||
// Ignore control frames.
|
||||
return;
|
||||
}
|
||||
|
||||
WsClient* client = reinterpret_cast<WsClient*>(user_data);
|
||||
switch (arg->opcode) {
|
||||
case WSLAY_TEXT_FRAME:
|
||||
XELOGW(XT("Text frame ignored; use binary messages"));
|
||||
break;
|
||||
case WSLAY_BINARY_FRAME:
|
||||
client->OnMessage(arg->msg, arg->msg_length);
|
||||
break;
|
||||
default:
|
||||
// Unknown opcode - some frame stuff?
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
std::string EncodeBase64(const uint8_t* input, size_t length) {
|
||||
// Good god what a horrible API.
|
||||
BIO* b64 = BIO_new(BIO_f_base64());
|
||||
BIO* bmem = BIO_new(BIO_s_mem());
|
||||
b64 = BIO_push(b64, bmem);
|
||||
BIO_write(b64, input, length);
|
||||
XEIGNORE(BIO_flush(b64));
|
||||
BUF_MEM* bptr;
|
||||
BIO_get_mem_ptr(b64, &bptr);
|
||||
std::string result(bptr->data, bptr->length);
|
||||
BIO_free_all(b64);
|
||||
// Strip the last character, which is a \n.
|
||||
result.erase(result.size() - 1);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int WsClient::PerformHandshake() {
|
||||
std::string headers;
|
||||
char buffer[4096];
|
||||
ssize_t r;
|
||||
while (true) {
|
||||
while ((r = read(socket_id_, buffer, sizeof(buffer))) == -1 &&
|
||||
errno == EINTR);
|
||||
if (r == -1) {
|
||||
if (errno == EWOULDBLOCK || errno == EAGAIN) {
|
||||
if (!headers.size()) {
|
||||
// Nothing read yet - spin.
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
} else {
|
||||
XELOGE(XT("HTTP header read failure"));
|
||||
return 1;
|
||||
}
|
||||
} else if (r == 0) {
|
||||
// EOF.
|
||||
XELOGE(XT("HTTP header EOF"));
|
||||
return 2;
|
||||
} else {
|
||||
headers.append(buffer, buffer + r);
|
||||
if (headers.size() > 8192) {
|
||||
XELOGE(XT("HTTP headers exceeded max buffer size"));
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (headers.find("\r\n\r\n") == std::string::npos) {
|
||||
XELOGE(XT("Incomplete HTTP headers: %s"), headers.c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Parse the headers to verify its a websocket request.
|
||||
std::string::size_type keyhdstart;
|
||||
if (headers.find("Upgrade: websocket\r\n") == std::string::npos ||
|
||||
headers.find("Connection: Upgrade\r\n") == std::string::npos ||
|
||||
(keyhdstart = headers.find("Sec-WebSocket-Key: ")) ==
|
||||
std::string::npos) {
|
||||
XELOGW(XT("HTTP connection does not contain websocket headers"));
|
||||
return 2;
|
||||
}
|
||||
keyhdstart += 19;
|
||||
std::string::size_type keyhdend = headers.find("\r\n", keyhdstart);
|
||||
std::string client_key = headers.substr(keyhdstart, keyhdend - keyhdstart);
|
||||
std::string accept_key = client_key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
|
||||
uint8_t accept_sha[20];
|
||||
SHA1((uint8_t*)accept_key.c_str(), accept_key.size(), accept_sha);
|
||||
accept_key = EncodeBase64(accept_sha, sizeof(accept_sha));
|
||||
|
||||
// Write the response to upgrade the connection.
|
||||
std::string response =
|
||||
"HTTP/1.1 101 Switching Protocols\r\n"
|
||||
"Upgrade: websocket\r\n"
|
||||
"Connection: Upgrade\r\n"
|
||||
"Sec-WebSocket-Accept: " + accept_key + "\r\n"
|
||||
"\r\n";
|
||||
size_t write_offset = 0;
|
||||
size_t write_length = response.size();
|
||||
while (true) {
|
||||
while ((r = write(socket_id_, response.c_str() + write_offset,
|
||||
write_length)) == -1 && errno == EINTR);
|
||||
if (r == -1) {
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
||||
break;
|
||||
} else {
|
||||
XELOGE(XT("HTTP response write failure"));
|
||||
return 4;
|
||||
}
|
||||
} else {
|
||||
write_offset += r;
|
||||
write_length -= r;
|
||||
if (!write_length) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void WsClient::EventThread() {
|
||||
int r;
|
||||
|
||||
// Enable non-blocking IO on the socket.
|
||||
int flags;
|
||||
while ((flags = fcntl(socket_id_, F_GETFL, 0)) == -1 &&
|
||||
errno == EINTR);
|
||||
if (flags == -1) {
|
||||
return;
|
||||
}
|
||||
while ((r = fcntl(socket_id_, F_SETFL, flags | O_NONBLOCK)) == -1 &&
|
||||
errno == EINTR);
|
||||
if (r == -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
// First run the HTTP handshake.
|
||||
// This will fail if the connection is not for websockets.
|
||||
if (PerformHandshake()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Prep callbacks.
|
||||
struct wslay_event_callbacks callbacks = {
|
||||
WsClientRecvCallback,
|
||||
WsClientSendCallback,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
WsClientOnMsgCallback,
|
||||
};
|
||||
|
||||
// Prep the websocket server context.
|
||||
wslay_event_context_ptr ctx;
|
||||
wslay_event_context_server_init(&ctx, &callbacks, this);
|
||||
|
||||
// Event for waiting on input.
|
||||
struct pollfd events[2];
|
||||
xe_zero_struct(&events, sizeof(events));
|
||||
events[0].fd = socket_id_;
|
||||
events[0].events = POLLIN;
|
||||
events[1].fd = notify_rd_id_;
|
||||
events[1].events = POLLIN;
|
||||
|
||||
// Loop forever.
|
||||
while(wslay_event_want_read(ctx) || wslay_event_want_write(ctx)) {
|
||||
// Wait on the event.
|
||||
while ((r = poll(events, XECOUNT(events), -1)) == -1 && errno == EINTR);
|
||||
if (r == -1) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Handle any self-generated events to queue messages.
|
||||
if (events[1].revents) {
|
||||
uint8_t dummy;
|
||||
XEIGNORE(recv(notify_rd_id_, &dummy, 1, 0));
|
||||
xe_mutex_lock(mutex_);
|
||||
for (std::vector<struct wslay_event_msg>::iterator it =
|
||||
pending_messages_.begin(); it != pending_messages_.end(); it++) {
|
||||
struct wslay_event_msg* msg = &*it;
|
||||
wslay_event_queue_msg(ctx, msg);
|
||||
}
|
||||
pending_messages_.clear();
|
||||
xe_mutex_unlock(mutex_);
|
||||
events[1].revents = 0;
|
||||
events[1].events = POLL_IN;
|
||||
}
|
||||
|
||||
// Handle websocket messages.
|
||||
struct pollfd* event = &events[0];
|
||||
if (((event->revents & POLLIN) && wslay_event_recv(ctx)) ||
|
||||
((event->revents & POLLOUT) && wslay_event_send(ctx)) ||
|
||||
((event->revents & (POLLERR | POLLHUP | POLLNVAL)))) {
|
||||
// Error handling the event.
|
||||
XELOGE(XT("Error handling WebSocket data"));
|
||||
break;
|
||||
}
|
||||
event->revents = 0;
|
||||
event->events = 0;
|
||||
if (wslay_event_want_read(ctx)) {
|
||||
event->events |= POLLIN;
|
||||
}
|
||||
if (wslay_event_want_write(ctx)) {
|
||||
event->events |= POLLOUT;
|
||||
}
|
||||
}
|
||||
|
||||
wslay_event_context_free(ctx);
|
||||
}
|
||||
|
||||
void WsClient::Write(uint8_t** buffers, size_t* lengths, size_t count) {
|
||||
if (!count) {
|
||||
return;
|
||||
}
|
||||
|
||||
size_t combined_length;
|
||||
uint8_t* combined_buffer;
|
||||
if (count == 1) {
|
||||
// Single buffer, just copy.
|
||||
combined_length = lengths[0];
|
||||
combined_buffer = (uint8_t*)xe_malloc(lengths[0]);
|
||||
XEIGNORE(xe_copy_memory(combined_buffer, combined_length,
|
||||
buffers[0], lengths[0]));
|
||||
} else {
|
||||
// Multiple buffers, merge.
|
||||
combined_length = 0;
|
||||
for (size_t n = 0; n < count; n++) {
|
||||
combined_length += lengths[n];
|
||||
}
|
||||
combined_buffer = (uint8_t*)xe_malloc(combined_length);
|
||||
for (size_t n = 0, offset = 0; n < count; n++) {
|
||||
XEIGNORE(xe_copy_memory(
|
||||
combined_buffer + offset, combined_length - offset,
|
||||
buffers[n], lengths[n]));
|
||||
offset += lengths[n];
|
||||
}
|
||||
}
|
||||
|
||||
struct wslay_event_msg msg = {
|
||||
WSLAY_BINARY_FRAME,
|
||||
combined_buffer,
|
||||
combined_length,
|
||||
};
|
||||
|
||||
xe_mutex_lock(mutex_);
|
||||
pending_messages_.push_back(msg);
|
||||
bool needs_signal = pending_messages_.size() == 1;
|
||||
xe_mutex_unlock(mutex_);
|
||||
|
||||
if (needs_signal) {
|
||||
// Notify the poll().
|
||||
uint8_t b = 0xFF;
|
||||
write(notify_wr_id_, &b, 1);
|
||||
}
|
||||
}
|
||||
59
src/xenia/dbg/ws_client.h
Normal file
59
src/xenia/dbg/ws_client.h
Normal file
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2013 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef XENIA_KERNEL_DBG_WS_CLIENT_H_
|
||||
#define XENIA_KERNEL_DBG_WS_CLIENT_H_
|
||||
|
||||
#include <xenia/common.h>
|
||||
#include <xenia/core.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <xenia/dbg/client.h>
|
||||
|
||||
|
||||
struct wslay_event_msg;
|
||||
|
||||
|
||||
namespace xe {
|
||||
namespace dbg {
|
||||
|
||||
|
||||
class WsClient : public Client {
|
||||
public:
|
||||
WsClient(Debugger* debugger, int socket_id);
|
||||
virtual ~WsClient();
|
||||
|
||||
int socket_id();
|
||||
|
||||
virtual int Setup();
|
||||
|
||||
virtual void Write(uint8_t** buffers, size_t* lengths, size_t count);
|
||||
|
||||
private:
|
||||
static void* StartCallbackPthreads(void* param);
|
||||
|
||||
int PerformHandshake();
|
||||
void EventThread();
|
||||
|
||||
int socket_id_;
|
||||
|
||||
int notify_rd_id_;
|
||||
int notify_wr_id_;
|
||||
xe_mutex_t* mutex_;
|
||||
|
||||
std::vector<struct wslay_event_msg> pending_messages_;
|
||||
};
|
||||
|
||||
|
||||
} // namespace dbg
|
||||
} // namespace xe
|
||||
|
||||
|
||||
#endif // XENIA_KERNEL_DBG_WS_CLIENT_H_
|
||||
95
src/xenia/dbg/ws_listener.cc
Normal file
95
src/xenia/dbg/ws_listener.cc
Normal file
@@ -0,0 +1,95 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2013 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#include <xenia/dbg/ws_listener.h>
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
#include <xenia/dbg/ws_client.h>
|
||||
|
||||
|
||||
using namespace xe;
|
||||
using namespace xe::dbg;
|
||||
|
||||
|
||||
WsListener::WsListener(Debugger* debugger, xe_pal_ref pal, uint32_t port) :
|
||||
Listener(debugger, pal),
|
||||
port_(port) {
|
||||
|
||||
}
|
||||
|
||||
WsListener::~WsListener() {
|
||||
if (socket_id_) {
|
||||
close(socket_id_);
|
||||
}
|
||||
}
|
||||
|
||||
int WsListener::Setup() {
|
||||
socket_id_ = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
if (socket_id_ < 1) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int opt_value;
|
||||
opt_value = 1;
|
||||
setsockopt(socket_id_, SOL_SOCKET, SO_KEEPALIVE,
|
||||
&opt_value, sizeof(opt_value));
|
||||
opt_value = 1;
|
||||
setsockopt(socket_id_, SOL_SOCKET, SO_REUSEADDR,
|
||||
&opt_value, sizeof(opt_value));
|
||||
opt_value = 0;
|
||||
setsockopt(socket_id_, IPPROTO_TCP, TCP_NODELAY,
|
||||
&opt_value, sizeof(opt_value));
|
||||
|
||||
struct sockaddr_in socket_addr;
|
||||
socket_addr.sin_family = AF_INET;
|
||||
socket_addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
socket_addr.sin_port = htons(port_);
|
||||
int r = bind(socket_id_, (struct sockaddr*)&socket_addr,
|
||||
sizeof(socket_addr));
|
||||
if (r < 0) {
|
||||
XELOGE(XT("Could not bind listen socket: %d"), errno);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (listen(socket_id_, 5) < 0) {
|
||||
close(socket_id_);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int WsListener::WaitForClient() {
|
||||
// Accept the first connection we get.
|
||||
struct sockaddr_in client_addr;
|
||||
socklen_t client_count = sizeof(client_addr);
|
||||
int client_socket_id = accept(socket_id_, (struct sockaddr*)&client_addr,
|
||||
&client_count);
|
||||
if (client_socket_id < 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int client_ip = client_addr.sin_addr.s_addr;
|
||||
char client_ip_str[INET_ADDRSTRLEN];
|
||||
inet_ntop(AF_INET, &client_ip, client_ip_str, XECOUNT(client_ip_str));
|
||||
XELOGI(XT("Debugger connected from %s"), client_ip_str);
|
||||
|
||||
// Create the client object.
|
||||
// Note that the client will delete itself when done.
|
||||
WsClient* client = new WsClient(debugger_, client_socket_id);
|
||||
if (client->Setup()) {
|
||||
// Client failed to setup - abort.
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
45
src/xenia/dbg/ws_listener.h
Normal file
45
src/xenia/dbg/ws_listener.h
Normal file
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2013 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef XENIA_KERNEL_DBG_WS_LISTENER_H_
|
||||
#define XENIA_KERNEL_DBG_WS_LISTENER_H_
|
||||
|
||||
#include <xenia/common.h>
|
||||
#include <xenia/core.h>
|
||||
|
||||
#include <xenia/dbg/listener.h>
|
||||
|
||||
|
||||
namespace xe {
|
||||
namespace dbg {
|
||||
|
||||
|
||||
class Debugger;
|
||||
|
||||
|
||||
class WsListener : public Listener {
|
||||
public:
|
||||
WsListener(Debugger* debugger, xe_pal_ref pal, uint32_t port);
|
||||
virtual ~WsListener();
|
||||
|
||||
virtual int Setup();
|
||||
virtual int WaitForClient();
|
||||
|
||||
protected:
|
||||
uint32_t port_;
|
||||
|
||||
int socket_id_;
|
||||
};
|
||||
|
||||
|
||||
} // namespace dbg
|
||||
} // namespace xe
|
||||
|
||||
|
||||
#endif // XENIA_KERNEL_DBG_WS_LISTENER_H_
|
||||
Reference in New Issue
Block a user