void CNet::DealAcceptEvent(struct iocp_event * pEvent) { ASSERT(m_szCallAddress[CALL_REMOTE_CONNECTED]); if (m_szCallAddress[CALL_REMOTE_CONNECTED] != NULL) { if (ERROR_SUCCESS == pEvent->nerron) { s32 nConnectID = m_ConnectPool.CreateID(); m_ConnectPool[nConnectID]->s = pEvent->s; SafeSprintf(m_ConnectPool[nConnectID]->szIP, sizeof(m_ConnectPool[nConnectID]->szIP), "%s", inet_ntoa(pEvent->remote.sin_addr)); m_ConnectPool[nConnectID]->nPort = ntohs(pEvent->remote.sin_port); m_szCallAddress[CALL_REMOTE_CONNECTED](nConnectID, pEvent->p, 0); pEvent->event = EVENT_ASYNC_RECV; pEvent->wbuf.buf = pEvent->buff; pEvent->wbuf.len = sizeof(pEvent->buff); m_ConnectPool[nConnectID]->pContext = pEvent->p; pEvent->p = m_ConnectPool[nConnectID]; s32 err; if (ERROR_NO_ERROR != async_recv(pEvent, &err, pEvent->p)) { NET_ERROR("async_recv error %d code %d", err, pEvent->nerron); SafeShutdwon(pEvent, CSD_BOTH); return; } } else { safe_close(pEvent); } } }
THREAD_FUN CNet::NetLoop(LPVOID p) { CNet * pThis = (CNet *)p; if (NULL == pThis) { ASSERT(false); return -1; } s32 code = 0; s32 err = 0; struct iocp_event * pEvent; while (true) { if (pThis->m_stop) { CAutoLock(&(pThis->m_freelock)); pThis->m_nThreadCount--; return 0; } pEvent = NULL; code = get_event(&pEvent, &err); if (ERROR_NO_ERROR == code && pEvent != NULL) { NET_TRACE("event %d, code %d, last error %d, io bytes %d", pEvent->event, code, err, pEvent->ioBytes); pThis->m_queue.add(pEvent); } else { if (pEvent != NULL) { NET_ERROR("event %d, code %d, error %d", pEvent->event, code, err); } CSleep(10); } } }
void CNet::CSetCallBackAddress(const CALLBACK_TYPE eType, const CALL_FUN address) { if (eType >= CALL_TYPE_COUNT) { NET_ERROR("unknown call type"); ASSERT(false); } else { m_szCallAddress[eType] = address; } }
bool CNet::CConnectEx(const char * pStrIP, const s32 nPort, const void * pContext) { s32 err; if (async_connect(pStrIP, nPort, &err, (char *)pContext) != ERROR_NO_ERROR) { NET_ERROR("async_connect error, last code : %d", err); return false; } NET_TRACE("async_connect ok, ip:%s, port:%d, context:%ld", pStrIP, nPort, pContext); return true; }
/* * Class: sun_net_ExtendedOptionsImpl * Method: setFlowOption * Signature: (Ljava/io/FileDescriptor;Ljdk/net/SocketFlow;)V */ JNIEXPORT void JNICALL Java_sun_net_ExtendedOptionsImpl_setFlowOption (JNIEnv *env, jclass UNUSED, jobject fileDesc, jobject flow) { int fd = getFD(env, fileDesc); if (fd < 0) { NET_ERROR(env, JNU_JAVANETPKG "SocketException", "socket closed"); return; } else { sock_flow_props_t props; jlong bandwidth; int rv; jint priority = (*env)->GetIntField(env, flow, sf_priority); memset(&props, 0, sizeof(props)); props.sfp_version = SOCK_FLOW_PROP_VERSION1; if (priority != jdk_net_SocketFlow_UNSET) { props.sfp_mask |= SFP_PRIORITY; props.sfp_priority = priority; } bandwidth = (*env)->GetLongField(env, flow, sf_bandwidth); if (bandwidth > -1) { props.sfp_mask |= SFP_MAXBW; props.sfp_maxbw = (uint64_t) bandwidth; } rv = setsockopt(fd, SOL_SOCKET, SO_FLOW_SLA, &props, sizeof(props)); if (rv < 0) { if (errno == ENOPROTOOPT) { JNU_ThrowByName(env, "java/lang/UnsupportedOperationException", "unsupported socket option"); } else if (errno == EACCES || errno == EPERM) { NET_ERROR(env, JNU_JAVANETPKG "SocketException", "Permission denied"); } else { NET_ERROR(env, JNU_JAVANETPKG "SocketException", "set option SO_FLOW_SLA failed"); } return; } setStatus(env, flow, props.sfp_status); } }
/* * Class: sun_net_ExtendedOptionsImpl * Method: getFlowOption * Signature: (Ljava/io/FileDescriptor;Ljdk/net/SocketFlow;)V */ JNIEXPORT void JNICALL Java_sun_net_ExtendedOptionsImpl_getFlowOption (JNIEnv *env, jclass UNUSED, jobject fileDesc, jobject flow) { int fd = getFD(env, fileDesc); if (fd < 0) { NET_ERROR(env, JNU_JAVANETPKG "SocketException", "socket closed"); return; } else { sock_flow_props_t props; int status; socklen_t sz = sizeof(props); int rv = getsockopt(fd, SOL_SOCKET, SO_FLOW_SLA, &props, &sz); if (rv < 0) { if (errno == ENOPROTOOPT) { JNU_ThrowByName(env, "java/lang/UnsupportedOperationException", "unsupported socket option"); } else if (errno == EACCES || errno == EPERM) { NET_ERROR(env, JNU_JAVANETPKG "SocketException", "Permission denied"); } else { NET_ERROR(env, JNU_JAVANETPKG "SocketException", "set option SO_FLOW_SLA failed"); } return; } /* first check status to see if flow exists */ status = props.sfp_status; setStatus(env, flow, status); if (status == 0) { /* OK */ /* can set the other fields now */ if (props.sfp_mask & SFP_PRIORITY) { (*env)->SetIntField(env, flow, sf_priority, props.sfp_priority); } if (props.sfp_mask & SFP_MAXBW) { (*env)->SetLongField(env, flow, sf_bandwidth, (jlong)props.sfp_maxbw); } } } }
bool CNet::CListen(const char * pStrIP, const s32 nPort, const void * pContext, const s32 nBacklog) { s32 err; s32 code = async_listen(pStrIP, nPort, &err, (char *)pContext, nBacklog); if (code != ERROR_NO_ERROR) { ASSERT(false); NET_ERROR("async_listen error, last code : %d", err); return false; } NET_TRACE("async_listen ok, ip:%s, port:%d, context:%ld, nbocklog %d", pStrIP, nPort, pContext, nBacklog); return true; }
void CNet::CLoop(bool demon, s32 nFramems) { if (demon) { m_demo = true; HANDLE hThread = ::CreateThread(NULL, 0, CNet::DemonLoop, (LPVOID)this, 0, NULL); CloseHandle(hThread); } else { if (m_demo) { NET_ERROR("has already looped as demon"); return; } m_nFrameMs = nFramems; DemonLoop(this); } }
bool CNet::CNetInitialize(const s8 nThreadCount, const s64 lWaitMs) { LogModuleInit(); m_demo = false; m_stop = false; m_nThreadCount = nThreadCount; m_lWaitMs = lWaitMs; NET_TRACE("网络引擎初始化,线程数 %d, 单帧最大时间 %d", nThreadCount, lWaitMs); s32 err; s32 code = iocp_init(lWaitMs, &err); if (code != ERROR_NO_ERROR) { NET_ERROR("init iocp error, last code : %d", err); ASSERT(false); } else { NET_TRACE("init iocp success"); for (s32 i=0; i<nThreadCount; i++) { HANDLE hThread = ::CreateThread(NULL, 0, CNet::NetLoop, (LPVOID)this, 0, NULL); CloseHandle(hThread); } } return true; }
krui_err SnnsCLib::LEARN_MonteCarlo(int start_pattern, int end_pattern, float *parameterInArray, int NoOfInParams, float **parameterOutArray, int *NoOfOutParams) { //static float LEARN_MonteCarlo_OutParameter[1]; /* LEARN_MonteCarlo_OutParameter[0] stores the learning error */ int ret_code, pattern_no, sub_pat_no; float error; register FlagWord flags; register struct Link *link_ptr; register struct Unit *unit_ptr; register struct Site *site_ptr; if (NoOfInParams < 2) return (KRERR_PARAMETERS); /* Not enough input parameters */ *NoOfOutParams = 1; /* One return value is available (the * learning error) */ *parameterOutArray = LEARN_MonteCarlo_OutParameter; /* set the output parameter reference */ ret_code = KRERR_NO_ERROR; /* reset return code */ if (NetModified) { /* Net has been modified */ /* count the no. of I/O units and check the patterns */ ret_code = kr_IOCheck(); if (ret_code < KRERR_NO_ERROR) return (ret_code); /* sort units by topology and by topologic type */ ret_code = kr_topoSort(TOPOLOGICAL_FF); if ((ret_code != KRERR_NO_ERROR) && (ret_code != KRERR_DEAD_UNITS)) return (ret_code); MinimumError = 10000000; NetModified = FALSE; } if (NetInitialize || LearnFuncHasChanged) { /* Net has been modified */ MinimumError = 10000000; } /* randomize weigths and bias */ FOR_ALL_UNITS(unit_ptr) { unit_ptr->bias = (FlintType) u_drand48() * (LEARN_PARAM2(parameterInArray) - LEARN_PARAM1(parameterInArray)) + LEARN_PARAM1(parameterInArray); flags = unit_ptr->flags; if ((flags & UFLAG_IN_USE) == UFLAG_IN_USE) { /* unit is in use */ unit_ptr->value_a = (FlintType) 0; if (flags & UFLAG_SITES) { /* unit has sites */ FOR_ALL_SITES_AND_LINKS(unit_ptr, site_ptr, link_ptr) link_ptr->weight = (FlintType) u_drand48() * (LEARN_PARAM2(parameterInArray) - LEARN_PARAM1(parameterInArray)) + LEARN_PARAM1(parameterInArray); } else { /* unit has no sites */ if (flags & UFLAG_DLINKS) { /* unit has direct links */ FOR_ALL_LINKS(unit_ptr, link_ptr) link_ptr->weight = (FlintType) u_drand48() * (LEARN_PARAM2(parameterInArray) - LEARN_PARAM1(parameterInArray)) + LEARN_PARAM1(parameterInArray); } } } } /* compute the necessary sub patterns */ KernelErrorCode = kr_initSubPatternOrder(start_pattern, end_pattern); if (KernelErrorCode != KRERR_NO_ERROR) return (KernelErrorCode); NET_ERROR(LEARN_MonteCarlo_OutParameter) = 0.0; /* reset network error value */ /* calculate performance of new net */ while (kr_getSubPatternByOrder(&pattern_no, &sub_pat_no)) { propagateNetForward(pattern_no, sub_pat_no); /* Forward propagation */ if ((error = calculate_SS_error(pattern_no, sub_pat_no)) == -1) return (-1); NET_ERROR(LEARN_MonteCarlo_OutParameter) += error; } /* store weights and bias if error decreased */ if (NET_ERROR(LEARN_MonteCarlo_OutParameter) < MinimumError) { MinimumError = NET_ERROR(LEARN_MonteCarlo_OutParameter); FOR_ALL_UNITS(unit_ptr) { flags = unit_ptr->flags; unit_ptr->value_b = unit_ptr->bias; if ((flags & UFLAG_IN_USE) == UFLAG_IN_USE) { /* unit is in use */ if (flags & UFLAG_SITES) { /* unit has sites */ FOR_ALL_SITES_AND_LINKS(unit_ptr, site_ptr, link_ptr) link_ptr->value_b = link_ptr->weight; } else { /* unit has no sites */ if (flags & UFLAG_DLINKS) { /* unit has direct links */ FOR_ALL_LINKS(unit_ptr, link_ptr) link_ptr->value_b = link_ptr->weight; } } } } }