Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit 225006543482ebe306ae865a3fbb16336322fac9 @cpearce committed Nov 12, 2010
Showing with 816 additions and 0 deletions.
  1. +451 −0 HttpMediaServer.cpp
  2. +20 −0 HttpMediaServer.sln
  3. +92 −0 HttpMediaServer.vcxproj
  4. +119 −0 Thread.cpp
  5. +103 −0 Thread.h
  6. +8 −0 stdafx.cpp
  7. +15 −0 stdafx.h
  8. +8 −0 targetver.h
Oops, something went wrong.
@@ -0,0 +1,20 @@
+
+Microsoft Visual Studio Solution File, Format Version 11.00
+# Visual Studio 2010
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "HttpMediaServer", "HttpMediaServer.vcxproj", "{1F99DEE3-AF25-4BE4-87F4-546C9887C968}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Win32 = Debug|Win32
+ Release|Win32 = Release|Win32
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {1F99DEE3-AF25-4BE4-87F4-546C9887C968}.Debug|Win32.ActiveCfg = Debug|Win32
+ {1F99DEE3-AF25-4BE4-87F4-546C9887C968}.Debug|Win32.Build.0 = Debug|Win32
+ {1F99DEE3-AF25-4BE4-87F4-546C9887C968}.Release|Win32.ActiveCfg = Release|Win32
+ {1F99DEE3-AF25-4BE4-87F4-546C9887C968}.Release|Win32.Build.0 = Release|Win32
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+EndGlobal
@@ -0,0 +1,92 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <ItemGroup Label="ProjectConfigurations">
+ <ProjectConfiguration Include="Debug|Win32">
+ <Configuration>Debug</Configuration>
+ <Platform>Win32</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="Release|Win32">
+ <Configuration>Release</Configuration>
+ <Platform>Win32</Platform>
+ </ProjectConfiguration>
+ </ItemGroup>
+ <PropertyGroup Label="Globals">
+ <ProjectGuid>{1F99DEE3-AF25-4BE4-87F4-546C9887C968}</ProjectGuid>
+ <Keyword>Win32Proj</Keyword>
+ <RootNamespace>HttpMediaServer</RootNamespace>
+ </PropertyGroup>
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
+ <ConfigurationType>Application</ConfigurationType>
+ <UseDebugLibraries>true</UseDebugLibraries>
+ <CharacterSet>Unicode</CharacterSet>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
+ <ConfigurationType>Application</ConfigurationType>
+ <UseDebugLibraries>false</UseDebugLibraries>
+ <WholeProgramOptimization>true</WholeProgramOptimization>
+ <CharacterSet>Unicode</CharacterSet>
+ </PropertyGroup>
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
+ <ImportGroup Label="ExtensionSettings">
+ </ImportGroup>
+ <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+ </ImportGroup>
+ <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+ </ImportGroup>
+ <PropertyGroup Label="UserMacros" />
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+ <LinkIncremental>true</LinkIncremental>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+ <LinkIncremental>false</LinkIncremental>
+ </PropertyGroup>
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+ <ClCompile>
+ <PrecompiledHeader>
+ </PrecompiledHeader>
+ <WarningLevel>Level3</WarningLevel>
+ <Optimization>Disabled</Optimization>
+ <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ </ClCompile>
+ <Link>
+ <SubSystem>Console</SubSystem>
+ <GenerateDebugInformation>true</GenerateDebugInformation>
+ </Link>
+ </ItemDefinitionGroup>
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+ <ClCompile>
+ <WarningLevel>Level3</WarningLevel>
+ <PrecompiledHeader>
+ </PrecompiledHeader>
+ <Optimization>MaxSpeed</Optimization>
+ <FunctionLevelLinking>true</FunctionLevelLinking>
+ <IntrinsicFunctions>true</IntrinsicFunctions>
+ <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ </ClCompile>
+ <Link>
+ <SubSystem>Console</SubSystem>
+ <GenerateDebugInformation>true</GenerateDebugInformation>
+ <EnableCOMDATFolding>true</EnableCOMDATFolding>
+ <OptimizeReferences>true</OptimizeReferences>
+ </Link>
+ </ItemDefinitionGroup>
+ <ItemGroup>
+ <None Include="ReadMe.txt" />
+ </ItemGroup>
+ <ItemGroup>
+ <ClInclude Include="stdafx.h" />
+ <ClInclude Include="targetver.h" />
+ <ClInclude Include="Thread.h" />
+ </ItemGroup>
+ <ItemGroup>
+ <ClCompile Include="HttpMediaServer.cpp" />
+ <ClCompile Include="stdafx.cpp" />
+ <ClCompile Include="Thread.cpp" />
+ </ItemGroup>
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
+ <ImportGroup Label="ExtensionTargets">
+ </ImportGroup>
+</Project>
@@ -0,0 +1,119 @@
+#include "Thread.h"
+#include "stdio.h"
+#include <iostream>
+#include "assert.h"
+
+using namespace std;
+
+Thread::Thread() {
+ mHandle = CreateThread (
+ 0, // Security attributes
+ 0, // Stack size
+ ThreadEntry,
+ (void*)this,
+ CREATE_SUSPENDED,
+ &mId);
+ assert(mHandle != 0);
+ if (!mHandle) {
+ mState = eError;
+ cerr << "Can't create thread!\n";
+ } else {
+ mState = eInitialized;
+ }
+}
+
+
+DWORD WINAPI Thread::ThreadEntry(void *aThis) {
+ Thread* t = static_cast<Thread*>(aThis);
+ t->mState = eStarted;
+ t->Run();
+ t->mState = eFinished;
+ t->mHandle = 0;
+ return 0;
+}
+
+
+Thread::~Thread() {
+ assert(mState == eFinished);
+ CloseHandle(mHandle);
+}
+
+
+void Thread::WaitToFinish() {
+ if (mState == eStarted || mState == eInitialized) {
+ WaitForSingleObject(mHandle, INFINITE);
+ assert(mState == eFinished);
+ }
+}
+
+
+void Thread::Start() {
+ if (mState == eInitialized) {
+ mState = eStarted;
+ ResumeThread(mHandle);
+ }
+}
+
+
+// Simple thread to sum the numbers between in range.
+class SumThread : public Thread {
+public:
+ SumThread(int start, int end)
+ : mStart(start)
+ , mEnd(end)
+ , mResult(0)
+ { }
+
+ virtual ~SumThread() { }
+ virtual void Run() {
+ mResult = 0;
+ for (int i=mStart; i<mEnd; i++) {
+ mResult += i;
+ }
+ }
+
+ int mStart, mEnd, mResult;
+};
+
+
+// Trys joining a thread before it's started.
+class WaiterThread : public Thread {
+public:
+ WaiterThread(SumThread &sumThread)
+ : mSumThread(sumThread)
+ , mWasRun(false) {}
+ virtual ~WaiterThread() {}
+ virtual void Run() {
+ mSumThread.WaitToFinish();
+ mWasRun = true;
+ }
+ bool mWasRun;
+ SumThread& mSumThread;
+};
+
+
+
+void Thread::Test() {
+ SumThread t1(0,10);
+ SumThread *t2 = new SumThread(10,20);
+ t1.Start();
+ t2->Start();
+
+ t1.WaitToFinish();
+ t2->WaitToFinish();
+
+ assert(t1.mResult == 45);
+ assert(t2->mResult == 145);
+
+ delete t2;
+
+ SumThread t3(20,30);
+ WaiterThread t4(t3);
+ t4.Start();
+ t3.Start();
+
+ t3.WaitToFinish();
+ t4.WaitToFinish();
+
+ assert(t4.mWasRun);
+}
@@ -0,0 +1,103 @@
+#pragma once
+
+#include "windows.h"
+
+class Thread {
+public:
+ Thread();
+ virtual ~Thread();
+
+ virtual void Run() = 0;
+ virtual void WaitToFinish(); // Join.
+ virtual void Start();
+
+ static void Test();
+private:
+ HANDLE mHandle;
+ DWORD mId;
+ static DWORD WINAPI ThreadEntry(void *aThis);
+
+ enum ThreadState {
+ eError = -1,
+ eInitialized = 1,
+ eStarted = 2,
+ eFinished = 3,
+ };
+
+ // State tracking, for debug purposes.
+ ThreadState mState;
+};
+
+
+// Copied from: http://www.relisoft.com/win32/active.html
+// Simple mutex.
+class Mutex
+{
+ friend class Lock;
+public:
+ Mutex () { InitializeCriticalSection (& _critSection); }
+ ~Mutex () { DeleteCriticalSection (& _critSection); }
+ void Acquire ()
+ {
+ EnterCriticalSection (& _critSection);
+ }
+ void Release ()
+ {
+ LeaveCriticalSection (& _critSection);
+ }
+private:
+ CRITICAL_SECTION _critSection;
+};
+
+// Copied from: http://www.relisoft.com/win32/active.html
+// Stack-based mutex acquire/release.
+class Lock
+{
+public:
+ // Acquire the state of the semaphore
+ Lock ( Mutex & mutex )
+ : _mutex(mutex)
+ {
+ _mutex.Acquire();
+ }
+ // Release the state of the semaphore
+ ~Lock ()
+ {
+ _mutex.Release();
+ }
+private:
+ Mutex & _mutex;
+};
+
+// Copied from: http://www.relisoft.com/win32/active.html
+// An Event is a signalling device that threads use to communicate with
+// each other. You embed an Event in your active object. Then you make
+// the captive thread wait on it until some other thread releases it.
+// Remember however that if your captive thread waits on a event it can't
+// be terminated.
+class Event
+{
+public:
+ Event ()
+ {
+ // start in non-signaled state (red light)
+ // auto reset after every Wait
+ _handle = CreateEvent (0, FALSE, FALSE, 0);
+ }
+
+ ~Event ()
+ {
+ CloseHandle (_handle);
+ }
+
+ // put into signaled state
+ void Release () { SetEvent (_handle); } // Notify
+ void Wait ()
+ {
+ // Wait until event is in signaled (green) state
+ WaitForSingleObject (_handle, INFINITE);
+ }
+ operator HANDLE () { return _handle; }
+private:
+ HANDLE _handle;
+};
@@ -0,0 +1,8 @@
+// stdafx.cpp : source file that includes just the standard includes
+// EchoServer.pch will be the pre-compiled header
+// stdafx.obj will contain the pre-compiled type information
+
+#include "stdafx.h"
+
+// TODO: reference any additional headers you need in STDAFX.H
+// and not in this file
@@ -0,0 +1,15 @@
+// stdafx.h : include file for standard system include files,
+// or project specific include files that are used frequently, but
+// are changed infrequently
+//
+
+#pragma once
+
+#include "targetver.h"
+
+#include <stdio.h>
+#include <tchar.h>
+
+
+
+// TODO: reference additional headers your program requires here
@@ -0,0 +1,8 @@
+#pragma once
+
+// Including SDKDDKVer.h defines the highest available Windows platform.
+
+// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and
+// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h.
+
+#include <SDKDDKVer.h>

0 comments on commit 2250065

Please sign in to comment.