예제 #1
0
        public void OpenConnection(Server serverConfig, Context context, Action <Connection> onConnected)
        {
            ValidateProtocolName(serverConfig);

            int    port = ProtocolUtils.retrieveProtocolSetting(serverConfig, "port", -1);
            string host = ProtocolUtils.retrieveProtocolSetting(serverConfig, "host", (string)null);

            if (port == -1 || host == null)
            {
                throw new Error(ErrorCode.CONNECTION_ERROR, "No port and/or IP address is present in configuration.");
            }

            var socketAdapter = new BPSocketAdapter(host, port);

            onConnected(new WSJConnection(socketAdapter));
            socketAdapter.Open();
        }
예제 #2
0
        /// <summary>
        /// This is used as a callback to the BeginAcceptTcpClient on the TcpListener. It creates a new connection,
        /// invokes the user callback and continues to listen for new connections.
        /// </summary>
        /// <param name="ar"></param>
        private void CreateBPConnection(IAsyncResult ar)
        {
            AsyncServerState serverState = ar.AsyncState as AsyncServerState;

            if (serverState == null)
            {
                throw new ArgumentException("Invalid async state in async result", "ar");
            }

            // Create new connection and invoke the user callback.
            TcpClient client        = serverState.Listener.EndAcceptTcpClient(ar);
            var       socketAdapter = new BPSocketAdapter(client);

            serverState.Callback(new WSJConnection(socketAdapter));

            // Wait for the next connection attempt.
            serverState.Listener.BeginAcceptTcpClient(CreateBPConnection, serverState);
        }