예제 #1
0
        /// <summary>
        /// Listens for calls from the clients.
        /// </summary>
        /// <param name="stateInfo"></param>
        static void SocketThread(Object stateInfo)
        {
            Socket server_TcpSocket = null;

            try
            {
                IPEndPoint server_Address = new IPEndPoint(IPAddress.Any, SERVER_PORT);
                server_TcpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                server_TcpSocket.Bind(server_Address);

                byte[]         receivedData;
                int            dataCount;
                int            index;
                string         receivedMsg;
                typeMessage    msgType;
                string         msgData;
                string         msgHeader;
                MyMessageClass msgClass = new MyMessageClass(SERVER_ID); //-99... Server
                byte[]         response = new byte[MyMessageClass.BUFFER_SIZE_BYTE];

                server_TcpSocket.Listen(10);

                while (Run)
                {
                    //Blocking call for accepting requests.
                    Socket client_TcpSocket = server_TcpSocket.Accept();

                    //Address of the client:
                    IPEndPoint client_Address = (IPEndPoint)client_TcpSocket.RemoteEndPoint;

                    receivedData = new byte[MyMessageClass.BUFFER_SIZE_BYTE];
                    dataCount    = client_TcpSocket.Receive(receivedData);
                    if (dataCount != 0)
                    {
                        receivedMsg = Encoding.ASCII.GetString(receivedData, 0, dataCount);
                        msgHeader   = msgClass.getMessageHeader(receivedMsg);
                        msgData     = msgClass.getMessageData(receivedMsg);
                        msgType     = msgClass.getMessageType(msgHeader);
                        ClientData client;

                        switch (msgType)
                        {
                        case typeMessage.MSG_CONNECT:
                            client         = msgClass.ParseDataToClientData(msgType, msgData);
                            client.Number  = mServer.ClientNumber;
                            client.Address = client_Address;
                            client.Status  = ClientStati.Connected;
                            index          = mServer.FindClientIndex(client);
                            if (index == -1)
                            {
                                mServer.AddClient(client);
                            }
                            else
                            {
                                client = mServer.GetClientList()[index];
                            }
                            client.LastHeartBeat = DateTime.Now;
                            response             = msgClass.ConnectResponse(client, mServer.GetProjectListHash());
                            Console.WriteLine("Client added!");
                            break;

                        case typeMessage.MSG_NEWPROJECT:
                            mServer.AddProject(new Project(msgData));
                            response           = msgClass.NewProjectResponse(mServer.GetProjectListHash());
                            ProjectListChanged = true;
                            Console.WriteLine("Project added!");
                            break;

                        case typeMessage.MSG_ADDTIME:
                            Project tmp = msgClass.ParseDataToProject(typeMessage.MSG_ADDTIME, msgData);
                            response = mServer.UpdateTime(tmp)
                                    ? msgClass.AddTimeResponse(mServer.GetProjectListHash())
                                    : msgClass.AddTimeResponseError();
                            ProjectListChanged = true;
                            Console.WriteLine("Time added to project!");
                            break;

                        case typeMessage.MSG_HEARTBEAT:
                            client = msgClass.ParseDataToClientData(msgType, msgData);
                            index  = mServer.FindClientIndex(client);
                            if (index == -1)
                            {
                                throw new ArgumentOutOfRangeException();
                            }

                            mServer.GetClientList()[index].LastHeartBeat = DateTime.Now;
                            response = msgClass.HeartBeatResponse(mServer.GetClientList()[index],
                                                                  mServer.GetProjectListHash());
                            Console.WriteLine("HeartBeat to Client: " + index);
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }

                        // send response
                        client_TcpSocket.Send(response, response.Length, SocketFlags.None);
                    }

                    client_TcpSocket.Close();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error:\n" + e.Message);
            }
            finally
            {
                server_TcpSocket.Close();
            }
        }