editor: move all client stuff to socket-class

Rename NetworkSocket to ClientSocket, and put the client-specific
functionality in it.
This commit is contained in:
Erik Faye-Lund 2011-01-27 22:34:45 +01:00
parent b3825514b2
commit 9724884884
6 changed files with 182 additions and 159 deletions

75
editor/clientsocket.cpp Normal file
View File

@ -0,0 +1,75 @@
#include "clientsocket.h"
#include "../sync/track.h"
#include <cassert>
void ClientSocket::sendSetKeyCommand(uint32_t track, const struct track_key &key)
{
if (!connected() ||
clientRemap.count(track) == 0)
return;
track = htonl(clientRemap[track]);
uint32_t row = htonl(key.row);
union {
float f;
uint32_t i;
} v;
v.f = key.value;
v.i = htonl(v.i);
assert(key.type < KEY_TYPE_COUNT);
unsigned char cmd = SET_KEY;
send((char *)&cmd, 1, 0);
send((char *)&track, sizeof(track), 0);
send((char *)&row, sizeof(row), 0);
send((char *)&v.i, sizeof(v.i), 0);
send((char *)&key.type, 1, 0);
}
void ClientSocket::sendDeleteKeyCommand(int track, int row)
{
if (!connected() ||
clientRemap.count(track) == 0)
return;
track = htonl(int(clientRemap[track]));
row = htonl(row);
unsigned char cmd = DELETE_KEY;
send((char *)&cmd, 1, 0);
send((char *)&track, sizeof(int), 0);
send((char *)&row, sizeof(int), 0);
}
void ClientSocket::sendSetRowCommand(int row)
{
if (!connected())
return;
unsigned char cmd = SET_ROW;
row = htonl(row);
send((char *)&cmd, 1, 0);
send((char *)&row, sizeof(int), 0);
}
void ClientSocket::sendPauseCommand(bool pause)
{
if (!connected())
return;
unsigned char cmd = PAUSE, flag = pause;
send((char *)&cmd, 1, 0);
send((char *)&flag, 1, 0);
clientPaused = pause;
}
void ClientSocket::sendSaveCommand()
{
if (!connected())
return;
unsigned char cmd = SAVE_TRACKS;
send((char *)&cmd, 1, 0);
}

62
editor/clientsocket.h Normal file
View File

@ -0,0 +1,62 @@
#include "../sync/base.h"
#include <map>
class ClientSocket {
public:
ClientSocket() : socket(INVALID_SOCKET) {}
explicit ClientSocket(SOCKET socket) : socket(socket), clientPaused(true) {}
bool connected() const
{
return INVALID_SOCKET != socket;
}
void disconnect()
{
closesocket(socket);
socket = INVALID_SOCKET;
}
bool recv(char *buffer, size_t length, int flags)
{
if (!connected())
return false;
int ret = ::recv(socket, buffer, int(length), flags);
if (ret != int(length)) {
disconnect();
return false;
}
return true;
}
bool send(const char *buffer, size_t length, int flags)
{
if (!connected())
return false;
int ret = ::send(socket, buffer, int(length), flags);
if (ret != int(length)) {
disconnect();
return false;
}
return true;
}
bool pollRead()
{
if (!connected())
return false;
return !!socket_poll(socket);
}
void sendSetKeyCommand(uint32_t track, const struct track_key &key);
void sendDeleteKeyCommand(int track, int row);
void sendSetRowCommand(int row);
void sendPauseCommand(bool pause);
void sendSaveCommand();
bool clientPaused;
std::map<size_t, size_t> clientRemap;
private:
SOCKET socket;
};

View File

