Beispiel #1
0
        // HEY DUDE, USE PIPES C++, CONTAIN DATA, series of tubes, etc.
        private static void ReadCallback(IAsyncResult ar)
        {
            DisposableUtilities utilities = new DisposableUtilities();
            String content = String.Empty;
            //Console.WriteLine ("CommunicationServer :: reading ..");
            StateObject ConnectorState = (StateObject)ar.AsyncState;
            Socket handler = ConnectorState.WorkSocket;
            try {
                int bytes_read = handler.EndReceive (ar);
                if (bytes_read > 0) {
                    // There might be more fata, so store the data received so far.
                    ConnectorState.sb.Append (Encoding.UTF8.GetString (ConnectorState.buffer, 0, bytes_read));
                    // Check for end-of-line tag. If it is not there => read more data.
                    content = ConnectorState.sb.ToString ();
                    //Console.WriteLine ("CommunicationServer :: received {0}", content);
                    //if (content.IndexOf ("\n\r") > -1) {
                    if (content.IndexOf ("<EOF>") > -1) {
                        //Console.WriteLine ("CommunicationServer :: received {0}", content);

                        // I still smoke hack every day ..
                        //Array.Clear(ConnectorState.buffer, 0, ConnectorState.buffer.Length);
                        ConnectorState.sb.Clear();

                        //#Alpha5
                        //Send (handler, content);

                        // parser
                        String[] commands = utilities.explode(new string[] { "\n", "\r", "<EOF>" }, content);
                        foreach (string s in commands) {
                            // skip all nonsense
                            if(s.Length < 3 || s.Contains("<EOF>"))
                                continue;
                            Console.WriteLine("CommunicationConnector :: {0}", String.IsNullOrEmpty(s) ? "<>" : s);

                            // IPC-Hub control messages
                            if(s.Contains("IPCH")) {
                                // synchronize threads
                                Thread.Sleep(30);
                                // pair modules
                                string[] i_deli = new string[] { ":", "\n", "\r" };
                                string[] i_hash = content.Split(i_deli, StringSplitOptions.RemoveEmptyEntries);
                                ThreadManager.AddEndpoint(handler, i_hash[1]);
                                                                        // bridge builder part 2 ..
                                // ConnectModuleBridge(handler, hash);	// hashed module can serve any amount of connections.
                                //
                                // EnableModuleBridge(handler, hash);	// new disposable container to pass data P-to-P in two directions.
                                //// new disposable container to pass the data
                                //// one container ONLY transfers data in TWO DIRECTIONS !!
                                //// 1 <=> 2
                                //// 1 <=> 3
                                //// 1 <=> 4
                                //// each in own "disposable" thread
                                //// but remember to manage object lifetime !! ServerLifetime monitor.
                            } else {
                                // proceed to module autodetection and derail connection once the new bridge is ready
                                if(ThreadManager.CreateModule(handler, s) != -1) { return; }
                            }
                        }
                        handler.BeginReceive (ConnectorState.buffer, 0, StateObject.buffersize, 0, new AsyncCallback (ReadCallback), ConnectorState); // read more data when finished
                    } else {
                        handler.BeginReceive (ConnectorState.buffer, 0, StateObject.buffersize, 0, new AsyncCallback (ReadCallback), ConnectorState);
                    }
                }
            } catch(SocketException) {
                //handler.Shutdown (SocketShutdown.Both);
                handler.Close ();
                Console.WriteLine ("CommunicationConnector disconnected");
            }
            // I smoke hack every day.
            //handler.BeginReceive (ConnectorState.buffer, 0, StateObject.buffersize, 0, new AsyncCallback (ReadCallback), ConnectorState);
        }
Beispiel #2
0
 // create new hashed module, connect it later
 public int CreateModule(Socket handler, String passed_message)
 {
     //
     DisposableUtilities disposable = new DisposableUtilities();
     String hash;
     //
     generate_fresh_hash:
     hash = disposable.GetHash();
     //
     foreach (var m in ModuleManager)
         if (m.CheckFingerprint(hash) == false) { continue; /* fingerprint is suitable for use */ } else { goto generate_fresh_hash; /* fingerprint found, generate new */ }
     //
     //Console.WriteLine("CreateModule: hash={0}", hash);
     //
     Module module = new Module(hash, handler, passed_message);
     ModuleManager.Add(module);
     // return -1 if autodetection fails
     return 1;
 }