public MessageReceiver(String ipAddress, int portNo, BaseReceiveHandler receiveHandler) { try { // 1. ACCEPTOR 생성 this.acceptor = new AsyncSocketAcceptor(); // 2. DEFINE FILTER // 2.1. Logger filter this.acceptor.FilterChain.AddLast("logger", new LoggingFilter()); // 2.2. Codec filter ObjectSerializationCodecFactory objectSerializationCodecFactory = new ObjectSerializationCodecFactory(); objectSerializationCodecFactory.DecoderMaxObjectSize = CommConst.MAX_TRANSDATA_SIZE;; this.acceptor.FilterChain.AddLast("codec", new ProtocolCodecFilter(objectSerializationCodecFactory)); // 3. SET HANDLER this.acceptor.Handler = receiveHandler; // 4. BIND this.acceptor.Bind(new IPEndPoint(IPAddress.Any, portNo)); Console.WriteLine("Listening on " + acceptor.LocalEndPoint); } catch (Exception ex) { throw new Exception("Message Receiver를 생성하지 못했습니다.", ex); } }
/// <summary> /// Constructor /// </summary> /// <param name="ipAddress"></param> /// <param name="portNo"></param> private void init(IPAddress ipAddress, int portNo) { try { // 1. CONNECTOR 를 생성한다. this.connector = new AsyncSocketConnector(); this.connector.FilterChain.AddLast("logger", new LoggingFilter()); ObjectSerializationCodecFactory objectSerializationCodecFactory = new ObjectSerializationCodecFactory(); objectSerializationCodecFactory.EncoderMaxObjectSize = CommConst.MAX_TRANSDATA_SIZE; this.connector.FilterChain.AddLast("codec", new ProtocolCodecFilter(objectSerializationCodecFactory)); this.connector.SessionClosed += (o, e) => OnSessionClosed(); this.connector.MessageReceived += OnMessageReceived; // 2. 상대방에게 접속을 시도한다. this.endPoint = new IPEndPoint(ipAddress, portNo); this.Connect(); } catch (Exception ex) { throw new Exception(String.Format("{0}:{1}에 연결할 수 없습니다.", ipAddress, portNo), ex); } }