Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
MicroCART
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Distributed Autonomous Networked Control Lab
MicroCART
Commits
37a7a58a
Commit
37a7a58a
authored
8 years ago
by
burneykb
Browse files
Options
Downloads
Patches
Plain Diff
added safe_close_fd function
parent
7e0c65b8
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
groundStation/src/microcart_cli.c
+19
-13
19 additions, 13 deletions
groundStation/src/microcart_cli.c
with
19 additions
and
13 deletions
groundStation/src/microcart_cli.c
+
19
−
13
View file @
37a7a58a
...
@@ -43,6 +43,7 @@ void performCommand(char *, char *);
...
@@ -43,6 +43,7 @@ void performCommand(char *, char *);
int
startsWith
(
const
char
*
,
const
char
*
);
int
startsWith
(
const
char
*
,
const
char
*
);
int
safe_fd_set
(
int
,
fd_set
*
,
int
*
);
int
safe_fd_set
(
int
,
fd_set
*
,
int
*
);
int
safe_fd_clr
(
int
,
fd_set
*
,
int
*
);
int
safe_fd_clr
(
int
,
fd_set
*
,
int
*
);
static
void
safe_close_fd
(
int
fd
,
pthread_mutex_t
*
mutexLock
);
static
void
cb
(
struct
ucart_vrpn_TrackerData
*
);
static
void
cb
(
struct
ucart_vrpn_TrackerData
*
);
static
int
new_client
(
int
fd
);
static
int
new_client
(
int
fd
);
...
@@ -51,7 +52,7 @@ static ssize_t get_client_index(int fd);
...
@@ -51,7 +52,7 @@ static ssize_t get_client_index(int fd);
/* Returns pointer to client buffer, or -1 */
/* Returns pointer to client buffer, or -1 */
static
char
*
get_client_buffer
(
int
fd
);
static
char
*
get_client_buffer
(
int
fd
);
/* Returns -1 on error */
/* Returns -1 on error */
static
int
remove_client
(
fd
);
static
int
remove_client
(
int
fd
);
/* Thread-safe wrappers */
/* Thread-safe wrappers */
pthread_mutex_t
quadSocketMutex
;
pthread_mutex_t
quadSocketMutex
;
...
@@ -252,9 +253,8 @@ int main(int argc, char **argv)
...
@@ -252,9 +253,8 @@ int main(int argc, char **argv)
for
(
int
i
=
0
;
i
<
MAX_CLIENTS
;
++
i
)
{
for
(
int
i
=
0
;
i
<
MAX_CLIENTS
;
++
i
)
{
if
(
client_fds
[
i
]
!=
-
1
)
{
if
(
client_fds
[
i
]
!=
-
1
)
{
char
buff
;
char
buff
;
if
(
recv
(
client_fds
[
i
],
&
buff
,
1
,
MSG_PEEK
|
MSG_DONTWAIT
)
<
0
)
{
if
(
recv
(
client_fds
[
i
],
&
buff
,
1
,
MSG_PEEK
|
MSG_DONTWAIT
)
<
0
)
remove_client
(
client_fds
[
i
]);
remove_client
(
client_fds
[
i
]);
}
}
}
}
}
}
}
...
@@ -262,13 +262,9 @@ int main(int argc, char **argv)
...
@@ -262,13 +262,9 @@ int main(int argc, char **argv)
// ucart_vrpn_tracker_freeInstance(tracker);
// ucart_vrpn_tracker_freeInstance(tracker);
if
(
pthread_mutex_lock
(
&
quadSocketMutex
))
{
safe_close_fd
(
zyboSocket
,
&
quadSocketMutex
);
err
(
-
2
,
"pthrtead_mutex_lock (%s:%d):"
,
__FILE__
,
__LINE__
);
}
close
(
zyboSocket
);
if
(
pthread_mutex_unlock
(
&
quadSocketMutex
))
{
err
(
-
2
,
"pthrtead_mutex_unlock (%s:%d):"
,
__FILE__
,
__LINE__
);
}
closeLogFile
();
closeLogFile
();
return
0
;
return
0
;
...
@@ -479,14 +475,24 @@ static char * get_client_buffer(int fd)
...
@@ -479,14 +475,24 @@ static char * get_client_buffer(int fd)
}
}
}
}
static
int
remove_client
(
fd
)
{
static
int
remove_client
(
int
fd
)
{
ssize_t
slot
=
get_client_index
(
fd
);
ssize_t
slot
=
get_client_index
(
fd
);
if
(
slot
==
-
1
)
if
(
slot
==
-
1
)
return
slot
;
return
-
1
;
char
*
clientBuffer
=
get_client_buffer
(
fd
);
char
*
clientBuffer
=
get_client_buffer
(
fd
);
if
(
clientBuffer
==
NULL
)
if
(
clientBuffer
==
NULL
)
return
-
1
;
return
-
1
;
clientBuffer
=
'\0'
;
clientBuffer
=
'\0'
;
client_fds
[
clientIndex
]
=
-
1
;
client_fds
[
slot
]
=
-
1
;
return
0
;
return
0
;
}
static
void
safe_close_fd
(
int
fd
,
pthread_mutex_t
*
mutexLock
)
{
if
(
pthread_mutex_lock
(
mutexLock
))
{
err
(
-
2
,
"pthrtead_mutex_lock (%s:%d):"
,
__FILE__
,
__LINE__
);
}
close
(
fd
);
if
(
pthread_mutex_unlock
(
mutexLock
))
{
err
(
-
2
,
"pthrtead_mutex_unlock (%s:%d):"
,
__FILE__
,
__LINE__
);
}
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment