public void Start(string serverIP, int port, int backlog, int opaque, TCPObjectErrorHandle errorCallback, ReadCompleteHandle readCallback, AcceptHandle acceptCallback) { // we should init TCPSynchronizeContext in the TCP thread first TCPSynchronizeContext.GetInstance(); IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse(serverIP), port); m_listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); m_listener.Bind(ipEndPoint); m_listener.Listen(backlog); m_opaque = opaque; m_acceptEvent.Completed += IO_Complete; m_onErrorHandle = errorCallback; m_onReadCompleteHandle = readCallback; m_onAcceptHandle = acceptCallback; m_bindIP = serverIP; m_bindPort = port; BeginAccept(); }
private void IO_Complete(object sender, object o) { SocketAsyncEventArgs args = o as SocketAsyncEventArgs; switch (args.LastOperation) { case SocketAsyncOperation.Connect: { TCPSynchronizeContext.GetInstance().Post(OnConnectComplete, o); } break; case SocketAsyncOperation.Disconnect: { TCPSynchronizeContext.GetInstance().Post(OnDisconnectComplete, o); } break; case SocketAsyncOperation.Receive: { TCPSynchronizeContext.GetInstance().Post(OnRecvComplete, o); } break; case SocketAsyncOperation.Send: { TCPSynchronizeContext.GetInstance().Post(OnWriteComplete, o); } break; default: { throw new Exception("socket error: " + args.LastOperation); }; } }
// this function must call in tcp thread first public static TCPSynchronizeContext GetInstance() { if (m_instance == null) { m_instance = new TCPSynchronizeContext(); } return(m_instance); }
private void IO_Complete(object sender, object o) { SocketAsyncEventArgs asyncEventArgs = o as SocketAsyncEventArgs; if (asyncEventArgs.LastOperation == SocketAsyncOperation.Accept) { TCPSynchronizeContext.GetInstance().Post(OnAccpetComplete, asyncEventArgs); } }
public void Start(int opaque, TCPObjectErrorHandle errorCallback, ReadCompleteHandle readCallback, ConnectCompleteHandle connectCallback) { TCPSynchronizeContext.GetInstance(); m_onErrorHandle = errorCallback; m_onReadCompleteHandle = readCallback; m_onConnectCompleteHandle = connectCallback; m_opaque = opaque; }
public void Loop() { TCPSynchronizeContext.GetInstance().Loop(); }