📄 servconn.cpp
字号:
/* * ServConn.cpp - part of the jEdit Launcher package * Copyright (C) 2001 John Gellene * jgellene@nyc.rr.com * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or any later version. * * Notwithstanding the terms of the General Public License, the author grants * permission to compile and link object code generated by the compilation of * this program with object code and libraries that are not subject to the * GNU General Public License, provided that the executable output of such * compilation shall be distributed with source code on substantially the * same basis as the jEditLauncher package of which this program is a part. * By way of example, a distribution would satisfy this condition if it * included a working makefile for any freely available make utility that * runs on the Windows family of operating systems. This condition does not * require a licensee of this software to distribute any proprietary software * (including header files and libraries) that is licensed under terms * prohibiting redistribution to third parties. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * $Id: ServConn.cpp,v 1.3 2001/09/07 15:25:58 jgellene Exp $ */#include "stdafx.h"#include "ServConn.h"#include <winsock2.h> // winsock functionsServerConnection::ServerConnection(LPCTSTR lpszServerPath) : port(0), key(0L), hSocket(0), connected(false), pServerPath(lpszServerPath) {}ServerConnection::~ServerConnection(){ if(connected) Disconnect();}unsigned short ServerConnection::GetPort() const{ return port;}unsigned long ServerConnection::GetKey() const{ return key;}bool ServerConnection::IsConnected() const{ return connected;}HRESULT ServerConnection::FindServer(){ UINT nErrorMsg = 0; CHAR buffer[255]; ZeroMemory(buffer, 255); HANDLE h = ::CreateFile(pServerPath, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); //FLAG_SEQUENTIAL_SCAN, 0); OutputDebugString(h != INVALID_HANDLE_VALUE ? "File opened." : "File not opened"); if(h == INVALID_HANDLE_VALUE) { LPSTR szErrMsg = 0; ::FormatMessageA( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR) &szErrMsg, 0, NULL ); OutputDebugString(szErrMsg); LocalFree((LPVOID)szErrMsg); } DWORD dwBytesRead; HRESULT hr = S_OK; if( h != INVALID_HANDLE_VALUE && ::ReadFile(h, (LPVOID)buffer, 255, &dwBytesRead, 0)) { // replaces sscanf int nFieldsRead = 0; port = (unsigned short)atoi(buffer); if(port != 0) ++nFieldsRead; char *s = buffer; while(*s != 0 && *s != '\n') ++s; if(s - buffer < (long)dwBytesRead) ++s; key = (unsigned long)atol(s); if(key != 0) ++nFieldsRead; if(nFieldsRead != 2) { hr = E_FAIL; switch(nFieldsRead) { case 0: MakeErrorInfo("Port number in server file is invalid."); break; case 1: MakeErrorInfo("Key number in server file is invalid."); break; default: MakeErrorInfo("Server file could not be read."); } } } else { hr = S_FALSE; MakeErrorInfo("Could not find server file."); } if(h != INVALID_HANDLE_VALUE) ::CloseHandle(h); return hr;}HRESULT ServerConnection::Connect(){ if(connected) return S_OK; if(port == 0 && S_OK != FindServer()) { return E_FAIL; } WSADATA wsa_data; if (WSAStartup(MAKEWORD(1, 0), &wsa_data) != 0) { MakeErrorInfo("Could not start Windows socket service."); return E_FAIL; } hSocket = socket(PF_INET, SOCK_STREAM, 0); if(hSocket == INVALID_SOCKET) { MakeErrorInfo("Invalid Window socket."); hSocket = 0; WSACleanup(); return E_FAIL; } struct sockaddr_in address; address.sin_family = AF_INET; address.sin_addr.s_addr = htonl(0x7f000001); address.sin_port = htons(port); CHAR buf[32]; ultoa((ULONG)key, buf, 10); strcat(buf, "\n"); int len = strlen(buf); if (connect(hSocket, (struct sockaddr*)&address, sizeof(address)) == SOCKET_ERROR || len != send(hSocket, buf, len, 0)) { if(WSAGetLastError() == WSAECONNREFUSED) { MakeErrorInfo("jEdit edit server refused connection."); } else { MakeErrorInfo("Bad connection with jEdit edit server."); } return E_FAIL; } else connected = true; return S_OK;}HRESULT ServerConnection::Disconnect(){ if(hSocket != 0) { closesocket(hSocket); hSocket = 0; } connected = false; WSACleanup(); return S_OK;}HRESULT ServerConnection::Clear(){ port = 0; key = 0; return Disconnect();}HRESULT ServerConnection::SendData(char* pData, long nLength){ if(!connected) return S_FALSE; long nSent = send(hSocket, pData, nLength, 0); return nSent == nLength ? S_OK : E_FAIL;}void ServerConnection::MakeErrorInfo(CHAR* pszErrorMsg){ if(pszErrorMsg == 0) return; int len = strlen(pszErrorMsg); WCHAR pwszErrorMsg[256]; ZeroMemory(pwszErrorMsg, sizeof(WCHAR) * 256); MultiByteToWideChar(CP_ACP, 0, pszErrorMsg, len, pwszErrorMsg, len); ICreateErrorInfo *piCreateErr = 0; HRESULT hr = ::CreateErrorInfo( &piCreateErr); if(FAILED(hr)) return; //piCreateErr->SetHelpFile(...); //piCreateErr->SetHelpContext(...); piCreateErr->SetSource(L"JEdit.JEditLauncher"); piCreateErr->SetDescription(pwszErrorMsg); IErrorInfo *piError = 0; hr = piCreateErr->QueryInterface(IID_IErrorInfo, (void**)&piError); if(SUCCEEDED(hr)) { ::SetErrorInfo(0L, piError); piError->Release(); } piCreateErr->Release();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -