private void HandleServerMsgRecv(ServerMsgArg args)
 {
     if (onServerMsgRecv != null)
     {
         onServerMsgRecv(this, args);
     }
 }
        public void SendCmd(ServerMsgArg arg)
        {
            if (!isServerStarted || !haveCheckedPW || arg == null)
            {
                return;
            }

            transmitLock.AcquireWriterLock(-1);
            try
            {
                transmitQueue.Enqueue(arg);
            }
            catch (Exception) { }
            finally
            {
                transmitLock.ReleaseWriterLock();
            }

            dataReady.Set();
        }
        private void SendMsg(ServerMsgArg arg)
        {
            try
            {
                Socket        rSocket = arg.socket;
                SocketMessage msg     = arg.msg;
                MsgType       type    = (MsgType)msg.msgType;
                if (showMsg)
                {
                    Trace.WriteLine(string.Format("{0}-Send Msg - {1}", DateTime.Now.ToString("HH:mm:ss:fff"), type));
                }
                byte[] buffer = null;
                switch (type)
                {
                case MsgType.CheckPassword_ECHO:
                    if (rSocket != null)
                    {
                        buffer = StrutsToBytesArray(msg);
                        if (buffer != null)
                        {
                            rSocket.Send(buffer);
                        }
                        if (msg.fParameter0 == 1)
                        {
                            clientSocket.Add(rSocket);
                            rSocket.BeginReceive(recvMsgBuffer, 0, recvMsgBuffer.Length, SocketFlags.None, new AsyncCallback(RecieveCallBack), rSocket);
                        }
                        else
                        {
                            if (rSocket != null)
                            {
                                if (rSocket.Connected)
                                {
                                    rSocket.Shutdown(SocketShutdown.Both);
                                    Thread.Sleep(10);
                                }
                                rSocket.Close();
                            }
                        }
                    }
                    break;

                case MsgType.SetName_ECHO:
                    if (rSocket != null)
                    {
                        buffer = StrutsToBytesArray(msg);
                        if (buffer != null)
                        {
                            rSocket.Send(buffer);
                        }
                    }
                    break;
                    //buffer = StrutsToBytesArray(msg);
                    //if (buffer != null)
                    //{
                    //    Socket badClient = null;
                    //    foreach (Socket client in clientSocket)
                    //    {
                    //        try
                    //        {
                    //            client.Send(buffer);
                    //        }
                    //        catch (Exception)
                    //        {
                    //            badClient = client;
                    //            continue;
                    //        }
                    //    }

                    //    if (badClient != null)
                    //        clientSocket.Remove(badClient);
                    //}

                    //break;
                }
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
                return;
            }
        }
        private void RecieveCallBack(IAsyncResult AR)
        {
            try
            {
                Socket rSocket = AR.AsyncState as Socket;
                int    REnd    = rSocket.EndReceive(AR);
                if (REnd <= 0)
                {
                    lock (lockObj)
                    {
                        if (clientSocket.Contains(rSocket))
                        {
                            clientSocket.Remove(rSocket);
                        }
                    }

                    if (rSocket != null)
                    {
                        if (rSocket.Connected)
                        {
                            rSocket.Shutdown(SocketShutdown.Both);
                            Thread.Sleep(10);
                        }
                        rSocket.Close();
                    }

                    return;
                }

                byte[] recvBuff = new byte[REnd];
                Array.Copy(recvMsgBuffer, 0, recvBuff, 0, REnd);
                object obj = BytesToStruts(recvBuff, typeof(SocketMessage));
                if (obj != null && obj is SocketMessage)
                {
                    SocketMessage arg     = (SocketMessage)obj;
                    MsgType       msgType = (MsgType)arg.msgType;
                    if (showMsg)
                    {
                        Trace.WriteLine(string.Format("{0}-Received Msg - {1}", DateTime.Now.ToString("HH:mm:ss:fff"), msgType));
                    }
                    switch (msgType)
                    {
                    case MsgType.CheckPassword:
                        string clientPW = Encoding.UTF8.GetString(arg.strParameter).Trim('\0');
                        if (string.Compare(clientPW, password, false) == 0)
                        {
                            haveCheckedPW = true;
                            ServerMsgArg checkPWArg = new ServerMsgArg(rSocket, arg);
                            HandleServerMsgRecv(checkPWArg);
                        }
                        else
                        {
                            haveCheckedPW = false;
                            SocketMessage msg = new SocketMessage();
                            msg.msgType     = (int)MsgType.CheckPassword_ECHO;
                            msg.fParameter0 = 0;
                            sendMsgBuffer   = StrutsToBytesArray(msg);
                            if (sendMsgBuffer != null)
                            {
                                rSocket.Send(sendMsgBuffer);
                            }

                            if (rSocket != null)
                            {
                                if (rSocket.Connected)
                                {
                                    rSocket.Shutdown(SocketShutdown.Both);
                                    Thread.Sleep(10);
                                }
                                rSocket.Close();
                            }
                        }
                        break;

                    case MsgType.SetName:
                    {
                        ServerMsgArg serverArg = new ServerMsgArg(rSocket, arg);
                        HandleServerMsgRecv(serverArg);
                        rSocket.BeginReceive(recvMsgBuffer, 0, recvMsgBuffer.Length, SocketFlags.None,
                                             new AsyncCallback(RecieveCallBack), rSocket);
                    }
                    break;
                    }
                }
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
                return;
            }
        }