@ -269,7 +269,7 @@ bool fileSaveAs()
if (GetSaveFileNameW(&ofn)) {
if (document.save(temp)) {
document.sendSaveCommand();
document.clientSocket.sendSaveCommand();
setWindowFileName(temp);
fileName = temp;
@ -289,7 +289,7 @@ bool fileSave()
return fileSaveAs();
if (!document.save(fileName.c_str())) {
document.sendSaveCommand();
document.clientSocket.sendSaveCommand();
error("Failed to save file");
return false;
}
@ -422,7 +422,7 @@ static LRESULT CALLBACK mainWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARA
break;
case ID_FILE_REMOTEEXPORT:
document.sendSaveCommand();
document.clientSocket.sendSaveCommand();
break;
case ID_RECENTFILES_FILE1:
@ -562,7 +562,7 @@ SOCKET clientConnect(SOCKET serverSocket, sockaddr_in *host)
}
size_t clientIndex;
void processCommand(NetworkSocket &sock)
void processCommand(ClientSocket &sock)
{
int strLen, serverIndex, newRow;
std::string trackName;
@ -589,12 +589,12 @@ void processCommand(NetworkSocket &sock)
int(document.createTrack(trackName));
// setup remap
document.clientRemap[serverIndex] = clientIndex++;
document.clientSocket.clientRemap[serverIndex] = clientIndex++;
// send key-frames
t = document.tracks[serverIndex];
for (int i = 0; i < (int)t->num_keys; ++i)
document.sendSetKeyCommand(int(serverIndex),
document.clientSocket.sendSetKeyCommand(int(serverIndex),
t->keys[i]);
InvalidateRect(trackViewWin, NULL, FALSE);
@ -697,29 +697,26 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/,
char temp[256];
snprintf(temp, 256, "Connected to %s", inet_ntoa(client.sin_addr));
SendMessage(statusBarWin, SB_SETTEXT, 0, (LPARAM)temp);
document.clientSocket = NetworkSocket(clientSocket);
document.clientRemap.clear();
document.clientSocket = ClientSocket(clientSocket);
clientIndex = 0;
document.sendPauseCommand(true);
document.sendSetRowCommand(trackView->getEditRow());
document.clientSocket.sendPauseCommand(true);
document.clientSocket.sendSetRowCommand(trackView->getEditRow());
guiConnected = true;
}
else SendMessage(statusBarWin, SB_SETTEXT, 0, (LPARAM)"Not Connected.");
}
}
if (document.clientSocket.connected())
{
NetworkSocket &clientSocket = document.clientSocket;
if (document.clientSocket.connected()) {
ClientSocket &clientSocket = document.clientSocket;
// look for new commands
while (clientSocket.pollRead())
processCommand(clientSocket);
}
if (!document.clientSocket.connected() && guiConnected)
{
document.clientPaused = true;
if (!document.clientSocket.connected() && guiConnected) {
document.clientSocket.clientPaused = true;
InvalidateRect(trackViewWin, NULL, FALSE);
SendMessage(statusBarWin, SB_SETTEXT, 0, (LPARAM)"Not Connected.");
guiConnected = false;

View File

@ -179,6 +179,10 @@
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\clientsocket.cpp"
>
</File>
<File
RelativePath="..\sync\data.c"
>
</File>
@ -209,6 +213,10 @@
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath=".\clientsocket.h"
>
</File>
<File
RelativePath="..\sync\data.h"
>
</File>
@ -247,11 +255,11 @@
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
<File
RelativePath=".\editor.rc"
RelativePath=".\appicon.ico"
>
</File>
<File
RelativePath=".\appicon.ico"
RelativePath=".\editor.rc"
>
</File>
</Filter>

View File

@ -11,66 +11,15 @@ extern "C" {
#include <stack>
#include <list>
#include <vector>
#include <map>
#include <string>
#include <cassert>
class NetworkSocket
{
public:
NetworkSocket() : socket(INVALID_SOCKET) {}
explicit NetworkSocket(SOCKET socket) : socket(socket) {}
bool connected() const
{
return INVALID_SOCKET != socket;
}
void disconnect()
{
closesocket(socket);
socket = INVALID_SOCKET;
}
bool recv(char *buffer, size_t length, int flags)
{
if (!connected())
return false;
int ret = ::recv(socket, buffer, int(length), flags);
if (ret != int(length)) {
disconnect();
return false;
}
return true;
}
bool send(const char *buffer, size_t length, int flags)
{
if (!connected())
return false;
int ret = ::send(socket, buffer, int(length), flags);
if (ret != int(length)) {
disconnect();
return false;
}
return true;
}
bool pollRead()
{
if (!connected())
return false;
return !!socket_poll(socket);
}
private:
SOCKET socket;
};
#include "clientsocket.h"
class SyncDocument : public sync_data
{
public:
SyncDocument() : clientPaused(true), rows(128), savePointDelta(0), savePointUnreachable(true)
SyncDocument() : rows(128), savePointDelta(0), savePointUnreachable(true)
{
this->tracks = NULL;
this->num_tracks = 0;
@ -84,71 +33,7 @@ public:
trackOrder.push_back(index);
return index;
}
void sendSetKeyCommand(uint32_t track, const struct track_key &key)
{
if (!clientSocket.connected()) return;
if (clientRemap.count(track) == 0) return;
track = htonl(clientRemap[track]);
uint32_t row = htonl(key.row);
union {
float f;
uint32_t i;
} v;
v.f = key.value;
v.i = htonl(v.i);
assert(key.type < KEY_TYPE_COUNT);
unsigned char cmd = SET_KEY;
clientSocket.send((char*)&cmd, 1, 0);
clientSocket.send((char*)&track, sizeof(track), 0);
clientSocket.send((char*)&row, sizeof(row), 0);
clientSocket.send((char*)&v.i, sizeof(v.i), 0);
clientSocket.send((char*)&key.type, 1, 0);
}
void sendDeleteKeyCommand(int track, int row)
{
if (!clientSocket.connected()) return;
if (clientRemap.count(track) == 0) return;
track = htonl(int(clientRemap[track]));
row = htonl(row);
unsigned char cmd = DELETE_KEY;
clientSocket.send((char*)&cmd, 1, 0);
clientSocket.send((char*)&track, sizeof(int), 0);
clientSocket.send((char*)&row, sizeof(int), 0);
}
void sendSetRowCommand(int row)
{
if (!clientSocket.connected()) return;
unsigned char cmd = SET_ROW;
row = htonl(row);
clientSocket.send((char*)&cmd, 1, 0);
clientSocket.send((char*)&row, sizeof(int), 0);
}
void sendPauseCommand(bool pause)
{
if (!clientSocket.connected()) return;
unsigned char cmd = PAUSE;
clientSocket.send((char*)&cmd, 1, 0);
unsigned char flag = pause;
clientSocket.send((char*)&flag, 1, 0);
clientPaused = pause;
}
void sendSaveCommand()
{
if (!clientSocket.connected()) return;
unsigned char cmd = SAVE_TRACKS;
clientSocket.send((char*)&cmd, 1, 0);
}
class Command
{
public:
@ -169,7 +54,7 @@ public:
assert(!is_key_frame(t, key.row));
if (sync_set_key(t, &key))
throw std::bad_alloc("sync_set_key");
data->sendSetKeyCommand(track, key); // update clients
data->clientSocket.sendSetKeyCommand(track, key); // update clients
}
void undo(SyncDocument *data)
@ -178,7 +63,7 @@ public:
assert(is_key_frame(t, key.row));
if (sync_del_key(t, key.row))
throw std::bad_alloc("sync_del_key");
data->sendDeleteKeyCommand(track, key.row); // update clients
data->clientSocket.sendDeleteKeyCommand(track, key.row); // update clients
}
private:
@ -200,7 +85,7 @@ public:
oldKey = t->keys[idx];
if (sync_del_key(t, row))
throw std::bad_alloc("sync_del_key");
data->sendDeleteKeyCommand(track, row); // update clients
data->clientSocket.sendDeleteKeyCommand(track, row); // update clients
}
void undo(SyncDocument *data)
@ -209,7 +94,7 @@ public:
assert(!is_key_frame(t, row));
if (sync_set_key(t, &oldKey))
throw std::bad_alloc("sync_set_key");
data->sendSetKeyCommand(track, oldKey); // update clients
data->clientSocket.sendSetKeyCommand(track, oldKey); // update clients
}
private:
@ -232,7 +117,7 @@ public:
oldKey = t->keys[idx];
if (sync_set_key(t, &key))
throw std::bad_alloc("sync_set_key");
data->sendSetKeyCommand(track, key); // update clients
data->clientSocket.sendSetKeyCommand(track, key); // update clients
}
void undo(SyncDocument *data)
@ -241,7 +126,7 @@ public:
assert(is_key_frame(t, key.row));
if (sync_set_key(t, &oldKey))
throw std::bad_alloc("sync_set_key");
data->sendSetKeyCommand(track, oldKey); // update clients
data->clientSocket.sendSetKeyCommand(track, oldKey); // update clients
}
private:
@ -380,10 +265,8 @@ public:
if (savePointUnreachable) return true;
return 0 != savePointDelta;
}
NetworkSocket clientSocket;
std::map<size_t, size_t> clientRemap;
bool clientPaused;
ClientSocket clientSocket;
size_t getRows() const { return rows; }
void setRows(size_t rows) { this->rows = rows; }

