コード例 #1
0
ファイル: TcpServer.cs プロジェクト: KonstantinKolesnik/MFE
        public void Start()
        {
            if (serverSocket == null)
            {
                serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint localEP = new IPEndPoint(IPAddress.Any, port);
                serverSocket.Bind(localEP);
                serverSocket.Listen(int.MaxValue);

                isStopped = false;

                new Thread(delegate
                {
                    using (serverSocket)
                    {
                        while (!isStopped)
                        {
                            try
                            {
                                if (serverSocket.Poll(1000 * 10, SelectMode.SelectRead))
                                {
                                    TcpSession session = new TcpSession(serverSocket.Accept());
                                    session.DataReceived += new TcpSessionDataReceivedEventHandler(NetworkClient_DataReceived);
                                    session.Disconnected += new TcpSessionEventHandler(NetworkClient_Disconnected);
                                    sessions.Add(session);

                                    if (SessionConnected != null)
                                        SessionConnected(session);

                                    session.Start();
                                }
                            }
                            catch (SocketException e)
                            {
                                if (e.ErrorCode != 10035)
                                    break;
                            }
                            catch (Exception e)
                            {
                                Debug.Print(e.Message);
                                break;
                            }
                            Thread.Sleep(0);
                        }
                    }

                    serverSocket.Close();
                    serverSocket = null;
                    sessions.Clear();
                }).Start();
            }
        }
コード例 #2
0
ファイル: Model.cs プロジェクト: KonstantinKolesnik/DTC
 private void Session_Disconnected(TcpSession session)
 {
     // TODO: release locos and accessories
 }
コード例 #3
0
ファイル: Model.cs プロジェクト: KonstantinKolesnik/DTC
        private bool Session_DataReceived(TcpSession session, byte[] data)
        {
            BlinkLED(HWConfig.LEDNetwork);

            NetworkMessageReceiver nmr = session.Tag as NetworkMessageReceiver;
            NetworkMessage[] msgs = nmr.Process(data);
            if (msgs != null)
                foreach (NetworkMessage msg in msgs)
                {
                    NetworkMessage response = ProcessNetworkMessage(msg);
                    if (response != null)
                    {
                        session.Send(WSDataFrame.WrapString(response.PackToString(nmr.MessageFormat)));
                        BlinkLED(HWConfig.LEDNetwork);
                    }
                }

            return false; // don't disconnect
        }
コード例 #4
0
ファイル: Model.cs プロジェクト: KonstantinKolesnik/DTC
 private void Session_Connected(TcpSession session)
 {
     session.Tag = new NetworkMessageReceiver(msgFormat);
 }
コード例 #5
0
 private void Session_Disconnected(TcpSession session)
 {
     
 }
コード例 #6
0
ファイル: TcpServer.cs プロジェクト: KonstantinKolesnik/MFE
        private void NetworkClient_Disconnected(TcpSession session)
        {
            if (SessionDisconnected != null)
                SessionDisconnected(session);

            if (sessions.Contains(session))
                sessions.Remove(session);
        }
コード例 #7
0
ファイル: TcpServer.cs プロジェクト: KonstantinKolesnik/MFE
 private bool NetworkClient_DataReceived(TcpSession session, byte[] data)
 {
     return SessionDataReceived != null ? SessionDataReceived(session, data) : false;
 }
コード例 #8
0
ファイル: WSServer.cs プロジェクト: KonstantinKolesnik/MFE
        private bool tcpServer_SessionReceived(TcpSession session, byte[] data)
        {
            if (!session.IsHandshaked)
            {
                // from tablet:
                /*
                GET /Typhoon HTTP/1.1
                Upgrade: WebSocket
                Connection: Upgrade
                Host: 192.168.0.102:2013
                Origin: http://192.168.0.102:81
                Sec-Websocket-Key: +d6dlnAp7rrQ3otS7Zvi7g==
                Sec-WebSocket-Version: 13
                +d6dlnAp7rrQ3otS7Zvi7g==: websocket
                +d6dlnAp7rrQ3otS7Zvi7g==: Upgrade
                */

                // from local:
                /*
                GET /Typhoon HTTP/1.1
                Upgrade: WebSocket
                Connection: Upgrade
                Host: localhost:2013
                Origin: http://localhost:81
                Sec-Websocket-Key: K92AZtSFpS+9OgiwcPMheg==
                Sec-WebSocket-Version: 13
                K92AZtSFpS+9OgiwcPMheg==: websocket
                K92AZtSFpS+9OgiwcPMheg==: x-webkit-deflate-frame
                K92AZtSFpS+9OgiwcPMheg==: Upgrade
                */

                // location: ws://localhost:2013
                // origin: "http://localhost:81"

                WSClientHandshake chs = WSClientHandshake.FromBytes(data);
                //if (chs.IsValid && "ws://" + chs.Host == location && chs.Origin == origin)
                //if (chs.IsValid && "ws://" + chs.Host == location)
                if (chs.IsValid)
                {
                    WSServerHandshake shs = new WSServerHandshake(chs.Key);
                    string stringShake = shs.ToString();
                    byte[] byteResponse = Encoding.UTF8.GetBytes(stringShake);
                    session.Send(byteResponse);
                    session.IsHandshaked = true;

                    // for debug:
                    //client.Send(WebSocketDataFrame.WrapString("Hello from server!!!"));

                    return false;
                }
                else
                    return true; // disconnect client
            }
            else
            {
                WSDataFrame frame = new WSDataFrame(data);
                byte[] payload = null;
                if (frame.IsValid() && frame.FIN)
                    payload = frame.GetPayload();

                // for debug:
                //string s = new string(Encoding.UTF8.GetChars(payload));
                //string b = "";
                //b += s;

                return SessionDataReceived != null ? SessionDataReceived(session, payload) : false;
            }
        }
コード例 #9
0
ファイル: WSServer.cs プロジェクト: KonstantinKolesnik/MFE
 private void tcpServer_SessionDisconnected(TcpSession session)
 {
     if (SessionDisconnected != null)
         SessionDisconnected(session);
 }
コード例 #10
0
ファイル: TcpServer.cs プロジェクト: radtek/MFE
 private bool NetworkClient_DataReceived(TcpSession session, byte[] data)
 {
     return(SessionDataReceived != null?SessionDataReceived(session, data) : false);
 }