int main (int argc, const char * argv[]) { ServerContext c = {NULL, NULL, NULL, NULL}; ServerRef server = ServerCreate(NULL, AcceptConnection, &c); if (server != NULL && ServerConnect(server, NULL, kServiceType, 0)) CFRunLoopRun(); return 0; }
// // 함수: InitInstance(HINSTANCE, int) // // 목적: 인스턴스 핸들을 저장하고 주 창을 만듭니다. // // 설명: // // 이 함수를 통해 인스턴스 핸들을 전역 변수에 저장하고 // 주 프로그램 창을 만든 다음 표시합니다. // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { ghInstance = hInstance; DWORD dwStyle = WS_OVERLAPPED | WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU | WS_BORDER; RECT rc = { 0, 0, FRAME_BUFFER_WIDTH, FRAME_BUFFER_HEIGHT }; AdjustWindowRect(&rc, dwStyle, FALSE); HWND hMainWnd = CreateWindow(szWindowClass, szTitle, dwStyle, CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, hInstance, NULL); if (!hMainWnd) return FALSE; gGameFramework.OnCreate(hInstance, hMainWnd); // Connect Server cout << "ip : "; cin >> ip; ServerConnect(); ::ShowWindow(hMainWnd, nCmdShow); ::UpdateWindow(hMainWnd); return TRUE; }
int ServerSendMessage( int *msgid, char *server, int op, int *retstatus, int *conid_out, void (*ast)(), void *astparam, void (*before_ast)(), int numargs_in, ...) { static unsigned int addr = 0; short port; int conid; int flags = 0; int status = ServerPATH_DOWN; int jobid; int i; if (StartReceiver(&port) && ((conid = ServerConnect(server)) >= 0)) { char cmd[4096]; unsigned char numargs = max(0,min(numargs_in,8)); unsigned char idx = 0; char dtype; short len; char ndims; int dims[8]; int numbytes; int *dptr; va_list vlist; void *mem=0; struct descrip *arg; if (conid_out) *conid_out = conid; if (addr == 0) { int sock=getSocket(conid); struct sockaddr_in addr_struct; unsigned int len=sizeof(addr_struct); if (getsockname(sock,(struct sockaddr *)&addr_struct,&len) == 0) addr = *(int *)&addr_struct.sin_addr; } if (addr) jobid = RegisterJob(msgid,retstatus,ast,astparam,before_ast,conid); if (before_ast) flags |= SrvJobBEFORE_NOTIFY; sprintf(cmd,"MdsServerShr->ServerQAction(%d,%dwu,%d,%d,%d",addr,port,op,flags,jobid); va_start(vlist,numargs_in); for (i=0;i<numargs;i++) { strcat(cmd,","); arg = va_arg(vlist,struct descrip *); if (op == SrvMonitor && numargs == 8 && i == 5 && arg->dtype == DTYPE_LONG && *(int *)arg->ptr == MonitorCheckin) MonJob = jobid; switch(arg->dtype) { case DTYPE_CSTRING: { int j; int k; char *c = (char *)arg->ptr; int len = strlen(c); strcat(cmd,"\""); for (j=0,k=strlen(cmd);j<len;j++,k++) { if (c[j] == '"' || c[j] == '\\') cmd[k++]='\\'; cmd[k]=c[j]; } cmd[k]=0; strcat(cmd,"\""); break; } case DTYPE_LONG: sprintf(&cmd[strlen(cmd)],"%d",*(int *)arg->ptr); break; case DTYPE_CHAR: sprintf(&cmd[strlen(cmd)],"%d",(int)*(char *)arg->ptr); break; default: printf("shouldn't get here! ServerSendMessage dtype = %d\n",arg->dtype); } } strcat(cmd,")"); SndArgChk(conid, idx++, DTYPE_CSTRING, 1, (short)strlen(cmd), 0, 0, cmd); status = GetAnswerInfoTS(conid, &dtype, &len, &ndims, dims, &numbytes, (void **)&dptr, &mem); if (mem) free(mem); if (!addr) { if (retstatus) *retstatus = status; if (ast) (*ast)(astparam,"Job Done"); } }
static AgentConnection *ServerConnection(const char *server, FileCopy fc, int *err) { AgentConnection *conn; *err = 0; #if !defined(__MINGW32__) signal(SIGPIPE, SIG_IGN); #endif /* !__MINGW32__ */ #if !defined(__MINGW32__) static sigset_t signal_mask; sigemptyset(&signal_mask); sigaddset(&signal_mask, SIGPIPE); pthread_sigmask(SIG_BLOCK, &signal_mask, NULL); #endif conn = NewAgentConn(server); if (strcmp(server, "localhost") == 0) { conn->authenticated = true; return conn; } conn->authenticated = false; conn->encryption_type = CfEnterpriseOptions(); /* username of the client - say root from Windows */ #ifdef __MINGW32__ snprintf(conn->username, CF_SMALLBUF, "root"); #else GetCurrentUserName(conn->username, CF_SMALLBUF); #endif /* !__MINGW32__ */ if (conn->sd == SOCKET_INVALID) { if (!ServerConnect(conn, server, fc)) { Log(LOG_LEVEL_INFO, "No server is responding on this port"); DisconnectServer(conn); *err = -1; return NULL; } if (conn->sd < 0) /* INVALID or OFFLINE socket */ { UnexpectedError("ServerConnect() succeeded but socket descriptor is %d!", conn->sd); *err = -1; return NULL; } if (!IdentifyAgent(conn->sd)) { Log(LOG_LEVEL_ERR, "Id-authentication for '%s' failed", VFQNAME); errno = EPERM; DisconnectServer(conn); *err = -2; // auth err return NULL; } if (!AuthenticateAgent(conn, fc.trustkey)) { Log(LOG_LEVEL_ERR, "Authentication dialogue with '%s' failed", server); errno = EPERM; DisconnectServer(conn); *err = -2; // auth err return NULL; } conn->authenticated = true; return conn; } return conn; }
void CNullModem::handleUpperEvent(Bit16u type) { switch(type) { case SERIAL_POLLING_EVENT: { // periodically check if new data arrived, disconnect // if required. Add it back. setEvent(SERIAL_POLLING_EVENT, 1.0f); // update Modem input line states updateMSR(); switch(rx_state) { case N_RX_IDLE: if (CanReceiveByte()) { if (doReceive()) { // a byte was received rx_state=N_RX_WAIT; setEvent(SERIAL_RX_EVENT, bytetime*0.9f); } // else still idle } else { #if SERIAL_DEBUG log_ser(dbg_aux,"Nullmodem: block on polling."); #endif rx_state=N_RX_BLOCKED; // have both delays (1ms + bytetime) setEvent(SERIAL_RX_EVENT, bytetime*0.9f); } break; case N_RX_BLOCKED: // one timeout tick if (!CanReceiveByte()) { rx_retry++; if (rx_retry>=rx_retry_max) { // it has timed out: rx_retry=0; removeEvent(SERIAL_RX_EVENT); if (doReceive()) { // read away everything while(doReceive()); rx_state=N_RX_WAIT; setEvent(SERIAL_RX_EVENT, bytetime*0.9f); } else { // much trouble about nothing rx_state=N_RX_IDLE; #if SERIAL_DEBUG log_ser(dbg_aux,"Nullmodem: unblock due to no more data",rx_retry); #endif } } // else wait further } else { // good: we can receive again removeEvent(SERIAL_RX_EVENT); rx_retry=0; if (doReceive()) { rx_state=N_RX_FASTWAIT; setEvent(SERIAL_RX_EVENT, bytetime*0.65f); } else { // much trouble about nothing rx_state=N_RX_IDLE; } } break; case N_RX_WAIT: case N_RX_FASTWAIT: break; } break; } case SERIAL_RX_EVENT: { switch(rx_state) { case N_RX_IDLE: LOG_MSG("internal error in nullmodem"); break; case N_RX_BLOCKED: // try to receive case N_RX_WAIT: case N_RX_FASTWAIT: if (CanReceiveByte()) { // just works or unblocked if (doReceive()) { rx_retry=0; // not waiting anymore if (rx_state==N_RX_WAIT) setEvent(SERIAL_RX_EVENT, bytetime*0.9f); else { // maybe unblocked rx_state=N_RX_FASTWAIT; setEvent(SERIAL_RX_EVENT, bytetime*0.65f); } } else { // didn't receive anything rx_retry=0; rx_state=N_RX_IDLE; } } else { // blocking now or still blocked #if SERIAL_DEBUG if (rx_state==N_RX_BLOCKED) log_ser(dbg_aux,"Nullmodem: rx still blocked (retry=%d)",rx_retry); else log_ser(dbg_aux,"Nullmodem: block on continued rx (retry=%d).",rx_retry); #endif setEvent(SERIAL_RX_EVENT, bytetime*0.65f); rx_state=N_RX_BLOCKED; } break; } break; } case SERIAL_TX_EVENT: { // Maybe echo cirquit works a bit better this way if (rx_state==N_RX_IDLE && CanReceiveByte() && clientsocket) { if (doReceive()) { // a byte was received rx_state=N_RX_WAIT; setEvent(SERIAL_RX_EVENT, bytetime*0.9f); } } ByteTransmitted(); break; } case SERIAL_THR_EVENT: { ByteTransmitting(); // actually send it setEvent(SERIAL_TX_EVENT,bytetime+0.01f); break; } case SERIAL_SERVER_POLLING_EVENT: { // As long as nothing is connected to our server poll the // connection. if (!ServerConnect()) { // continue looking setEvent(SERIAL_SERVER_POLLING_EVENT, 50); } break; } case SERIAL_TX_REDUCTION: { // Flush the data in the transmitting buffer. if (clientsocket) clientsocket->FlushBuffer(); tx_block=false; break; } case SERIAL_NULLMODEM_DTR_EVENT: { if ((!DTR_delta) && getDTR()) { // DTR went positive. Try to connect. if (ClientConnect(new TCPClientSocket((char*)hostnamebuffer, (Bit16u)clientport))) break; // no more DTR wait event when connected } DTR_delta = getDTR(); setEvent(SERIAL_NULLMODEM_DTR_EVENT,50); break; } } }
int main(int argc, char **argv) { int i; int timeout = 10; int si = 1; int debug = 0; THREAD tid; char *pwd, *sta = NULL; char RunFile[MAXPATHLEN+1], *log = (char *) NULL; static char LogFile[MAXPATHLEN+1]; static char *default_server = "localhost"; ISP_SERVER ispd; time_t idle = 600; server = default_server; for (i = 1; i < argc; i++) { if (strncasecmp(argv[i], "home=", strlen("home=")) == 0) { home = argv[i] + strlen("home="); } else if (strncasecmp(argv[i], "sta=", strlen("sta=")) == 0) { sta = argv[i] + strlen("sta="); } else if (strncasecmp(argv[i], "debug=", strlen("debug=")) == 0) { debug = atoi(argv[i] + strlen("debug=")); } else if (strncasecmp(argv[i], "idle=", strlen("idle=")) == 0) { idle = atoi(argv[i] + strlen("idle=")); } else if (strcasecmp(argv[i], "-h") == 0) { help(argv[0]); } else if (strcasecmp(argv[i], "help") == 0) { help(argv[0]); } else if (sta == (char *) NULL) { sta = argv[i]; } else { help(argv[0]); } } SetMaxIdle(idle); if ((pwd = isp_setup(&home, &sta)) == (char *) NULL) exit(1); if ((Syscode = strdup(sta)) == NULL) { perror(argv[0]); exit(1); } util_lcase(Syscode); sprintf(RunFile, "%s/%s", pwd, ISP_RUN_FILE); if (!ispLoadRunParam(RunFile, sta, NULL, &ispd)) { fprintf(stderr, "%s: problems with parameter file\n", argv[0]); exit(1); } if (debug) util_logopen("syslogd", 1, 9, debug, "CONSOLE", argv[0]); console = (getppid() == 1); if (console) util_log(1, "Running as BOOT CONSOLE"); /* Start signal handling thread */ InitSignals(); /* Allocate space for channel map */ InitChanMap(); /* Initialize the display */ sprintf(LogFile, "%s/log/nrtslog", home); if (OpenDisplay(server, ispd.port, LogFile) != 0) { fprintf(stderr, "%s: ABORT: Can't open display\n", argv[0]); sleep(1); Quit(1); } /* Connect to the server */ ServerConnect(server, ispd.port, ispd.to, TRUE); /* Start status request thread */ util_log(1, "Start routine status requests"); if (!THREAD_CREATE(&tid, StatusRequest, NULL)) { EndWin(); fprintf(stderr, "failed to create StatusRequest thread\n"); exit(1); } /* Start interactive thread */ util_log(1, "Start interactive display"); FirstDisplay(); while (1) { switch (Digitizer()) { case ISP_DAS: util_log(1, "ISP_DAS detected, start DasDisplay"); if (!THREAD_CREATE(&tid, DasDisplay, NULL)) { EndWin(); fprintf(stderr, "failed to create DasDisplay thread\n"); exit(1); } THREAD_EXIT(0); case ISP_SAN: util_log(1, "ISP_San detected, start SanDisplay"); if (!THREAD_CREATE(&tid, SanDisplay, NULL)) { EndWin(); fprintf(stderr, "failed to create SanDisplay thread\n"); exit(1); } THREAD_EXIT(0); default: sleep(1); } } }