forked from rolandoam/JavaScriptCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleTCPServer.cpp
More file actions
64 lines (52 loc) · 1.28 KB
/
SimpleTCPServer.cpp
File metadata and controls
64 lines (52 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//
// SimpleTCPServer.cpp
// JavaScriptCore
//
// Created by Rolando Abarca on 2/10/12.
// Copyright (c) 2012 Zynga Inc. All rights reserved.
//
#include "config.h"
#include <string>
#include "SimpleTCPServer.h"
// auto-generated file with ragel
#include "simple_debug.c"
using namespace JSCDebug;
SimpleTCPServer::SimpleTCPServer(int port)
{
struct hostent *hp;
struct sockaddr_in sa;
printf("starting tcp server on port %d\n", port);
memset(&sa, 0, sizeof(struct sockaddr_in));
// listen all
hp = gethostbyname("0.0.0.0");
ASSERT(hp);
sa.sin_port = htons(port);
sa.sin_family = hp->h_addrtype;
m_socket = socket(AF_INET, SOCK_STREAM, 0);
ASSERT(m_socket > 0);
int on = 1;
int res = setsockopt(m_socket, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
(void)res;
ASSERT(res == 0);
if (bind(m_socket, (const struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) {
close(m_socket);
ASSERT(0);
}
}
SimpleTCPServer::~SimpleTCPServer()
{
close(m_socket);
}
void SimpleTCPServer::start(JSCDebug::JSCDebugger *debugger)
{
listen(m_socket, 1);
int t;
bool keepListening = true;
while (keepListening && (t = accept(m_socket, NULL, NULL)) > 0) {
// we got something, process...
write(t, ">> ", 3);
if (parseInput(t, debugger) == 0)
keepListening = false;
close(t);
}
}