コード例 #1
0
        private void ConnectCallback(IAsyncResult ar)
        {
            try
            {
                _tcpSocket.EndConnect(ar);


                if (_tcpSocket.Connected)
                {
                    lock (connectSync)
                    {
                        _isConnected = true;
                    }
                    _sh               = new SocketHandler(_tcpSocket, _channelParameters.BufferSize);
                    _sh.Received     += sh_received;
                    _sh.Disconnected += sh_disconnected;
                    _sh.Start();
                    LocalHost = ((IPEndPoint)_tcpSocket.LocalEndPoint).Address.ToString();
                    LocalPort = ((IPEndPoint)_tcpSocket.LocalEndPoint).Port;
                    if (Connected != null)
                    {
                        EventsHelper.Fire(Connected, this, new ConnectionEventArgs(0, 0, "OK"));
                        //Connected(this, new ConnectionEventArgs(0, 0, "OK"));
                    }
                }
                else
                {
                    lock (connectSync)
                    {
                        _isConnected = false;
                    }
                    if (OnError != null)
                    {
                        OnError(this, new ErrorEventArgs(-1, "Cannot connect to remote host."));
                    }
                }
            }
            catch (SocketException se)
            {
                if (OnError != null)
                {
                    OnError(this, new ErrorEventArgs(se.ErrorCode, se.Message));
                }
            }
            catch (Exception e)
            {
                if (OnError != null)
                {
                    OnError(this, new ErrorEventArgs(-1, e.Message));
                }
            }
        }
コード例 #2
0
ファイル: IPDaemon.cs プロジェクト: kruthikk/Websocket
        private void sh_disconnected(object sender, ConnectionEventArgs e)
        {
            SocketHandler sh = sender as SocketHandler;

            if (sh != null)
            {
                DeleteClientConnection(sh.ConnectionId);
            }

            //if (OnError != null)
            //{
            //    OnError(this, new ErrorEventArgs(e.StatusCode, e.Description));
            //}
            if (ClientDisconnected != null)
            {
                ClientDisconnected(this, e);
            }
        }
コード例 #3
0
ファイル: IPDaemon.cs プロジェクト: kruthikk/Websocket
        public void Send(int connectionId, byte[] data)
        {
            SocketHandler sh = null;

            lock (m_workerSocketList)
            {
                if (m_workerSocketList.ContainsKey(connectionId))
                {
                    sh = m_workerSocketList[connectionId];
                }
            }

            if (sh != null)
            {
                sh.Send(data);
            }
            else
            {
                // remove from socket list --this should not happen so log it
                DeleteClientConnection(connectionId);
                OnError(this, new ErrorEventArgs(-1, string.Format("Connection not available for Id: {0}", connectionId)));
            }
        }
コード例 #4
0
ファイル: IPDaemon.cs プロジェクト: kruthikk/Websocket
        private void OnClientConnect(IAsyncResult asyn)
        {
            try
            {
                // Here we complete/end the BeginAccept() asynchronous call
                // by calling EndAccept() - which returns the reference to
                // a new Socket object
                Socket workerSocket = _mainSocket.EndAccept(asyn);

                int tmpConnId;
                lock (m_DisconnectedConnections)
                {
                    if (m_DisconnectedConnections.Count > 0)
                    {
                        // Assign old disconnecetd connection id to a new one
                        tmpConnId = m_DisconnectedConnections[0];
                        m_DisconnectedConnections.RemoveAt(0);
                    }
                    else
                    {
                        tmpConnId = Interlocked.Increment(ref m_clientCount);
                    }
                }
                if (workerSocket.Connected)
                {
                    if (_channelParameters.TimeOut > 0)
                    {
                        workerSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, _channelParameters.TimeOut);
                    }
                    if (_channelParameters.TimeOut > 0)
                    {
                        workerSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, _channelParameters.TimeOut);
                    }
                    SocketHandler sh = new SocketHandler(workerSocket, _channelParameters.BufferSize);
                    sh.Received     += sh_received;
                    sh.Disconnected += sh_disconnected;
                    sh.ConnectionId  = tmpConnId;
                    // Add the workerSocket handler reference to our ArrayList
                    lock (m_workerSocketList)
                    {
                        m_workerSocketList.Add(sh.ConnectionId, sh);
                    }
                    //12/29 set the delimiter before receiving...
                    //11/22 - By default set message builder eol to carriage return...
                    EOL(sh.ConnectionId, string.Format("\r"));

                    sh.Start();
                    if (Connected != null)
                    {
                        EventsHelper.Fire(Connected, this, new ConnectionEventArgs(sh.ConnectionId, 0, "OK"));
                        //Connected(this, new ConnectionEventArgs(sh.ConnectionId, 0, "OK"));
                    }
                }

                // Since the main Socket is now free, it can go back and wait for
                // other clients who are attempting to connect
                if (ConnectionCount < MaxConnections)
                {
                    _mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);
                }
            }
            catch (SocketException se)
            {
                if (OnError != null)
                {
                    OnError(this, new ErrorEventArgs(se.ErrorCode, se.Message));
                }
            }
            catch (Exception e)
            {
                if (OnError != null)
                {
                    OnError(this, new ErrorEventArgs(-1, e.Message));
                }
                //throw e;
            }
        }