Beispiel #1
0
        protected AsyncSocketImpl(AsyncSocketImpl sockImpl)
        {
            sockImpl.DisableEvents();
            socketManager_.RemoveAsyncSocket(sockImpl);

            socket_ = sockImpl.socket_;

            Init();
        }
Beispiel #2
0
 internal void RemoveAsyncSocket(AsyncSocketImpl socket)
 {
     lock (locker_)
     {
         if (sockets_.ContainsKey(socket.socket_))
         {
             sockets_.Remove(socket.socket_);
         }
     }
 }
Beispiel #3
0
 internal void AddAsyncSocket(AsyncSocketImpl socket)
 {
     lock (locker_)
     {
         if (!sockets_.ContainsKey(socket.socket_))
         {
             sockets_[socket.socket_] = socket;
         }
     }
 }
Beispiel #4
0
        public bool AttachHandle(AsyncSocketImpl sockImpl)
        {
            if (sockImpl == null)
            {
                return(false);
            }

            sockImpl.DisableEvents();
            socketManager_.RemoveAsyncSocket(sockImpl);

            DisableEvents();
            socket_ = sockImpl.socket_;

            Init();

            return(true);
        }
Beispiel #5
0
            private void SocketManagerThreadFunc()
            {
                while (!shutDown_)
                {
                    List <Socket> readSockets  = new List <Socket>();
                    List <Socket> writeSockets = new List <Socket>();
                    List <Socket> errorSockets = new List <Socket>();

                    lock (locker_)
                    {
                        sockets_.Keys.ToList().ForEach(s =>
                        {
                            readSockets.Add(s);
                            writeSockets.Add(s);
                            errorSockets.Add(s);
                        });
                    }

                    if (readSockets.Count == 0)
                    {
                        Thread.Sleep(TIMEOUT);
                        continue;
                    }

                    try
                    {
                        Socket.Select(readSockets, writeSockets, errorSockets, TIMEOUT * 1000);

                        if (shutDown_)
                        {
                            return;
                        }

                        lock (locker_)
                        {
                            readSockets.ForEach(s =>
                            {
                                if (!sockets_.ContainsKey(s))
                                {
                                    return;
                                }

                                AsyncSocketImpl socket = sockets_[s];

                                if (socket.listen_called_)
                                {
                                    socket.FireOnAcceptEvent();
                                }
                                else
                                {
                                    socket.FireOnReceiveEvent();
                                }
                            });

                            writeSockets.ForEach(s =>
                            {
                                if (!sockets_.ContainsKey(s))
                                {
                                    return;
                                }

                                AsyncSocketImpl socket = sockets_[s];

                                if (socket.Connected && socket.connect_event_fired_)
                                {
                                    socket.FireOnSendEvent();
                                }
                                else
                                {
                                    socket.connect_event_fired_ = true;
                                    socket.FireOnConnectEvent();
                                }
                            });
                            errorSockets.ForEach(s =>
                            {
                                if (!sockets_.ContainsKey(s))
                                {
                                    return;
                                }

                                AsyncSocketImpl socket = sockets_[s];

                                socket.FireOnErrorEvent(10061);
                            });
                        }
                    }
                    catch (SocketException ex)
                    {
                        MpdUtilities.DebugLogError(string.Format("Socket Exception:{0},{1}",
                                                                 ex.ErrorCode, ex.SocketErrorCode), ex);
                    }
                    catch (Exception ex)
                    {
                        MpdUtilities.DebugLogError(ex);
                    }
                }//while
            }