Skip to content

Commit 1be3371

Browse files
author
Levente Orban
committed
Implement the socket communication (#7)
JerryScript-DCO-1.0-Signed-off-by: Levente Orban orbanl@inf.u-szeged.hu
1 parent 687a2d9 commit 1be3371

File tree

2 files changed

+120
-1
lines changed

2 files changed

+120
-1
lines changed

jerry-core/debugger/jerry-debugger.c

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,114 @@
1-
//TODO: Implement the socket communication
1+
/* Copyright 2016 University of Szeged.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
#include <errno.h>
17+
#include <arpa/inet.h>
18+
#include <unistd.h>
19+
#include <string.h>
20+
#include <stdbool.h>
21+
22+
#include "jerry-debugger.h"
23+
#include "jerry-port.h"
24+
25+
#define PORT 5001
26+
#define BACKLOG 1
27+
28+
static int jerry_debugger_connection; /**< hold the file descriptor for the accepted socket */
29+
30+
bool jerry_debugger_socket_init ()
31+
{
32+
/* The arguments optval is used to access option values for setsockopt(). */
33+
bool optval = true;
34+
35+
int jerry_debugger_socket; /**< socket file descriptor for the remote communication */
36+
37+
struct sockaddr_in server_addr, client_addr; /**< declarations of the socket address */
38+
socklen_t sin_size = sizeof (struct sockaddr_in); /**< size of the structure pointed by
39+
* the server_addr and client_addr */
40+
41+
/* Server adress declaration */
42+
server_addr.sin_family = AF_INET;
43+
server_addr.sin_port = htons (PORT);
44+
server_addr.sin_addr.s_addr = INADDR_ANY;
45+
bzero (&(server_addr.sin_zero), BACKLOG);
46+
47+
/* Create an endpoint for communication */
48+
if ((jerry_debugger_socket = socket (AF_INET, SOCK_STREAM, 0)) == -1)
49+
{
50+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: %s\n", strerror (errno));
51+
return false;
52+
}
53+
54+
/* Set the options on socket */
55+
if (setsockopt (jerry_debugger_socket, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof (int)) == -1)
56+
{
57+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: %s\n", strerror (errno));
58+
return false;
59+
}
60+
61+
/* Bind to the server address */
62+
if (bind (jerry_debugger_socket, (struct sockaddr *)&server_addr, sizeof (struct sockaddr)) == -1)
63+
{
64+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: %s\n", strerror (errno));
65+
return false;
66+
}
67+
68+
/* Listen for connections on socket */
69+
if (listen (jerry_debugger_socket, BACKLOG) == -1)
70+
{
71+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: %s\n", strerror (errno));
72+
return false;
73+
}
74+
75+
jerry_port_log (JERRY_LOG_LEVEL_DEBUG, "Waiting for the client connection.\n");
76+
77+
/* Connect from the client */
78+
if ((jerry_debugger_connection = accept (jerry_debugger_socket, (struct sockaddr *)&client_addr, &sin_size)) == -1)
79+
{
80+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: %s\n", strerror (errno));
81+
return false;
82+
}
83+
84+
close (jerry_debugger_socket);
85+
86+
jerry_port_log (JERRY_LOG_LEVEL_DEBUG, "Connected from: %s:%d\n",
87+
inet_ntoa (client_addr.sin_addr), ntohs (client_addr.sin_port));
88+
89+
return true;
90+
} /* jerry_debugger_socket_init */
91+
92+
/* Close the socket connection with the client */
93+
void jerry_debugger_connection_end ()
94+
{
95+
jerry_port_log (JERRY_LOG_LEVEL_DEBUG, "TCPServer connection closed on port: %d\n", PORT);
96+
close (jerry_debugger_connection);
97+
} /* jerry_debugger_connection_end */
98+
99+
/* Send the parsed file names to the client side */
100+
bool jerry_debugger_send (size_t data_len) /**< data length */
101+
{
102+
ssize_t byte_send = send (jerry_debugger_connection, jerry_debugger_buffer, data_len, 0);
103+
104+
while (byte_send != (ssize_t) data_len)
105+
{
106+
if (byte_send == -1)
107+
{
108+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: %s\n", strerror (errno));
109+
return false;
110+
}
111+
byte_send += send (jerry_debugger_connection, jerry_debugger_buffer, data_len - (size_t) byte_send, 0);
112+
}
113+
return true;
114+
} /* jerry_debugger_send */

jerry-core/debugger/jerry-debugger.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121

2222
#define MAX_BUFFER_SIZE 128
2323

24+
extern uint8_t jerry_debugger_buffer[MAX_BUFFER_SIZE];
25+
26+
extern bool jerry_debugger_socket_init (void);
27+
extern void jerry_debugger_connection_end (void);
28+
extern bool jerry_debugger_send (size_t data_len);
29+
2430
/**
2531
* Limited resources available for the engine, so it is important to
2632
* check the maximum buffer size. It need to be between 64 and 256.

0 commit comments

Comments
 (0)