Skip to content

Commit 8541288

Browse files
author
Levente Orban
committed
Implement the socket communication
JerryScript-DCO-1.0-Signed-off-by: Levente Orban orbanl@inf.u-szeged.hu
1 parent 85eca53 commit 8541288

File tree

2 files changed

+122
-5
lines changed

2 files changed

+122
-5
lines changed

jerry-core/debugger/jerry-debugger.c

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,107 @@
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+
18+
#include "jerry-debugger.h"
19+
#include "jerry-port.h"
20+
21+
#define PORT 5001
22+
#define BACKLOG 1
23+
24+
int sock; /**< socket file descriptor for the remote communication */
25+
int connected; /**< hold the file descriptor for the accepted socket */
26+
27+
struct sockaddr_in server_addr, client_addr; /**< declarations of the socket address */
28+
socklen_t sin_size = sizeof (struct sockaddr_in); /**< size of the structure pointed by
29+
* the server_addr and client_addr */
30+
31+
bool remote_init ()
32+
{
33+
/* The arguments optval is used to access option values for setsockopt(). */
34+
bool optval = true;
35+
36+
/* Server adress declaration */
37+
server_addr.sin_family = AF_INET;
38+
server_addr.sin_port = htons (PORT);
39+
server_addr.sin_addr.s_addr = INADDR_ANY;
40+
bzero (&(server_addr.sin_zero), BACKLOG);
41+
42+
/* Create an endpoint for communication */
43+
if ((sock = socket (AF_INET, SOCK_STREAM, 0)) == -1)
44+
{
45+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Socket error!\n");
46+
return false;
47+
}
48+
49+
/* Set the options on socket */
50+
if (setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof (int)) == -1)
51+
{
52+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Setsockopt error!\n");
53+
return false;
54+
}
55+
56+
/* Bind to the server address */
57+
if (bind (sock, (struct sockaddr *)&server_addr, sizeof (struct sockaddr)) == -1)
58+
{
59+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Bind error, unable to bind!\n");
60+
return false;
61+
}
62+
63+
/* Listen for connections on socket */
64+
if (listen (sock, BACKLOG) == -1)
65+
{
66+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Listen error!\n");
67+
return false;
68+
}
69+
70+
jerry_port_log (JERRY_LOG_LEVEL_DEBUG, "Waiting for the client connection.\n");
71+
72+
/* Connect from the client */
73+
connected = accept (sock, (struct sockaddr *)&client_addr, &sin_size);
74+
75+
jerry_port_log (JERRY_LOG_LEVEL_DEBUG,"Connected from: %s:%d\n",
76+
inet_ntoa (client_addr.sin_addr),ntohs (client_addr.sin_port));
77+
78+
return true;
79+
} /* remote_init */
80+
81+
/* Close the socket connection with the client */
82+
void connection_closed ()
83+
{
84+
jerry_port_log (JERRY_LOG_LEVEL_DEBUG, "TCPServer connection closed on port %d.\n", PORT);
85+
close (connected);
86+
close (sock);
87+
} /* connection_closed */
88+
89+
/* Send the parsed file names to the client side */
90+
void send_to_client (uint16_t data_len) /**< data length */
91+
{
92+
ssize_t byte_send;
93+
94+
byte_send = send (connected, jerry_debugger_buffer, data_len, 0);
95+
96+
/* If send returns -1 then some error messages are generated by the socket layer. */
97+
if (byte_send == -1)
98+
{
99+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: %s\n", strerror (errno));
100+
}
101+
/* Compare the byte_send size with the data_len. If it is not equal then not all of the
102+
* information is sent to the client. */
103+
else if (byte_send != (ssize_t) data_len)
104+
{
105+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error, missing bytes from the pacakge...\n");
106+
}
107+
} /* send_to_client */

jerry-core/debugger/jerry-debugger.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,23 @@
1616
#ifndef JERRY_DEBUGGER_H
1717
#define JERRY_DEBUGGER_H
1818

19+
#include <arpa/inet.h>
20+
#include <unistd.h>
21+
#include <string.h>
22+
#include <stdbool.h>
23+
1924
#define MAX_MESSAGE_SIZE 128
2025

26+
static uint8_t jerry_debugger_buffer[MAX_MESSAGE_SIZE];
27+
28+
extern bool remote_init (void);
29+
extern void connection_closed (void);
30+
extern void send_to_client (uint16_t data_len);
31+
2132
/**
2233
* Package header
2334
*/
24-
typdef struct
35+
typedef struct
2536
{
2637
uint8_t type; /**< type of the message */
2738
uint8_t size; /**< size of the message */
@@ -30,10 +41,10 @@ typdef struct
3041
/**
3142
* Source file name
3243
*/
33-
typdef struct
44+
typedef struct
3445
{
35-
jerry_debug_message_header header; /**< header of the source file name struct */
36-
char file_name[1]; /**< the message */
46+
jerry_debug_message_header_t header; /**< header of the source file name struct */
47+
char file_name[1]; /**< the message */
3748
} jerry_debug_message_source_name_t;
3849

3950
#endif /* JERRY_DEBUGGER_H */

0 commit comments

Comments
 (0)