📄 mynspsvc.cpp
字号:
return MYNSP_ERROR;
GlobalFree(QueryInfo[index].Query);
QueryInfo[index].Query = NULL;
QueryInfo[index].hHandle = QueryInfo[dwNumQueries-1].hHandle;
QueryInfo[index].iIndex = QueryInfo[dwNumQueries-1].iIndex;
QueryInfo[index].Query = QueryInfo[dwNumQueries-1].Query;
dwNumQueries--;
return MYNSP_SUCCESS;
}
//
// Function: main
//
// Description:
// The main function contains the heart of the namespace service.
// It establishes a listening socket and then waits for client
// connections. All client requests are handle in the main loop
// which means all clients are handle sequentially and one at a
// time. This is for simplicity. A real name space provider would
// most like be able to service multiple client requests at a
// time.
//
int main(int argc, char **argv)
{
WSADATA wsd;
SOCKET s, ns;
SOCKADDR_IN local, client;
char buf[BUFFER_SZ];
int clientsz,
ret,
bytesread,
bytes2follow,
byteswritten;
BYTE action;
// Load Winsock and create our listening socket
//
if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
{
printf("WSAStartup() failed: %d\n", GetLastError());
return -1;
}
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == INVALID_SOCKET)
{
printf("socket() failed: %d\n", WSAGetLastError());
return -1;
}
local.sin_family = AF_INET;
local.sin_port = htons(5150);
local.sin_addr.s_addr = htonl(INADDR_ANY);
//
// bind and listen on this socket
//
if (bind(s, (SOCKADDR *)&local, sizeof(local)) == SOCKET_ERROR)
{
printf("bind() failed: %d\n", WSAGetLastError());
return -1;
}
if (listen(s, 7) == SOCKET_ERROR)
{
printf("listen() failed: %d\n", WSAGetLastError());
return -1;
}
printf("listening...\n");
while (1)
{
// Wait for a client connection
//
ns = accept(s, (SOCKADDR *)&client, &clientsz);
if (ns == INVALID_SOCKET)
{
printf("accept() failed: %d\n", WSAGetLastError());
return -1;
}
printf("accepted client: %s:%d\n", inet_ntoa(client.sin_addr),
ntohs(client.sin_port));
//
// Read the command
//
bytesread = sizeof(action);
readdata(ns, (char *)&action, sizeof(action), &bytesread);
//
// Decipher the command and take appropriate action
//
memset(buf, 0, BUFFER_SZ);
//
// Decode the action
//
switch (action)
{
case MYNSP_REGISTER_CLASS: // Register a service class
{
WSASERVICECLASSINFO *sc=NULL, *sc2=NULL;
#ifdef DEBUG
printf("register service class\n");
#endif
bytesread = sizeof(bytes2follow);
readdata(ns, (char *)&bytes2follow, sizeof(bytes2follow), &bytesread);
readdata(ns, buf, BUFFER_SZ, &bytes2follow);
sc = (WSASERVICECLASSINFO *)GlobalAlloc(GPTR, bytes2follow);
DeMarshallServiceClassInfo(sc, buf);
#ifdef DEBUG
PrintServiceClass(sc);
#endif
//
// See if this class already exists
//
sc2 = LookupServiceClass(sc->lpServiceClassId);
if (!sc2)
{
if (dwNumServiceClasses >= MAX_SERVICES)
ret = MYNSP_SUCCESS;
else
{
ServiceClasses[dwNumServiceClasses++] = sc;
ret = MYNSP_SUCCESS;
}
}
else
ret = MYNSP_ERROR;
//
// Return error or success
//
writedata(ns, (char *)&ret, sizeof(ret), &byteswritten);
closesocket(ns);
break;
}
case MYNSP_DEREGISTER_CLASS: // remove a service class
{
GUID classguid;
#ifdef DEBUG
printf("deregister service class\n");
#endif
bytesread = sizeof(classguid);
readdata(ns, (char *)&classguid, sizeof(classguid), &bytesread);
ret = DeregisterServiceClass(&classguid);
writedata(ns, (char *)&ret, sizeof(ret), &byteswritten);
closesocket(ns);
break;
}
case MYNSP_LOOKUP_CLASS: // Lookup a service class
{
WSASERVICECLASSINFO *sc2=NULL;
GUID classguid;
int buflen = BUFFER_SZ;
#ifdef DEBUG
printf("lookup service class\n");
#endif
bytesread = sizeof(classguid);
readdata(ns, (char *)&classguid, sizeof(classguid), &bytesread);
sc2 = LookupServiceClass(&classguid);
if (!sc2)
ret = MYNSP_ERROR;
else
ret = MYNSP_SUCCESS;
writedata(ns, (char *)&ret, sizeof(ret), &byteswritten);
if (!sc2)
{
MarshallServiceClassInfo(sc2, buf, &buflen);
writedata(ns, buf, buflen, &byteswritten);
}
closesocket(ns);
break;
}
case MYNSP_REGISTER_SERVICE: // Register a service
{
WSAQUERYSET *si=NULL,
*si2=NULL;
#ifdef DEBUG
printf("REGISTER SERVICE\n");
#endif
bytesread = sizeof(bytes2follow);
readdata(ns, (char *)&bytes2follow, sizeof(bytes2follow),
&bytesread);
readdata(ns, buf, BUFFER_SZ, &bytes2follow);
si = (WSAQUERYSET *)GlobalAlloc(GPTR, bytes2follow);
DeMarshallServiceInfo(si, buf);
//
// See if there's already a service registered with this
// name
//
si2 = LookupService(si->lpServiceClassId, si->lpszServiceInstanceName);
if (!si2)
{
#ifdef DEBUG
printf("Successfully registered on index: %d\n", dwNumServices);
#endif
Services[dwNumServices++] = si;
ret = MYNSP_SUCCESS;
}
else
{
#ifdef DEBUG
printf("Registration failed\n");
#endif
ret = MYNSP_ERROR;
}
writedata(ns, (char *)&ret, sizeof(ret), &byteswritten);
closesocket(ns);
break;
}
case MYNSP_DEREGISTER_SERVICE: // deregister a service
{
WSAQUERYSET *si=NULL,
*si2=NULL;
#ifdef DEBUG
printf("deregister service\n");
#endif
bytesread = sizeof(bytes2follow);
readdata(ns, (char *)&bytes2follow, sizeof(bytes2follow), &bytesread);
readdata(ns, buf, BUFFER_SZ, &bytes2follow);
si = (WSAQUERYSET *)GlobalAlloc(GPTR, bytes2follow);
DeMarshallServiceInfo(si, buf);
ret = DeregisterService(si->lpServiceClassId, si->lpszServiceInstanceName);
writedata(ns, (char *)&ret, sizeof(ret), &byteswritten);
closesocket(ns);
break;
}
case MYNSP_LOOKUP_SERVICE_BEGIN: // start a query
{
WSAQUERYSET *si=NULL;
HANDLE handle;
#ifdef DEBUG
printf("lookup service begin\n");
#endif
bytesread = sizeof(bytes2follow);
readdata(ns, (char *)&bytes2follow, sizeof(bytes2follow), &bytesread);
si = (WSAQUERYSET *)GlobalAlloc(GPTR, bytes2follow);
readdata(ns, buf, BUFFER_SZ, &bytes2follow);
DeMarshallServiceInfo(si, buf);
RegisterQuery(si, &handle);
writedata(ns, (char *)&handle, sizeof(handle), &byteswritten);
closesocket(ns);
break;
}
case MYNSP_LOOKUP_SERVICE_NEXT: // perform the query
{
HANDLE hQuery;
int nLeft,
iError;
bytesread = sizeof(hQuery);
readdata(ns, (char *)&hQuery, sizeof(hQuery), &bytesread);
#ifdef DEBUG
printf("lookup service next on HANDLE: %d\n", (int)hQuery);
#endif
//
// Perform the query, the iError code returned from
// PerformQuery indicates whether there are no more
// results or whether nothing was found.
//
ret = PerformQuery(hQuery, &iError);
if (ret == -1)
{
if (iError == WSA_E_NO_MORE)
nLeft = 0;
else if (iError == WSASERVICE_NOT_FOUND)
nLeft = -1;
}
else
{
MarshallServiceInfo(Services[ret], buf, &nLeft);
#ifdef DEBUG
PrintQuery(Services[ret]);
#endif
}
writedata(ns, (char *)&nLeft, sizeof(nLeft), &byteswritten);
if (nLeft > 0)
{
writedata(ns, (char *)buf, nLeft, &byteswritten);
}
closesocket(ns);
break;
}
case MYNSP_LOOKUP_SERVICE_END: // end a query
{
HANDLE hQuery;
bytesread = sizeof(hQuery);
readdata(ns, (char *)&hQuery, sizeof(hQuery), &bytesread);
#ifdef DEBUG
printf("lookup service next on HANDLE: %d\n", hQuery);
#endif
//
// Delete a query
//
ret = DeleteQuery(hQuery);
writedata(ns, (char *)&ret, sizeof(ret), &byteswritten);
closesocket(ns);
break;
}
}
}
closesocket(s);
WSACleanup();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -