/// <summary> /// 데이터 수신 처리 콜백 함수 /// </summary> /// <param name="ar"></param> private void OnReceiveCallBack(IAsyncResult ar) { try { StateObject so = (StateObject)ar.AsyncState; int bytesRead = so.Worker.EndReceive(ar); AsyncSocketReceiveEventArgs rev = new AsyncSocketReceiveEventArgs(this.id, bytesRead, so.Buffer); // 데이터 수신 이벤트를 처리한다. if (bytesRead > 0) { Received(rev); } // 다음 읽을 데이터를 처리한다. Receive(); } catch (System.Exception e) { AsyncSocketErrorEventArgs eev = new AsyncSocketErrorEventArgs(this.id, e); ErrorOccured(eev); } }
/// <summary> /// 연결 요청 처리 콜백 함수 /// </summary> /// <param name="ar"></param> private void OnConnectCallback(IAsyncResult ar) { try { Socket client = (Socket)ar.AsyncState; // 보류 중인 연결을 완성한다. client.EndConnect(ar); conn = client; // 연결에 성공하였다면, 데이터 수신을 대기한다. Receive(); // 연결 성공 이벤트를 날린다. AsyncSocketConnectionEventArgs cev = new AsyncSocketConnectionEventArgs(this.id); Connected(cev); } catch (System.Exception e) { AsyncSocketErrorEventArgs eev = new AsyncSocketErrorEventArgs(this.id, e); ErrorOccured(eev); } }
protected virtual void ErrorOccured(AsyncSocketErrorEventArgs e) { AsyncSocketErrorEventHandler handler = OnError; if (handler != null) { handler(this, e); } }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected virtual void OnErrorOccurred(object sender, AsyncSocketErrorEventArgs e) { // Copy a reference to the delegate field now into a temporary field for thread safety EventHandler <AsyncSocketErrorEventArgs> temp = Interlocked.CompareExchange(ref ErrorOccurred, null, null); if (temp != null) { temp(sender, e); } }
/// <summary> /// Client의 접속을 비동기적으로 대기한다. /// </summary> /// <returns></returns> private void StartAccept() { try { listener.BeginAccept(new AsyncCallback(OnListenCallBack), listener); } catch (System.Exception e) { AsyncSocketErrorEventArgs eev = new AsyncSocketErrorEventArgs(this.id, e); ErrorOccured(eev); } }
/// <summary> /// 데이터 수신을 비동기적으로 처리 /// </summary> public void Receive() { try { StateObject so = new StateObject(conn); so.Worker.BeginReceive(so.Buffer, 0, so.BufferSize, 0, new AsyncCallback(OnReceiveCallBack), so); } catch (System.Exception e) { AsyncSocketErrorEventArgs eev = new AsyncSocketErrorEventArgs(this.id, e); ErrorOccured(eev); } }
/// <summary> /// 소켓 연결을 비동기적으로 종료 /// </summary> public void Close() { try { Socket client = conn; client.Shutdown(SocketShutdown.Both); client.BeginDisconnect(false, new AsyncCallback(OnCloseCallBack), client); } catch (System.Exception e) { AsyncSocketErrorEventArgs eev = new AsyncSocketErrorEventArgs(this.id, e); ErrorOccured(eev); } }
public void Listen() { try { listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); listener.Bind(new IPEndPoint(IPAddress.Any, this.port)); listener.Listen(backLog); StartAccept(); } catch (System.Exception e) { AsyncSocketErrorEventArgs eev = new AsyncSocketErrorEventArgs(this.id, e); ErrorOccured(eev); } }
public void Stop() { try { if (listener != null) { if (listener.IsBound) { listener.Close(100); } } } catch (System.Exception e) { AsyncSocketErrorEventArgs eev = new AsyncSocketErrorEventArgs(this.id, e); ErrorOccured(eev); } }
/// <summary> /// 데이터 송신 처리 콜백 함수 /// </summary> /// <param name="ar"></param> private void OnSendCallBack(IAsyncResult ar) { try { Socket client = (Socket)ar.AsyncState; int bytesWritten = client.EndSend(ar); AsyncSocketSendEventArgs sev = new AsyncSocketSendEventArgs(this.id, bytesWritten); Sent(sev); } catch (System.Exception e) { AsyncSocketErrorEventArgs eev = new AsyncSocketErrorEventArgs(this.id, e); ErrorOccured(eev); } }
/// <summary> /// 데이터 송신을 비동기적으로 처리 /// </summary> /// <param name="buffer"></param> /// <returns></returns> public bool Send(byte[] buffer) { try { Socket client = conn; client.BeginSend(buffer, 0, buffer.Length, 0, new AsyncCallback(OnSendCallBack), client); } catch (System.Exception e) { AsyncSocketErrorEventArgs eev = new AsyncSocketErrorEventArgs(this.id, e); ErrorOccured(eev); return(false); } return(true); }
/// <summary> /// 소켓 연결 종료를 처리하는 콜백 함수 /// </summary> /// <param name="ar"></param> private void OnCloseCallBack(IAsyncResult ar) { try { Socket client = (Socket)ar.AsyncState; client.EndDisconnect(ar); client.Close(); AsyncSocketConnectionEventArgs cev = new AsyncSocketConnectionEventArgs(this.id); Closed(cev); } catch (System.Exception e) { AsyncSocketErrorEventArgs eev = new AsyncSocketErrorEventArgs(this.id, e); ErrorOccured(eev); } }
/// <summary> /// 연결을 시도한다. /// </summary> /// <param name="hostAddress"></param> /// <param name="port"></param> /// <returns></returns> public bool Connect(string hostAddress, int port) { try { IPAddress[] ips = Dns.GetHostAddresses(hostAddress); IPEndPoint remoteEP = new IPEndPoint(ips[0], port); Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); client.BeginConnect(remoteEP, new AsyncCallback(OnConnectCallback), client); } catch (System.Exception e) { AsyncSocketErrorEventArgs eev = new AsyncSocketErrorEventArgs(this.id, e); ErrorOccured(eev); return(false); } return(true); }
/// <summary> /// Client의 비동기 접속을 처리한다. /// </summary> /// <param name="ar"></param> private void OnListenCallBack(IAsyncResult ar) { try { Socket listener = (Socket)ar.AsyncState; Socket worker = listener.EndAccept(ar); // Client를 Accept 했다고 Event를 발생시킨다. AsyncSocketAcceptEventArgs aev = new AsyncSocketAcceptEventArgs(worker); Accepted(aev); // 다시 새로운 클라이언트의 접속을 기다린다. StartAccept(); } catch (System.Exception e) { AsyncSocketErrorEventArgs eev = new AsyncSocketErrorEventArgs(this.id, e); ErrorOccured(eev); } }