# 4. 콜백함수 사용

### 4.1 OnEventReceived()

EZGEM은 통신 상태 변화나 발생하는 이벤트를 사용자에게 전달합니다. 사용자는 연결된 이벤트 핸들러(OnEventReceived) 를 통해 전달된 이벤트 별로 분기 처리를 수행하여 구현할 수 있습니다. EZGEM이벤트 번호에 맞는 설명은 다음 페이지에 상세히 정의되어있습니다.

```csharp
 private void OnEventReceived(IntPtr lpParam, short nEventId, int lParam)                
 { 
     switch (nEventId)
     {                                                    
         case EZGEM_EVENT_CONNECTED:                      // tcp 연결시
             OnConnected(lParam);                         //사용자가 함수 선언 후 처리
             break;                                         
         case EZGEM_EVENT_DISCONNECTED:                   // tcp 연결해제시           
             OnDisconnected();                            //사용자가 함수 선언 후 처리
             break;
         case EZGEM_EVENT_KEY_EVALUATION_MODE_START:
             AddLog("KEY_EVALUATION_MODE_START");         //예)로그 작성
             break;
         case EZGEM_EVENT_HOST_COMMAND:
             OnRemoteCommand(lParam);                   //S2F41 RemoteCommand처리
             break;
```

### 4.2 OnMsgReceived()

DisableAutoReply() 함수를 사용하여 GEM에서 받은 메시지를 Stream,Function 별로 처리합니다.

{% hint style="warning" %}
주의 : DisableAutoReply() 를 사용하면 라이브러리가 응답 메시지를 자동으로 보내지 않습니다.
{% endhint %}

```csharp
private void OnMsgReceived(IntPtr lpParam, int lMsgId)
{
    short nStream = 0, nFunction = 0, nWbit = 0;
    int nLength = 0;

    m_gem.GetMsgInfo(lMsgId, ref nStream, ref nFunction, ref nWbit, ref nLength);
    
    if (nStream == 1 && nFunction == 3)
    {
        OnS1F3W(lMsgId);
    }
    else if (nStream == 2 && nFunction == 17)            //S2F17
    {
        OnS2F17W(lMsgId);
    }
    else if (nStream == 2 && nFunction == 25)            //S2F25
    {
        OnS2F25W(lMsgId);
    }
}   
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://nviasoft.gitbook.io/nviasoft-docs/documentation/c/4..md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