View File

@ -191,8 +191,10 @@ void TrackView::paintTopMargin(HDC hdc, RECT rcTracks)
DrawEdge(hdc, &fillRect, BDR_RAISEDINNER | BDR_RAISEDOUTER, BF_ADJUST | BF_LEFT | BF_RIGHT | BF_BOTTOM);
FillRect(hdc, &fillRect, bgBrush);
if (doc->clientRemap.count(doc->getTrackIndexFromPos(track)) == 0) SetTextColor(hdc, GetSysColor(COLOR_GRAYTEXT));
else SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
if (!doc->clientSocket.clientRemap.count(doc->getTrackIndexFromPos(track)))
SetTextColor(hdc, GetSysColor(COLOR_GRAYTEXT));
else
SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
TextOut(hdc, fillRect.left, 0, t->name, int(strlen(t->name)));
}
@ -609,10 +611,8 @@ void TrackView::setEditRow(int newEditRow)
invalidateRange(selectStartTrack, selectStopTrack, selectStartRow, selectStopRow);
selectStartRow = selectStopRow = editRow;
selectStartTrack = selectStopTrack = editTrack;
}
if (doc->clientPaused)
{
doc->sendSetRowCommand(editRow);
} if (doc->clientSocket.clientPaused) {
doc->clientSocket.sendSetRowCommand(editRow);
}
SendMessage(GetParent(getWin()), WM_ROWCHANGED, 0, editRow);
SendMessage(GetParent(getWin()), WM_CURRVALDIRTY, 0, 0);
@ -954,10 +954,8 @@ LRESULT TrackView::onKeyDown(UINT keyCode, UINT /*flags*/)
}
}
if (editString.empty() && doc->clientPaused)
{
switch (keyCode)
{
if (editString.empty() && doc->clientSocket.clientPaused) {
switch (keyCode) {
case VK_UP:
if (GetKeyState(VK_CONTROL) < 0)
{
@ -1044,7 +1042,7 @@ LRESULT TrackView::onKeyDown(UINT keyCode, UINT /*flags*/)
invalidatePos(editTrack, editRow);
MessageBeep(~0U);
}
doc->sendPauseCommand( !doc->clientPaused );
doc->clientSocket.sendPauseCommand( !doc->clientSocket.clientPaused );
break;
}
return FALSE;