예제 #1
0
        void SendThread(MyCon con)
        {
            int id = ++maxId;

            Debug.LogError("SendThread Start " + id);
            while (!con.isClose && !DemoServer.demoServer.stop)
            {
                lock (con.msgBuffer) {
                    while (!con.isClose && con.msgBuffer.Count > 0 && !DemoServer.demoServer.stop)
                    {
                        int sent = 0;
                        int size = con.msgBuffer [0].Length;
                        while (!con.isClose && sent < size)
                        {
                            try {
                                sent += con.connect.Send(con.msgBuffer [0], sent, size - sent, SocketFlags.None);
                            } catch (SocketException ex) {
                                if (ex.SocketErrorCode == SocketError.WouldBlock ||
                                    ex.SocketErrorCode == SocketError.IOPending ||
                                    ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
                                {
                                    //Thread.Sleep(100);
                                    Debug.LogError("SendError Socket");
                                    break;
                                }
                                else                                    //SendThread Serious Error
                                {
                                    Debug.LogError("Critical Server Error " + ex.SocketErrorCode);
                                    con.Close();
                                    //DemoServer.demoServer.ShutDown();
                                    break;
                                }
                            }
                        }
                        con.msgBuffer.RemoveAt(0);
                    }
                }
                Thread.Sleep(100);
            }

            Debug.LogError("Send Finish " + id);
        }
예제 #2
0
        //Connection Lost Then Restart New Accept
        public void run()
        {
            InitListenSocket();
            Debug.LogError("Start Demo Server");
            byte[] buffer = new byte[1024];
            while (!DemoServer.demoServer.stop)
            {
                Debug.LogError("Start Accept Socket");
                Socket connect = null;
                msgReader           = new ServerMsgReader();
                msgReader.msgHandle = handleMsg;
                while (!DemoServer.demoServer.stop)
                {
                    try {
                        Debug.LogError("Server Start Accept " + random.Next());
                        connect = socket.Accept();
                        break;
                    } catch (Exception ex) {
                        Debug.LogError("ServerSocket AcceptError " + ex);
                        //socket.Shutdown(SocketShutdown.Both);
                        try {
                            socket.Close();
                        } catch (Exception ex1) {
                            Debug.LogError("Server Socket Close Error " + ex1);
                        }
                        Thread.Sleep(1);
                        if (!DemoServer.demoServer.stop)
                        {
                            InitListenSocket();
                        }
                    }
                }

                var con = new MyCon(connect);
                if (!DemoServer.demoServer.stop)
                {
                    if (currentCon != null)
                    {
                        lock (currentCon.msgBuffer) {
                            currentCon.msgBuffer.Clear();
                        }
                    }

                    currentCon = con;
                    var sendThread = new Thread(new ThreadStart(delegate() {
                        SendThread(con);
                    }));

                    sendThread.Start();
                }

                while (!con.isClose && !DemoServer.demoServer.stop)
                {
                    int num = 0;
                    try {
                        num = connect.Receive(buffer);
                    } catch (SocketException ex) {
                        if (ex.SocketErrorCode == SocketError.WouldBlock ||
                            ex.SocketErrorCode == SocketError.IOPending ||
                            ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
                        {
                            Debug.LogError("ReceiveSocket Error " + ex.SocketErrorCode);
                            Thread.Sleep(100);
                        }
                        else
                        {
                            Debug.LogError("Critical Server ReceiveError " + ex.SocketErrorCode);
                            con.Close();
                            break;
                        }
                    }
                    if (num > 0)
                    {
                        msgReader.process(buffer, (uint)num);
                    }
                    else
                    {
                        Debug.LogError("Receive Buffer Length " + num);
                        con.isConnected = false;
                        con.Close();
                        currentCon = null;
                    }
                }
                Debug.LogError("Socket Connect Stop");
            }

            Debug.LogError("DemoServer Stop");
            socket.Close();
        }