Ejemplo n.º 1
0
        /// <summary>
        /// 启动服务,已经启动则忽略,被占用则忽略加入和启动
        /// </summary>
        /// <param name="port">端口号</param>
        /// <param name="server">服务器</param>
        private void start(int port, ServerBase server)
        {
            if (port < 1024 || null == server)
            {
                return;
            }
            if (this.listenermap.ContainsKey(port))
            {
                return;
            }
            server.Port = port;
            IPEndPoint  localport = new IPEndPoint(IPAddress.Any, port);
            TcpListener listener  = new TcpListener(localport);

            this.listenermap.Add(port, listener);
            try
            {
                listener.Start();
            }
            catch
            {
                this.listenermap.Remove(port);
                return;
            }
            while (true)
            {
                Socket client = null;
                try
                {
                    client         = listener.AcceptSocket();
                    client.NoDelay = true;
                }
                catch (Exception ex)
                {
                    ErrorRequestEventArgs args = new ErrorRequestEventArgs(client, server, new byte[0], ex);
                    this.raiseEvent(args);
                    if (!this.listenermap.ContainsKey(port))
                    {
                        return;
                    }
                    listener.Start();
                    continue;
                }
                ClientInfo info = server.Add(client);
                if (null == info)
                {
                    Thread.Sleep(60000);
                    continue;
                }
                ThreadManager.QueueUserWorkItem(delegate(object obj) { this.readData(info, server); }, null);
                ThreadManager.QueueUserWorkItem(delegate(object obj) { this.writeData(info, server); }, null);
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 服务端解析异常事件
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 void commisrv_ErrorHandle(object sender, ErrorRequestEventArgs e)
 {
     if (null == e)
         return;
     NameValueCollection data = new NameValueCollection();
     try
     {
         if (null != e.Client && null != e.Client.RemoteEndPoint)
             data["客户端"] = e.Client.RemoteEndPoint.ToString();
     }
     catch { }
     data["类型"] = "服务端异常";
     if (null != e.Exception)
         LogMessage(e.Exception, data, EventLogEntryType.Error);
 }
Ejemplo n.º 3
0
 /// <summary>
 /// �������,�Ѿ���������,��ռ������Լ�������
 /// </summary>
 /// <param name="port">�˿ں�</param>
 /// <param name="server">������</param>
 private void start(int port, ServerBase server)
 {
     if (port < 1024 || null == server)
         return;
     if (this.listenermap.ContainsKey(port))
         return;
     server.Port = port;
     IPEndPoint localport = new IPEndPoint(IPAddress.Any, port);
     TcpListener listener = new TcpListener(localport);
     this.listenermap.Add(port, listener);
     try
     {
         listener.Start();
     }
     catch
     {
         this.listenermap.Remove(port);
         return;
     }
     while (true)
     {
         Socket client = null;
         try
         {
             client = listener.AcceptSocket();
         }
         catch (Exception ex)
         {
             ErrorRequestEventArgs args = new ErrorRequestEventArgs(client, server, new byte[0], ex);
             this.raiseEvent(args);
             if (!this.listenermap.ContainsKey(port))
                 return;
             listener.Start();
             continue;
         }
         ClientInfo info = new ClientInfo(client);
         Monitor.Enter(server);
         server.ClientList.Add(info);
         Monitor.PulseAll(server);
         Monitor.Exit(server);
         ThreadManager.QueueUserWorkItem(delegate(object obj) { this.readData(info, server); }, null);
         ThreadManager.QueueUserWorkItem(delegate(object obj) { this.writeData(info, server); }, null);
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// ��������ȡͨѶ����
        /// </summary>
        /// <param name="client">�ͻ�������</param>
        /// <param name="server">������</param>
        private void readData(ClientInfo clientinfo, ServerBase server)
        {
            if (null == clientinfo || null == server)
                return;
            Socket client = clientinfo.Client;
            bool isopen = false;
            int iM = 1048576, available = 0;
            try
            {
                isopen = client.Connected;
                available = isopen ? client.Available : 0;
            }
            catch { return; }

            List<byte[]> responseList = clientinfo.BufferResponse;
            byte[] buffer = new byte[client.ReceiveBufferSize];
            MemoryStream stream = new MemoryStream();
            byte[] b = null;
            while (true)
            {
                while (available > 0)
                {
                    try
                    {
                        //ִ������
                        byte[] request = stream.ToArray();
                        long pos = stream.Position;
                        int len = client.Receive(buffer);
                        available = client.Available;
                        clientinfo.OPdt = DateTime.Now;
                        if (request.Length > 0)
                        {
                            stream = new MemoryStream();
                            long lenr = request.LongLength;
                            for (int i = 0; i * iM < lenr; i++)
                                stream.Write(request, iM * i, lenr > (i + 1) * iM ? iM : (int)(lenr - i * iM));
                        }
                        stream.Seek(pos, SeekOrigin.Begin);
                        byte[] reqarg = new byte[len];
                        Array.Copy(buffer, reqarg, len);
                        RequestEventArgs argreq = new RequestEventArgs(client, server, reqarg);
                        this.raiseEvent(argreq);
                        b = server.Execute(clientinfo, buffer, len, ref stream);
                    }
                    catch (Exception ex)
                    {
                        byte[] request = stream.ToArray();
                        stream = new MemoryStream();
                        ErrorRequestEventArgs argerr = new ErrorRequestEventArgs(client, server, request, ex);
                        this.raiseEvent(argerr);
                        if (ex is SocketException)
                        {
                            isopen = false;
                            break;
                        }
                        continue;
                    }
                    //��Ӧ���д�뻺��,����ͬ���¼�д����Ӧ
                    if (null != b && b.Length > 0)
                    {
                        Monitor.Enter(clientinfo);
                        responseList.Add(b);
                        Monitor.PulseAll(clientinfo);
                        Monitor.Exit(clientinfo);
                    }
                }//while (available > 0)
                while(available < 1)
                {
                    Thread.Sleep(10);
                    try
                    {
                        isopen = client.Connected;
                        available = isopen ? client.Available : 0;
                        if (!isopen) break;
                    }
                    catch
                    {
                        available = 0;
                        isopen = false;
                        break;
                    }
                }
                if (!isopen)
                {
                    this.closeClientInfo(clientinfo, server);
                    return;
                }
            }//while (true)
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 服务器读取通讯数据
        /// </summary>
        /// <param name="client">客户端连接</param>
        /// <param name="server">服务器</param>
        private void readData(ClientInfo clientinfo, ServerBase server)
        {
            if (null == clientinfo || null == server)
            {
                return;
            }
            Socket client = clientinfo.Client;
            bool   isopen = false;
            int    iM = 1048576, available = 0;

            try
            {
                isopen    = client.Connected;
                available = isopen ? client.Available : 0;
            }
            catch { return; }

            List <byte[]> responseList = clientinfo.BufferResponse;

            byte[]       buffer        = new byte[client.ReceiveBufferSize];
            MemoryStream stream        = new MemoryStream();

            byte[] b = null;
            while (true)
            {
                while (available > 0)
                {
                    try
                    {
                        //执行请求
                        byte[] request = stream.ToArray();
                        long   pos     = stream.Position;
                        int    len     = client.Receive(buffer);
                        string msgrp   = Encoding.GetEncoding("GB2312").GetString(buffer, 0, len);
                        Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " S接收:" + msgrp);
                        myLog.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " S接收:" + msgrp);
                        clientinfo.OPdt = DateTime.Now;
                        if (request.Length > 0)
                        {
                            stream = new MemoryStream();
                            long lenr = request.LongLength;
                            for (int i = 0; i *iM < lenr; i++)
                            {
                                stream.Write(request, iM * i, lenr > (i + 1) * iM ? iM : (int)(lenr - i * iM));
                            }
                        }
                        stream.Seek(pos, SeekOrigin.Begin);
                        byte[] reqarg = new byte[len];
                        Array.Copy(buffer, reqarg, len);
                        RequestEventArgs argreq = new RequestEventArgs(client, server, reqarg);
                        this.raiseEvent(argreq);
                        b         = server.Execute(clientinfo, buffer, len, ref stream);
                        available = client.Available;
                    }
                    catch (Exception ex)
                    {
                        byte[] request = stream.ToArray();
                        stream = new MemoryStream();
                        //NameValueCollection attr = new NameValueCollection();
                        //attr["剩余连接数"] = Convert.ToString(server.ClientList.Count);
                        //ExceptionManager.Publish(ex, attr);
                        ErrorRequestEventArgs argerr = new ErrorRequestEventArgs(client, server, request, ex);
                        this.raiseEvent(argerr);
                        if (ex is SocketException)
                        {
                            isopen = false;
                            break;
                        }
                        try { available = 0; isopen = client.Connected; available = isopen ? client.Available : 0; }
                        catch { isopen = false; }
                        continue;
                    }
                    //响应结果写入缓存,触发同步事件写入响应
                    if (null != b && b.Length > 0)
                    {
                        Monitor.Enter(clientinfo);
                        responseList.Add(b);
                        Monitor.PulseAll(clientinfo);
                        Monitor.Exit(clientinfo);
                    }
                }//while (available > 0)
                while (available < 1)
                {
                    Thread.Sleep(10);
                    try
                    {
                        isopen    = client.Connected;
                        available = isopen ? client.Available : 0;
                        if (!isopen)
                        {
                            break;
                        }
                    }
                    catch
                    {
                        available = 0;
                        isopen    = false;
                        break;
                    }
                }
                if (!isopen)
                {
                    server.Close(clientinfo);
                    return;
                }
            }//while (true)
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 服务器读取通讯数据
        /// </summary>
        /// <param name="client">客户端连接</param>
        /// <param name="server">服务器</param>
        private void readData(ClientInfo clientinfo, ServerBase server)
        {
            if (null == clientinfo || null == server)
            {
                return;
            }
            Socket client = clientinfo.Client;
            bool   isopen = false;
            int    iM = 1048576, available = 0;

            try
            {
                isopen    = client.Connected;
                available = isopen ? client.Available : 0;
            }
            catch { return; }

            List <byte[]> responseList = clientinfo.BufferResponse;

            byte[]       buffer        = new byte[client.ReceiveBufferSize];
            MemoryStream stream        = new MemoryStream();

            byte[] b = null;
            while (true)
            {
                while (available > 0)
                {
                    try
                    {
                        //执行请求
                        byte[] request = stream.ToArray();
                        long   pos     = stream.Position;
                        int    len     = client.Receive(buffer);
                        available       = client.Available;
                        clientinfo.OPdt = DateTime.Now;
                        if (request.Length > 0)
                        {
                            stream = new MemoryStream();
                            long lenr = request.LongLength;
                            for (int i = 0; i *iM < lenr; i++)
                            {
                                stream.Write(request, iM * i, lenr > (i + 1) * iM ? iM : (int)(lenr - i * iM));
                            }
                        }
                        stream.Seek(pos, SeekOrigin.Begin);
                        byte[] reqarg = new byte[len];
                        Array.Copy(buffer, reqarg, len);
                        RequestEventArgs argreq = new RequestEventArgs(client, server, reqarg);
                        this.raiseEvent(argreq);
                        b = server.Execute(clientinfo, buffer, len, ref stream);
                    }
                    catch (Exception ex)
                    {
                        byte[] request = stream.ToArray();
                        stream = new MemoryStream();
                        ErrorRequestEventArgs argerr = new ErrorRequestEventArgs(client, server, request, ex);
                        this.raiseEvent(argerr);
                        if (ex is SocketException)
                        {
                            isopen = false;
                            break;
                        }
                        continue;
                    }
                    //响应结果写入缓存,触发同步事件写入响应
                    if (null != b && b.Length > 0)
                    {
                        Monitor.Enter(clientinfo);
                        responseList.Add(b);
                        Monitor.PulseAll(clientinfo);
                        Monitor.Exit(clientinfo);
                    }
                }//while (available > 0)
                while (available < 1)
                {
                    Thread.Sleep(10);
                    try
                    {
                        isopen    = client.Connected;
                        available = isopen ? client.Available : 0;
                        if (!isopen)
                        {
                            break;
                        }
                    }
                    catch
                    {
                        available = 0;
                        isopen    = false;
                        break;
                    }
                }
                if (!isopen)
                {
                    this.closeClientInfo(clientinfo, server);
                    return;
                }
            }//while (true)
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 服务器读取通讯数据
        /// </summary>
        /// <param name="client">客户端连接</param>
        /// <param name="server">服务器</param>
        private void readData(ClientInfo clientinfo, ServerBase server)
        {
            if (null == clientinfo || null == server)
                return;
            Socket client = clientinfo.Client;
            bool isopen = false;
            int iM = 1048576, available = 0;
            try
            {
                isopen = client.Connected;
                available = isopen ? client.Available : 0;
            }
            catch { return; }

            List<byte[]> responseList = clientinfo.BufferResponse;
            byte[] buffer = new byte[client.ReceiveBufferSize];
            MemoryStream stream = new MemoryStream();
            byte[] b = null;
            while (true)
            {
                while (available > 0)
                {
                    try
                    {
                        //执行请求
                        byte[] request = stream.ToArray();
                        long pos = stream.Position;
                        int len = client.Receive(buffer);
                        string msgrp = Encoding.GetEncoding("GB2312").GetString(buffer, 0, len);
                        Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " S接收:" + msgrp);
                        myLog.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " S接收:" + msgrp);
                        clientinfo.OPdt = DateTime.Now;
                        if (request.Length > 0)
                        {
                            stream = new MemoryStream();
                            long lenr = request.LongLength;
                            for (int i = 0; i * iM < lenr; i++)
                                stream.Write(request, iM * i, lenr > (i + 1) * iM ? iM : (int)(lenr - i * iM));
                        }
                        stream.Seek(pos, SeekOrigin.Begin);
                        byte[] reqarg = new byte[len];
                        Array.Copy(buffer, reqarg, len);
                        RequestEventArgs argreq = new RequestEventArgs(client, server, reqarg);
                        this.raiseEvent(argreq);
                        b = server.Execute(clientinfo, buffer, len, ref stream);
                        available = client.Available;
                    }
                    catch (Exception ex)
                    {
                        byte[] request = stream.ToArray();
                        stream = new MemoryStream();
                        //NameValueCollection attr = new NameValueCollection();
                        //attr["剩余连接数"] = Convert.ToString(server.ClientList.Count);
                        //ExceptionManager.Publish(ex, attr);
                        ErrorRequestEventArgs argerr = new ErrorRequestEventArgs(client, server, request, ex);
                        this.raiseEvent(argerr);
                        if (ex is SocketException)
                        {
                            isopen = false;
                            break;
                        }
                        try { available = 0; isopen = client.Connected; available = isopen ? client.Available : 0; }
                        catch { isopen = false; }
                        continue;
                    }
                    //响应结果写入缓存,触发同步事件写入响应
                    if (null != b && b.Length > 0)
                    {
                        Monitor.Enter(clientinfo);
                        responseList.Add(b);
                        Monitor.PulseAll(clientinfo);
                        Monitor.Exit(clientinfo);
                    }
                }//while (available > 0)
                while(available < 1)
                {
                    Thread.Sleep(10);
                    try
                    {
                        isopen = client.Connected;
                        available = isopen ? client.Available : 0;
                        if (!isopen) break;
                    }
                    catch
                    {
                        available = 0; 
                        isopen = false; 
                        break;
                    }
                }
                if (!isopen)
                {
                    server.Close(clientinfo);
                    return;
                }
            }//while (true)
        }