clean up network code
This commit is contained in:
parent
8c9f5db445
commit
70bcea33ab
@ -217,10 +217,6 @@
|
|||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath="..\sync\network.h"
|
|
||||||
>
|
|
||||||
</File>
|
|
||||||
<File
|
|
||||||
RelativePath=".\recentfiles.h"
|
RelativePath=".\recentfiles.h"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
|
|||||||
@ -4,7 +4,6 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "../sync/network.h"
|
|
||||||
#include "../sync/data.h"
|
#include "../sync/data.h"
|
||||||
#include <stack>
|
#include <stack>
|
||||||
#include <list>
|
#include <list>
|
||||||
|
|||||||
@ -40,7 +40,6 @@ RecentFiles mruFileList(NULL);
|
|||||||
#define WM_BIASSELECTION (WM_USER+2)
|
#define WM_BIASSELECTION (WM_USER+2)
|
||||||
|
|
||||||
#include "../sync/sync.h"
|
#include "../sync/sync.h"
|
||||||
#include "../sync/network.h"
|
|
||||||
|
|
||||||
static LRESULT CALLBACK setRowsDialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
|
static LRESULT CALLBACK setRowsDialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
|
||||||
{
|
{
|
||||||
@ -558,7 +557,8 @@ int _tmain(int argc, _TCHAR* argv[])
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (!init_network())
|
WSADATA wsa;
|
||||||
|
if (0 != WSAStartup(MAKEWORD(2, 0), &wsa))
|
||||||
die("Failed to init network");
|
die("Failed to init network");
|
||||||
|
|
||||||
SOCKET serverSocket = socket(AF_INET, SOCK_STREAM, 0);
|
SOCKET serverSocket = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
@ -716,7 +716,7 @@ int _tmain(int argc, _TCHAR* argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
closesocket(serverSocket);
|
closesocket(serverSocket);
|
||||||
close_network();
|
WSACleanup();
|
||||||
|
|
||||||
delete trackView;
|
delete trackView;
|
||||||
trackView = NULL;
|
trackView = NULL;
|
||||||
|
|||||||
39
sync/base.h
39
sync/base.h
@ -1,3 +1,7 @@
|
|||||||
|
/* Copyright (C) 2007-2010 Erik Faye-Lund and Egbert Teeselink
|
||||||
|
* For conditions of distribution and use, see copyright notice in LICENSE.TXT
|
||||||
|
*/
|
||||||
|
|
||||||
#ifndef SYNC_BASE_H
|
#ifndef SYNC_BASE_H
|
||||||
#define SYNC_BASE_H
|
#define SYNC_BASE_H
|
||||||
|
|
||||||
@ -6,4 +10,39 @@
|
|||||||
#define strdup _strdup
|
#define strdup _strdup
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef WIN32
|
||||||
|
#include <winsock2.h>
|
||||||
|
#define WIN32_LEAN_AND_MEAN
|
||||||
|
#include <windows.h>
|
||||||
|
#else
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
#define SOCKET int
|
||||||
|
#define INVALID_SOCKET -1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static const char *client_greet = "hello, synctracker!";
|
||||||
|
static const char *server_greet = "hello, demo!";
|
||||||
|
|
||||||
|
enum {
|
||||||
|
SET_KEY = 0,
|
||||||
|
DELETE_KEY = 1,
|
||||||
|
GET_TRACK = 2,
|
||||||
|
SET_ROW = 3,
|
||||||
|
PAUSE = 4,
|
||||||
|
SAVE_TRACKS = 5
|
||||||
|
};
|
||||||
|
|
||||||
|
static inline int socket_poll(SOCKET socket)
|
||||||
|
{
|
||||||
|
struct timeval to = { 0, 0 };
|
||||||
|
fd_set fds;
|
||||||
|
|
||||||
|
FD_ZERO(&fds);
|
||||||
|
FD_SET(socket, &fds);
|
||||||
|
|
||||||
|
return select(0, &fds, NULL, NULL, &to) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
#endif /* SYNC_BASE_H */
|
#endif /* SYNC_BASE_H */
|
||||||
|
|||||||
120
sync/device.c
120
sync/device.c
@ -28,25 +28,44 @@ const char *sync_track_path(const char *base, const char *name)
|
|||||||
static SOCKET server_connect(const char *host, int nport)
|
static SOCKET server_connect(const char *host, int nport)
|
||||||
{
|
{
|
||||||
struct hostent *he;
|
struct hostent *he;
|
||||||
struct sockaddr_in addr;
|
struct sockaddr_in sa;
|
||||||
char greet[128];
|
char greet[128], **ap;
|
||||||
SOCKET sock;
|
SOCKET sock = INVALID_SOCKET;
|
||||||
|
|
||||||
if (!init_network())
|
#ifdef WIN32
|
||||||
return INVALID_SOCKET;
|
static int need_init = 1;
|
||||||
|
if (need_init) {
|
||||||
|
WSADATA wsa;
|
||||||
|
if (WSAStartup(MAKEWORD(2, 0), &wsa))
|
||||||
|
return INVALID_SOCKET;
|
||||||
|
need_init = 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
he = gethostbyname(host);
|
he = gethostbyname(host);
|
||||||
if (!he)
|
if (!he)
|
||||||
return INVALID_SOCKET;
|
return INVALID_SOCKET;
|
||||||
|
|
||||||
addr.sin_family = AF_INET;
|
for (ap = he->h_addr_list; *ap; ++ap) {
|
||||||
addr.sin_port = htons(nport);
|
sa.sin_family = he->h_addrtype;
|
||||||
addr.sin_addr.s_addr = ((struct in_addr *)(he->h_addr_list[0]))->s_addr;
|
sa.sin_port = htons(nport);
|
||||||
|
memcpy(&sa.sin_addr, *ap, he->h_length);
|
||||||
|
|
||||||
|
sock = socket(he->h_addrtype, SOCK_STREAM, 0);
|
||||||
|
if (sock == INVALID_SOCKET)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (connect(sock, (struct sockaddr *)&sa, sizeof(sa)) >= 0)
|
||||||
|
break;
|
||||||
|
|
||||||
|
closesocket(sock);
|
||||||
|
sock = INVALID_SOCKET;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sock == INVALID_SOCKET)
|
||||||
|
return INVALID_SOCKET;
|
||||||
|
|
||||||
sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
||||||
connect(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
|
|
||||||
send(sock, client_greet, (int)strlen(client_greet), 0);
|
send(sock, client_greet, (int)strlen(client_greet), 0);
|
||||||
|
|
||||||
recv(sock, greet, (int)strlen(server_greet), 0);
|
recv(sock, greet, (int)strlen(server_greet), 0);
|
||||||
if (!strncmp(server_greet, greet, strlen(server_greet)))
|
if (!strncmp(server_greet, greet, strlen(server_greet)))
|
||||||
return sock;
|
return sock;
|
||||||
@ -171,27 +190,40 @@ static int hanle_set_key_cmd(SOCKET sock, struct sync_data *data)
|
|||||||
ret += recv(sock, (char *)&key.value, sizeof(float), 0);
|
ret += recv(sock, (char *)&key.value, sizeof(float), 0);
|
||||||
ret += recv(sock, (char *)&type, 1, 0);
|
ret += recv(sock, (char *)&type, 1, 0);
|
||||||
if (ret != sizeof(int) * 2 + sizeof(float) + 1)
|
if (ret != sizeof(int) * 2 + sizeof(float) + 1)
|
||||||
return 1;
|
return 0;
|
||||||
|
|
||||||
assert(type < KEY_TYPE_COUNT);
|
assert(type < KEY_TYPE_COUNT);
|
||||||
assert(track < (int)data->num_tracks);
|
assert(track < (int)data->num_tracks);
|
||||||
|
|
||||||
key.type = (enum key_type)type;
|
key.type = (enum key_type)type;
|
||||||
sync_set_key(data->tracks[track], &key);
|
sync_set_key(data->tracks[track], &key);
|
||||||
return 0;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int hanle_del_key_cmd(SOCKET sock, struct sync_data *data)
|
static int hanle_del_key_cmd(SOCKET sock, struct sync_data *data)
|
||||||
{
|
{
|
||||||
int ret, track, row;
|
int ret, track, row;
|
||||||
|
|
||||||
ret = recv(sock, (char *)&track, sizeof(int), 0);
|
ret = recv(sock, (char *)&track, sizeof(int), 0);
|
||||||
ret += recv(sock, (char *)&row, sizeof(int), 0);
|
ret += recv(sock, (char *)&row, sizeof(int), 0);
|
||||||
if (ret != sizeof(int) * 2)
|
if (ret != sizeof(int) * 2)
|
||||||
return 1;
|
return 0;
|
||||||
|
|
||||||
assert(track < (int)data->num_tracks);
|
assert(track < (int)data->num_tracks);
|
||||||
|
|
||||||
sync_del_key(data->tracks[track], row);
|
sync_del_key(data->tracks[track], row);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int purge_and_rerequest(struct sync_device *d)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < (int)d->data.num_tracks; ++i) {
|
||||||
|
free(d->data.tracks[i]->keys);
|
||||||
|
d->data.tracks[i]->keys = NULL;
|
||||||
|
d->data.tracks[i]->num_keys = 0;
|
||||||
|
|
||||||
|
if (request_track_data(d->sock, d->data.tracks[i]->name, i));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -199,45 +231,36 @@ void sync_update(struct sync_device *d, double row)
|
|||||||
{
|
{
|
||||||
if (d->sock == INVALID_SOCKET) {
|
if (d->sock == INVALID_SOCKET) {
|
||||||
d->sock = server_connect(REMOTE_HOST, REMOTE_PORT);
|
d->sock = server_connect(REMOTE_HOST, REMOTE_PORT);
|
||||||
if (d->sock != INVALID_SOCKET) {
|
if (purge_and_rerequest(d))
|
||||||
int i;
|
goto sockerr;
|
||||||
for (i = 0; i < (int)d->data.num_tracks; ++i) {
|
|
||||||
/* throw out old data */
|
|
||||||
free(d->data.tracks[i]->keys);
|
|
||||||
d->data.tracks[i]->keys = NULL;
|
|
||||||
d->data.tracks[i]->num_keys = 0;
|
|
||||||
/* request new data */
|
|
||||||
request_track_data(d->sock,
|
|
||||||
d->data.tracks[i]->name, i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* look for new commands */
|
/* look for new commands */
|
||||||
while (d->sock != INVALID_SOCKET && socket_poll(d->sock)) {
|
while (d->sock != INVALID_SOCKET && socket_poll(d->sock)) {
|
||||||
unsigned char cmd = 0, flag;
|
unsigned char cmd = 0, flag;
|
||||||
int err = 1, row;
|
int row;
|
||||||
if (!recv(d->sock, (char *)&cmd, 1, 0)) {
|
if (!recv(d->sock, (char *)&cmd, 1, 0))
|
||||||
d->sock = INVALID_SOCKET;
|
goto sockerr;
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (cmd)
|
switch (cmd) {
|
||||||
{
|
|
||||||
case SET_KEY:
|
case SET_KEY:
|
||||||
err = hanle_set_key_cmd(d->sock, &d->data);
|
if (hanle_set_key_cmd(d->sock, &d->data))
|
||||||
|
goto sockerr;
|
||||||
break;
|
break;
|
||||||
case DELETE_KEY:
|
case DELETE_KEY:
|
||||||
err = hanle_del_key_cmd(d->sock, &d->data);
|
if (hanle_del_key_cmd(d->sock, &d->data))
|
||||||
|
goto sockerr;
|
||||||
break;
|
break;
|
||||||
case SET_ROW:
|
case SET_ROW:
|
||||||
err = recv(d->sock, (char *)&row, sizeof(int), 0) != sizeof(int);
|
if (recv(d->sock, (char *)&row, sizeof(int), 0) != sizeof(int))
|
||||||
if (!err && d->cb && d->cb->set_row)
|
goto sockerr;
|
||||||
|
if (d->cb && d->cb->set_row)
|
||||||
d->cb->set_row(d->cb_param, row);
|
d->cb->set_row(d->cb_param, row);
|
||||||
break;
|
break;
|
||||||
case PAUSE:
|
case PAUSE:
|
||||||
err = recv(d->sock, (char *)&flag, 1, 0) != 1;
|
if (recv(d->sock, (char *)&flag, 1, 0) != 1)
|
||||||
if (!err && d->cb && d->cb->pause)
|
goto sockerr;
|
||||||
|
if (d->cb && d->cb->pause)
|
||||||
d->cb->pause(d->cb_param, flag);
|
d->cb->pause(d->cb_param, flag);
|
||||||
break;
|
break;
|
||||||
case SAVE_TRACKS:
|
case SAVE_TRACKS:
|
||||||
@ -245,10 +268,7 @@ void sync_update(struct sync_device *d, double row)
|
|||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, "unknown cmd: %02x\n", cmd);
|
fprintf(stderr, "unknown cmd: %02x\n", cmd);
|
||||||
}
|
goto sockerr;
|
||||||
if (err) {
|
|
||||||
d->sock = INVALID_SOCKET;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -259,11 +279,15 @@ void sync_update(struct sync_device *d, double row)
|
|||||||
int ret = send(d->sock, (char*)&cmd, 1, 0);
|
int ret = send(d->sock, (char*)&cmd, 1, 0);
|
||||||
ret += send(d->sock, (char*)&nrow, sizeof(int), 0);
|
ret += send(d->sock, (char*)&nrow, sizeof(int), 0);
|
||||||
if (ret != sizeof(int) + 1)
|
if (ret != sizeof(int) + 1)
|
||||||
d->sock = INVALID_SOCKET;
|
goto sockerr;
|
||||||
else
|
d->row = nrow;
|
||||||
d->row = nrow;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return;
|
||||||
|
|
||||||
|
sockerr:
|
||||||
|
d->sock = INVALID_SOCKET;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -8,10 +8,6 @@
|
|||||||
#include "track.h"
|
#include "track.h"
|
||||||
#include "data.h"
|
#include "data.h"
|
||||||
|
|
||||||
#ifndef SYNC_PLAYER
|
|
||||||
#include "network.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -1,65 +0,0 @@
|
|||||||
/* Copyright (C) 2007-2010 Erik Faye-Lund and Egbert Teeselink
|
|
||||||
* For conditions of distribution and use, see copyright notice in LICENSE.TXT
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef SYNC_NETWORK_H
|
|
||||||
#define SYNC_NETWORK_H
|
|
||||||
|
|
||||||
#ifdef WIN32
|
|
||||||
#include <winsock2.h>
|
|
||||||
#define WIN32_LEAN_AND_MEAN
|
|
||||||
#include <windows.h>
|
|
||||||
#else
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <netdb.h>
|
|
||||||
#define SOCKET int
|
|
||||||
#define INVALID_SOCKET -1
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "base.h"
|
|
||||||
|
|
||||||
static const char *client_greet = "hello, synctracker!";
|
|
||||||
static const char *server_greet = "hello, demo!";
|
|
||||||
|
|
||||||
enum {
|
|
||||||
SET_KEY = 0,
|
|
||||||
DELETE_KEY = 1,
|
|
||||||
GET_TRACK = 2,
|
|
||||||
SET_ROW = 3,
|
|
||||||
PAUSE = 4,
|
|
||||||
SAVE_TRACKS = 5
|
|
||||||
};
|
|
||||||
|
|
||||||
static inline int init_network()
|
|
||||||
{
|
|
||||||
#ifdef WIN32
|
|
||||||
WSADATA wsa;
|
|
||||||
if (0 != WSAStartup(MAKEWORD(2, 0), &wsa))
|
|
||||||
return 0;
|
|
||||||
if (LOBYTE(wsa.wVersion) != 2 || HIBYTE(wsa.wVersion) != 0)
|
|
||||||
return 0;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void close_network()
|
|
||||||
{
|
|
||||||
#ifdef WIN32
|
|
||||||
WSACleanup();
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline int socket_poll(SOCKET socket)
|
|
||||||
{
|
|
||||||
struct timeval to = { 0, 0 };
|
|
||||||
fd_set fds;
|
|
||||||
|
|
||||||
FD_ZERO(&fds);
|
|
||||||
FD_SET(socket, &fds);
|
|
||||||
|
|
||||||
return select(0, &fds, NULL, NULL, &to) > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif /* SYNC_NETWORK_H */
|
|
||||||
@ -298,10 +298,6 @@
|
|||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath=".\sync\network.h"
|
|
||||||
>
|
|
||||||
</File>
|
|
||||||
<File
|
|
||||||
RelativePath=".\sync\sync.h"
|
RelativePath=".\sync\sync.h"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user