示例#1
0
        public void Stop(string key)
        {
            StateObject state   = D2Sockets.Get(key);
            Socket      handler = state.workSocket;

            try
            {
                handler.BeginDisconnect(false, new AsyncCallback(DisconnectCallback), state);
            }
            catch (SocketException se)
            {
                ExceptionBuffer.Add(se);
            }
        }
示例#2
0
        public void StartListening()
        {
            IPHostEntry IPHEServer = Dns.GetHostEntry(Dns.GetHostName());
            IPAddress   IPAServer  = null;
            IPEndPoint  IPEPServer;

            foreach (IPAddress IPA in IPHEServer.AddressList)
            {
                if (IPA.AddressFamily == AddressFamily.InterNetwork)
                {
                    IPAServer = IPA;
                    break;
                }
            }

            IP         = IPAServer.ToString();
            IPEPServer = new IPEndPoint(IPAServer, this.Port);


            Socket       listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            LingerOption LO       = new LingerOption(false, 0);

            listener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, LO);
            listener.Bind(IPEPServer);
            listener.Listen(100);

            try
            {
                while (true)
                {
                    allDone.Reset();
                    listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);
                    allDone.WaitOne();
                }
            }
            catch (ThreadInterruptedException exc)
            {
                ExceptionBuffer.Add(exc);
                return;
            }
            finally
            {
                listener.Close();
            }
        }
示例#3
0
        private static void ReadCallback(IAsyncResult IAR)
        {
            StateObject state     = (StateObject)IAR.AsyncState;
            Socket      handler   = state.workSocket;
            int         bytesRead = 0;

            if (!handler.Connected)
            {
                return;
            }
            try
            {
                bytesRead = handler.EndReceive(IAR);
            }
            catch
            {
                return;
            }

            if (bytesRead <= 0)
            {
                return;
            }

            System.String content = System.String.Empty;
            state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
            content = state.sb.ToString();


            if (content.IndexOf(TcpAsyncCommon.EOM) < 0)
            {
                if (content.Length < StateObject.BufferSize / 2)
                {
                    try
                    {
                        handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
                    }catch (Exception e)
                    {
                        ExceptionBuffer.Add(e);
                    }
                }
                else
                {
                    state.sb.Clear();
                    //state.buffer = new byte[StateObject.BufferSize];
                    return;
                }
            }

            state.sb.Clear();
            //state.buffer = new byte[StateObject.BufferSize];
            List <string> Packets = content.SplitSafe(TcpAsyncCommon.EOM).ToList();

            if (Packets.Count > 0)
            {
                foreach (string packet in Packets)
                {
                    string subPacket = packet;
                    if (subPacket == null)
                    {
                        subPacket = "";
                    }
                    else
                    {
                        subPacket = subPacket.Replace(@"\\", @"\");
                    }

                    if (!D3BReceive.Contains(state.key))
                    {
                        return;
                    }
                    else
                    {
                        D3BReceive.Get(state.key).Set(subPacket);
                    }
                    //Thread.Sleep(1);
                }
            }
        }